Hasnain’s CSC 300 Archived Resources

Deque:

When working with a doubly linked list, it is important to keep in mind the previous pointer. Lets look at the node class:

 

private static class Node {
public Node() { } // the constructor
int item;
Node next;
Node prev;
}

This is our nested class that is the ‘blueprint’ to our Node object. If we want to create the node object, we must use the word new: “Node newNode = new Node()”. In java, this actually creates a Node object and is not just a pointer. On the other hand: “Node point”, is how we create just a pointer. The two pieces of code do different things and you should know when to use them. The first should be used when actually adding a new Node to a linked list. The other can be used to help refer to nodes when manipulating a list. Note, a pointer can point to any node all you have to do is use the ‘=’ sign. For example, if I type “point = newNode”, then pointer (“point“) is pointing towards newNode. We can access the information in newNode (i.e., prev, next, and item) in 2 ways: ‘newNode.next’ or ‘pointer.next’.

Java Linked List:

While we talk about linked lists in class, I wanted to go over why we talked about a first/header pointer at the beginning of the list but not a tail pointer (yet). We are discussing a stack, which follows the LIFO property (meaning Last In, First Out). The last thing entered into the stack becomes the first node. This means that we can only remove in constant time nodes from the front of the list (where the first/header pointer is). To remove from anywhere else in the list, we would have to traverse the list (through a while loop or recursion) and examine each node. This of course would be slower than constant time. Here is a video that discusses the linked list implementation in java.

 

Java Recursion

 

For-Loops to Reverse an int Array

So I wanted to go over the purpose of for-loops since they are so important for this class. Here is an example of using a For-Loop to reverse the elements in an array:

/*I used ideone.com for this program because it is a lot faster than starting eclipse if all you want to do is play around with some code. Copy this code into ideone.com and you should get the same output*/

/* Following import statements allows our program to use java libraries. Need this for print and array setup */

import java.util.*;
import java.lang.*;
import java.io.*;

class SIBlog
{
public static void main (String[] args) throws java.lang.Exception
{
int[] test = {1,2,3,4,5,6}; /* square brackets tell java to create an array and int tells java the elements will be of type integer. */
int temp; // Declare int variable called temp

for (int i = 0; i <test.length/2; i++) { //stop when it reaches half the array length using integer division

temp = test[i]; //assign value into temp
test[i] = test[(test.length-1)-i]; // swaps values
test[(test.length-1)-i] = temp; // uses temp to store value back in array

}

System.out.println(test.toString()); //Gives us memory address >> [I@106d69c
System.out.println(test); //Gives us memory address   > [I@106d69c
System.out.println(Arrays.toString(test)); // Gives us string representation of array > [6, 5, 4, 3, 2, 1]

/* “Integer division is division in which the fractional part
(remainder) is discarded is called integer division and is sometimes denoted” . */
//Examples of Integer Division
System.out.println(6/2); // give us the number 3
System.out.println(7/2); // give us the number 3
System.out.println(8/2); // give us the number 4

}

} // end of class file/program

 

Output:

[I@106d69c
[I@106d69c
[6, 5, 4, 3, 2, 1]
3
3
4

 

About Me

I am a computer science senior at DePaul and look forward to helping student gain a strong understanding of CSC 300. I have been an SI leader for a year and a half now and have been an SI for accounting in the past.

 

———————————————————————————————————-

Below are a bunch of resources that I think are helpful for CSC 300 students. If you have any questions come to an SI session or leave a comment below. You can also upload any of your helpful resources in the comment section!

Tips

The algorithms that we will learn will go a long way in helping you in your future classes and especially in interviews so make sure to come to supplemental instruction!

Videos and Handouts

Leave a Reply

Your email address will not be published. Required fields are marked *