Transcript Java Review

CS2006- Data Structures I
Chapter 5
Linked Lists II
ADT Linked List

Specification:

Elements:



Elements are nodes
One reference
Operations:

isEmpty:


getLength


Delete an element from a specific position
retrieve


Insert a new element at a specific position
remove


Get the length of the list
insert


Check if the list is empty
Rerieve contents of an element From a specific position
traverse

Perform a specific Operation on all the elements of the list
Inserting a Node

Steps:
1. Determine the point of insertion
2. Create a new node & store the new data
3. Connect the new node to the list by changing
“pointers”
Special case?
Inserting a Node

Finding Point of Insertion in a Sorted List
 Java Code: Using for-loop
for

(prev = NULL, cur = Head;
/ / Start
(cur !=NULL) && (newValue > cur.getItem()); / / Termination
prev = cur, cur = cur.getNext());
/ / Step
Determining the values of cur & prev is simpler when
you insert or delete a node by position instead of by
value
Deleting a Node

Node Deletion

Steps:
1. Locate the node that you want to delete (find cur)
2. Disconnect the node from the list by changing
“pointers”
3. Return the node to the system

Special cases?
Deleting a node not at the head


Deleting a node not at the head is similar to
adding a node to the middle of the list.
We would have to set up previous (prev)
and current (cur)

