




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





In CS1, you learned how to implement a linked list in C. In Java, the main difference will be that you do not explicitly use pointers. Instead, since every non-primitive is a reference, you will simply link together nodes using references.
Here is the Node class that we will use:
public class Node {
public int data; public Node next;
public Node() { data = 0; next = null; }
public Node(int x) { data = x; next = null; } }
The most important methods in a linked list class are those for insertion and deletion. These are the two I have selected to show you today. In this implementation, the linked list of integers is maintained in numerical order.
Here are a list of issues we should think about before looking at the code.
With respect to deletions, the same types of questions arise:
Let's go through and draw some examples of how to do each of these things before analyzing the code.
// Insertion into the front of the LL object. if (head.data > x) { temp.next = head; head = temp; }
else {
// Set up helper reference to refer to the node that the inserted // node should be inserted after. Node helper = head; while ((helper.next != null) && (helper.next.data < x)) helper = helper.next;
// Adjust necessary references. temp.next = helper.next; helper.next = temp; } } }
// Removes a node storing the value x. If no such node exists, nothing // is done and the method returns false. Otherwise, the node is // removed and true is returned. public boolean remove(int x) {
// Can't remove anything from an empty list. if (head == null) return false;
// Remove the first element, if necessary. if (head.data == x) { head = head.next; return true; }
// Set up helper reference to refer to the node right before the node // to be deleted would be stored. Node helper = head; while ((helper.next != null) && (helper.next.data < x)) helper = helper.next;
// If x was too big to be on the list, simply return false. if (helper.next == null) return false;
// Only if the appropriate node stores x should it be deleted. if (helper.next.data == x) { helper.next = helper.next.next; return true; }
return false; // Case where x is not found. }
// Prints out all the elements in the LL object in the order they are // stored. public void printlist() {
Node temp = head; while (temp != null) { System.out.print(temp.data+" "); temp = temp.next; } }
You should have developed a linked list stack implementation in recitation last week. Here are some key observations about such an implementation:
public class Stack { private Node front;
public Stack() { front = null; }
public void push(int n) { Node temp = new Node(n); temp.next = front; front = temp; }
public int pop(int n) { if (front == null) return -1; int retval = front.data; front = front.next; return retvall; } }
One key observation is necessary about this implementation:
Two node instance variables are needed for an efficient implementation because enqueues and dequeues access the opposite "sides" of the data. Since this is the case, both instance variables have to be maintained. Here is some of the class:
public class Queue {
private Node front; private Node back;
public Queue() { front = null; back = null; }
public void enqueue(int n) { back = new Node(n); if (front == null) front = back; }
public int dequeue() { if (front==null) return -1; int retval = front.data; front = front.next; return retval; } }