200 likes | 397 Views
Abstract Data Types –III. Overview Basic Node Design Example: The Dynamic Phone List Manipulating the Phone List Inserting Nodes into a List The insert() Method Traversing a List Printing the Nodes of a List Looking up a Node in a List The remove() Method Testing the List
E N D
Abstract Data Types –III Overview • Basic Node Design • Example: The Dynamic Phone List • Manipulating the Phone List • Inserting Nodes into a List • The insert() Method • Traversing a List • Printing the Nodes of a List • Looking up a Node in a List • The remove() Method • Testing the List • The Generic Node Class Lecture 25
Basic Node Design • A node in a linked list contains data elements and link elements. Stores any kind of object. public class Node { private Object data; private Node next; public Node(Object obj); // Constructor public void setData(Object obj); // Data access public Object getData(); public void setNext(Node link); // Link access public Node getNext(); } // Node Data elements. Link elements. Lecture 25
Example: The Dynamic Phone List public class PhoneListNode { private String name; private String phone; private PhoneListNode next; public PhoneListNode(String s1, String s2) { name = s1; phone = s2; next = null; } // PhoneListNode() public void setNext(PhoneListNode nextPtr) { next = nextPtr; } // setNext() public PhoneListNode getNext() { return next; } // getNext() } // PhoneListNode public void setData(String s1, String s2) { name = s1; phone = s2; } // setData() public String getName() { return name; } // getName() public String getData() { return name + " " + phone; } // getData() public String toString() { return name + " " + phone; } // toString() Data elements. Lecture 25
Manipulating the Phone List • A class to manipulate the list. Null reference defines empty list. public class PhoneList { private PhoneListNode head; public PhoneList() { head = null; // Start with empty list } public boolean isEmpty() { // Defines an empty list return head == null; } public void insert(PhoneListNode node) { } public String getPhone(String name) { } public String remove(String name) { } public void print() { } } // PhoneList Design: These methods require little knowledge of the list. Lecture 25
Inserting Nodes into a List • Insert at the end of the list. Case 1: Empty list After Before Insertion Insertion Head Head Newnode 529-8109 Case 2: Nonempty list. (a) Insertion into Empty List Head Ahmed 529-8109 Head Ahmed 529-8109 Omer 529-5112 Omer 529-5112 Newnode 529-0011 (b) Insertion into Existing List Lecture 25
The insert() Method • If the list is not empty, insert() traverses the list and inserts at the end. Case 1: Empty list public void insert(PhoneListNode newNode) { if (isEmpty()) head = newNode; // Insert at head of list else { PhoneListNode current = head; // Start traversal at head while (current.getNext() != null)// While not at the lastnode current = current.getNext(); // go to the next node current.setNext( newNode ); // Do the insertion } } // insert() Note loop bound. Case 2: Nonempty list. Lecture 25
Traversing a List Traverse:current always points to current node. New node inserted after the last node. Lecture 25
Printing the Nodes of a List • Traverse the list, printing each node. Traverse:current always points to current node. public void print() { if (isEmpty()) System.out.println("Phone list is empty"); PhoneListNode current = head; // Start traversal at head while (current != null) {// While not at end of list System.out.println( current.toString() ); // print node's data current = current.getNext();// go to the next node } } // print() Note loop bound. • Terminating Traversal: The loop bound depends on whether you need a reference to the last node or not after the loop terminates. Lecture 25
Looking up a Node in a List • Searching: traverse the list until the name is found or until the end is reached (not found). public String getPhone(String name) { if (isEmpty()) // Case 1: empty list return "Phone list is empty"; else { PhoneListNode current = head; while ((current.getNext() != null) && (!current.getName().equals(name))) current = current.getNext(); if (current.getName().equals(name)) // Case 2: found the name return current.getData(); else // Case 3: no such person return ("Sorry. No entry for " + name); } } // getPhone() Compound condition. current points to correct node. Lecture 25
Removing a Node From a List Previous Current Previous Current • To remove, the list must be re-linked. Head Removed nodes Head will be garbage collected Re-link the head. (a) Removing First Node Head Two pointers needed to re-link an internal node. Head (b) Removing Other Node Lecture 25
The remove() Method Previous Current Previous Current Head Head public String remove(String name) { // Remove an entry by name if (isEmpty())// Case 1: empty list return "Phone list is empty"; PhoneListNode current = head; PhoneListNode previous = null; if (current.getName().equals(name)) { // Case 2: remove first node head = current.getNext(); return "Removed " + current.toString() ; } while ((current.getNext() != null) && (!current.getName().equals(name))) { previous = current; current = current.getNext(); } if (current.getName().equals(name)) { // Case 3: remove named node previous.setNext(current.getNext()); return "Removed " + current.toString(); } else return ("Sorry. No entry for " + name); // Case 4: node not found } // remove() Re-link the head. Pointer to previous node. a Cut out the node. Lecture 25
Testing the List • Strategy • Test insertions. • Test lookups, both successes and failures. • Test removals at different locations. public static void main(String argv[]) { // Create list and insert some nodes PhoneList list = new PhoneList(); list.insert( new PhoneListNode(“Ahmed M", "997-0020")); list.insert( new PhoneListNode(“Omer W", "997-0086")); list.insert( new PhoneListNode(“Salem P", "997-0010")); list.insert( new PhoneListNode(“Nuri M", "997-2101")); list.insert( new PhoneListNode(“Rami K", "997-2517")); // Test whether insertions worked System.out.println( "Phone Directory" ); list.print(); } // main() Lecture 25
Testing the List • Searches: // Test whether lookups work System.out.println("Looking up numbers by name"); System.out.println(list.getPhone(“Ahmed M")); System.out.println(list.getPhone(“Omer R")); System.out.println(list.getPhone(“Salem K")); System.out.println(list.getPhone(“Zaki M")); System.out.println(list.remove(“Majdi P")); • Removals: System.out.println("Phone Directory"); list.print(); // Test removals, printing list after each removal System.out.println(list.remove(“Ahmed M")); System.out.println("Phone Directory"); list.print(); System.out.println(list.remove(“Rami K")); System.out.println("Phone Directory"); list.print(); Lecture 25
OOD: The List Abstract Data Type (ADT) • Wanted: a generic list structure. • An Abstract Data Type (ADT) has two parts: • the data that are being stored and manipulated, • the methods and operations on those data. • Example: Integer data • Data: all the integral values • Operations: +, -, /, *, % • Example: List • Data: the objects to be stored. • Operations: insert, delete, search. • Information Hiding: Hide implementation! Lecture 25
The Generic Node Class public class Node { private Object data; // Stores any kind of data private Node next; public Node(Object obj) { // Constructor data = obj; next = null; } // Link access methods public void setNext( Node nextPtr ) { next = nextPtr; } public Node getNext() { return next; } } // Node // Data access methods public void setData(Object obj) { data = obj; } public Object getData() { return data; } public String toString() { return data.toString(); } Lecture 25
The Generic List Class public class List { private Node head; public List() { head = null; } public boolean isEmpty() { return head == null; } public void print() { } public void insertAtFront( Object newObj ) { } public void insertAtRear( Object newObj ) { } public Object removeFirst() { } public Object removeLast() { } } // List Two kinds of insertions ... … and kinds of removals. Lecture 25
List Insertion Methods The inserted object goes into a new Node which is inserted in the list. public void insertAtFront(Object newObj) { Node current = new Node( newObj ); current.setNext(head); head = current; } public void insertAtRear(Object newObj) { if (isEmpty()) head = new Node(newObj); else { Node current = head; // Start at head of list while (current.getNext() != null) // Find the end of the list current = current.getNext(); current.setNext(new Node(newObj));// Insert the newObj } } // insertAtRear() Lecture 25
List Removal Methods Previous Current Previous Current Head Removed nodes Head will be garbage collected (a) Removing First Node Head public Object removeLast() { if (isEmpty()) // Empty list return null; Node current = head; if (current.getNext() == null) { // Singleton list head = null; return current; } Node previous = null; // All other cases while (current.getNext() != null) { previous = current; current = current.getNext(); } previous.setNext(null); return current; } // removeLast() Re-link the list. public Object removeFirst() { Node first = head; head = head.getNext(); return first; } // removeFirst() Lecture 25
Testing the List ADT public static void main( String argv[] ) { // Create list and insert heterogeneous nodes List list = new List(); list.insertAtFront(new PhoneRecord(“Ahmed M", "997-0020")); list.insertAtFront(new Integer(8647)); list.insertAtFront(new String("Hello World")); list.insertAtRear(new PhoneRecord(“Sami M", "997-2101")); list.insertAtRear(new PhoneRecord(“Rami K", "997-2517")); System.out.println("Generic List"); // Print the list list.print(); // Remove objects and print resulting list Object o; o = list.removeLast(); System.out.println(" Removed " + o.toString()); System.out.println("Generic List:"); list.print(); o = list.removeFirst(); System.out.println(" Removed " +o.toString()); System.out.println("Generic List:"); list.print(); } // main() Insert different types of objects. Note use of toString(). Lecture 25
Sample output of phoneList that defines a node of a linked list of phone records. Sample output of List(a generic list data structures. In linked list Lecture 25