Top Banner
CS2006- Data Structures I Chapter 5 Linked Lists II
31

CS2006- Data Structures I Chapter 5 Linked Lists II.

Jan 18, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS2006- Data Structures I Chapter 5 Linked Lists II.

CS2006- Data Structures I

Chapter 5Linked Lists II

Page 2: CS2006- Data Structures I Chapter 5 Linked Lists II.

ADT Linked List Specification:

Elements: Elements are nodes One reference

Operations: isEmpty:

Check if the list is empty getLength

Get the length of the list insert

Insert a new element at a specific position remove

Delete an element from a specific position retrieve

Rerieve contents of an element From a specific position traverse

Perform a specific Operation on all the elements of the list

Page 3: CS2006- Data Structures I Chapter 5 Linked Lists II.

Inserting a Node

Steps:1. Determine the point of insertion2. Create a new node & store the new data3. Connect the new node to the list by changing

“pointers”

Special case?

Page 4: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 5: CS2006- Data Structures I Chapter 5 Linked Lists II.

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?

Page 6: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 7: CS2006- Data Structures I Chapter 5 Linked Lists II.

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.

Page 8: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 9: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 10: CS2006- Data Structures I Chapter 5 Linked Lists II.

Traversing a Linked List

for ( Node cur = head ; cur != null ; cur = cur.getNext() ) {

System.out.println ( cur.getItem() );

}

head 1 3 7 nullnull9

cur

Recursive?

Page 11: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 12: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 13: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 14: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 15: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 16: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 17: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 18: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 19: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 20: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 21: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 22: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 23: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 24: CS2006- Data Structures I Chapter 5 Linked Lists II.

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

Page 25: CS2006- Data Structures I Chapter 5 Linked Lists II.

25

Review The last node of a linear linked list

______. 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

Page 26: CS2006- Data Structures I Chapter 5 Linked Lists II.

26

Review A reference variable whose sole purpose

is to locate the first node in a linked list is called ______. top front Head first

Page 27: CS2006- Data Structures I Chapter 5 Linked Lists II.

27

Review Which of the following will be true when

the reference variable curr references the last node in a linear linked list? curr == null head == null curr.getNext() == null head.getNext() == null

Page 28: CS2006- Data Structures I Chapter 5 Linked Lists II.

28

Review If a linked list is empty, the statement

head.getNext() will throw a(n) ______. IllegalAccessException ArithmeticException IndexOutOfBoundsException NullPointerException

Page 29: CS2006- Data Structures I Chapter 5 Linked Lists II.

29

Review To delete a node N from a linear linked list,

you will need to ______. 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

Page 30: CS2006- Data Structures I Chapter 5 Linked Lists II.

30

Review Which of the following statements deletes

the node that curr references? prev.setNext(curr); curr.setNext(prev); curr.setNext(curr.getNext()); prev.setNext(curr.getNext());

Page 31: CS2006- Data Structures I Chapter 5 Linked Lists II.

31

Review Which of the following statements deletes

the first node of a linear linked list that has 10 nodes? head.setNext(curr.getNext()); prev.setNext(curr.getNext()); head = head.getNext(); head = null;