example
Deleting from the middle
public void deleteNode() {
if(cur != null && prev != null){
prev.setNext(cur.getNext());
cur = cur.getNext();
}

The deleteNode() method works even if the
node is a tail node. In this case, the next of the
previous node will be assigned the null value.
Deleting a Node

Node Deletion:
 Observations


Links of the list can't be followed
backwards
Two external “pointers” are needed:



cur: Node to be deleted
prev: Node pointing to cur
List should be traversed to find the proper
positions of cur & prev
Traversing a Linked List


Visit each node in the list , do some operation
(e.g. display) on its items, until the end of the
list is reached
Example

Displaying the Contents of a Linked List
Traversing a Linked List
for ( Node cur = head ; cur != null ; cur = cur.getNext() ) {
System.out.println ( cur.getItem() );
}
cur
head
Recursive?
1
3
7
9 null
Insert and Delete operations on
an empty list

Problem with insertion and deletion methods:


They require special cases and different actions for first nodes.
The addition of a dummy head node to the linked
list eliminates the special cases
“dummy" head node does not contain any data and its
reference points to the first data containing node.
An empty list now consists of a head reference and a header
node with a null reference
Interface for ADT List
// ****************************************************
// Interface for the ADT list
//for easy understanding, I change Comparable to Object
// ****************************************************
public interface ListInterface {
// list operations:
public boolean isEmpty();
public int size();
public void addFirst(Object item);
public void addLast(Object item);
public void remove(Object item);
public Node find(Object item);
public void removeAll();
} // end ListInterface
Comparable Node Class
public class Node {
private Object item;
private Node next;
public Node(Object newItem) {
item = newItem;
next = null;
} // end constructor
public Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
} // end constructor
Comparable Node Class (2)
public void setItem(Object newItem) {
item = newItem;
} // end setItem
public Object getItem() {
return item;
} // end getitem
public void setNext(Node nextNode) {
next = nextNode;
} // end setNext
public Node getNext() {
return next;
} // end getNext
} // end class Node
Implementation
of
ADT
List
// ****************************************************
// Reference-based implementation of ADT list.
// ****************************************************
public class List implements ListInterface {
// reference to linked list of items
private Node head;
private int numItems; // number of items in list
public List() {
numItems = 0;
head = null;
} // end default constructor
public boolean isEmpty( ) {
return numItems == 0;
} // end isEmpty
public int size( ) {
return numItems;
} // end size
Implementation
of ADT List
// ****************************************************
// Reference-based implementation of ADT list.
// ****************************************************
public class List implements ListInterface {
// reference to linked list of items
private Node head;
private int numItems; // number of items in list
public List() {
numItems = 0;
head = null;
} // end default constructor
public boolean isEmpty( ) {
return numItems == 0;
} // end isEmpty
public int size( ) {
return numItems;
} // end size
Implementation of ADT List (2)
public Node find(Object findItem) {
// Locates a specified node in a linked list.
// Returns a reference to the desired node.
Node curr = head;
while((curr != null) && (!findItem.equals (curr.getItem)) {
curr = curr.getNext();
} // end while
return curr;
} // end find
public void addFirst(Object item) {
// insert a new first node into the list
Node newNode = new Node(item, head);
head = newNode;
numItems++;
} // end addFirst
Implementation of ADT List (3)
public void addLast(Object item) {
// insert a new last node into the list
Node curr = head;
if (curr == null) { // insert a new first (and only) node
Node newNode = new Node(item, head);
head = newNode;
}
else {
while(curr.getNext() != null)
curr = curr.getNext();
// curr now contains a ref to the last node on the list
Node newNode = new Node(item);
curr.setNext(newNode);
}
numItems++;
} // end addLast
Implementation of ADT List (4)
public void remove(Object removeItem) {
// if(isEmpty()) return;
Node curr = head, prev = null;
if(curr == null) return;
while((curr != null) && (!removeItem.equals (curr.getItem)) {
prev = curr;
curr = curr.getNext();
} // end while - if curr == null removeItem was not found
if(curr != null) { // if node is not found do nothing
if(curr == head) // remove first node
head = head.getNext();
else
prev.setNext(curr.getNext()); // remove node after prev
numItems--;
}
} // end remove
Implementation of ADT List (5)
public void removeAll() {
// setting head to null causes list to be
// unreachable and thus marked for garbage collection
head = null;
numItems = 0;
} // end removeAll
Implementation of ADT List (5)
public void removeAll() {
// setting head to null causes list to be
// unreachable and thus marked for garbage collection
head = null;
numItems = 0;
} // end removeAll
Implementation of ADT List (6)
public void printList() {
// calls the recursive method to print the linked list
printNode(head);
System.out.println();
}
private void printNode(Node curr) {
// the recursive printing method
if(curr != null){
System.out.print(curr.getItem()+" ");
printNode(curr.getNext());
}
}
} // end List
Implementation of ADT List (6)
public void printList() {
// calls the recursive method to print the linked list
printNode(head);
System.out.println();
}
private void printNode(Node curr) {
// the recursive printing method
if(curr != null){
System.out.print(curr.getItem()+" ");
printNode(curr.getNext());
}
}
} // end List
Arrays vs. Lists
Arrays
Lists
Fixed size
Can have arbitrary length
Items placed in sequence
Items linked to one another using
reference
Number of items can't exceed array
size
Number of items can increase
indefinitely
All elements following the
insertion/deletion position have to
be shifted
reference change used to
insert/delete
Review

The last node of a linear linked list
______.




25
has the value null
has a next reference whose value is null
has a next reference which references the
first node of the list
cannot store any data
Review

A reference variable whose sole purpose
is to locate the first node in a linked list is
called ______.




26
top
front
Head
first
Review

Which of the following will be true when
the reference variable curr references the
last node in a linear linked list?




27
curr == null
head == null
curr.getNext() == null
head.getNext() == null
Review

If a linked list is empty, the statement
head.getNext() will throw a(n) ______.




28
IllegalAccessException
ArithmeticException
IndexOutOfBoundsException
NullPointerException
Review

To delete a node N from a linear linked
list, you will need to ______.




29
set the reference next in the node that precedes N to
reference the node that follows N
set the reference next in the node that precedes N to
reference N
set the reference next in the node that follows N to
reference the node that precedes N
set the reference next in N to reference the node that
follows N
Review

Which of the following statements deletes
the node that curr references?




30
prev.setNext(curr);
curr.setNext(prev);
curr.setNext(curr.getNext());
prev.setNext(curr.getNext());
Review

Which of the following statements deletes
the first node of a linear linked list that has
10 nodes?




31
head.setNext(curr.getNext());
prev.setNext(curr.getNext());
head = head.getNext();
head = null;