Download presentation
Presentation is loading. Please wait.
1
1 Lecture 25 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
2
2 Lecture 25 Basic Node Design l A node in a linked list contains data elements and link elements. 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. Stores any kind of object.
3
3 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.
4
4 Lecture 25 Manipulating the Phone List l A class to manipulate the 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. Null reference defines empty list.
5
5 Lecture 25 Inserting Nodes into a List l Insert at the end of the list. Case 1: Empty list Case 2: Nonempty list. Head 529-8109 Newnode Before Insertion After Insertion Head 529-8109 Ahmed 529-5112 Omer Head 529-8109 Ahmed 529-5112 Omer Newnode 529-0011 (a) Insertion into Empty List ( b) Insertion into Existing List
6
6 Lecture 25 The insert() Method If the list is not empty, insert() traverses the list and inserts at the end. 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 last node current = current.getNext(); // go to the next node current.setNext( newNode ); // Do the insertion } } // insert() Case 1: Empty list Case 2: Nonempty list. Note loop bound.
7
7 Lecture 25 Traversing a List Traverse: current always points to current node. New node inserted after the last node.
8
8 Lecture 25 Printing the Nodes of a List l Traverse the list, printing each 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() Traverse: current always points to current node. Note loop bound. l Terminating Traversal: The loop bound depends on whether you need a reference to the last node or not after the loop terminates.
9
9 Lecture 25 Looking up a Node in a List l 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.
10
10 Lecture 25 Removing a Node From a List l To remove, the list must be re-linked. Head Previous Head (a) Removing First Node Head (b) Removing Other Node Removed nodes will be garbage collected Current Previous Re-link the head. Two pointers needed to re- link an internal node.
11
11 Lecture 25 The remove() Method 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() Pointer to previous node. Cut out the node. Re-link the head. a Head Previous Head Current Previous
12
12 Lecture 25 Testing the List 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() l Strategy »Test insertions. »Test lookups, both successes and failures. »Test removals at different locations.
13
13 Lecture 25 Testing the List // 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")); l Searches: l 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();
14
14 Lecture 25 OOD: The List Abstract Data Type (ADT ) l Wanted: a generic list structure. l An Abstract Data Type (ADT) has two parts: »the data that are being stored and manipulated, »the methods and operations on those data. l Example: Integer data »Data: all the integral values »Operations: +, -, /, *, % l Example: List »Data: the objects to be stored. »Operations: insert, delete, search. l Information Hiding: Hide implementation!
15
15 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(); }
16
16 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.
17
17 Lecture 25 List Insertion Methods 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() The inserted object goes into a new Node which is inserted in the list.
18
18 Lecture 25 List Removal Methods 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() public Object removeFirst() { Node first = head; head = head.getNext(); return first; } // removeFirst() Re-link the list. Head (a) Removing First Node Removed nodes will be garbage collected Head Previous Head Current Previous
19
19 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().
20
20 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.