Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem Solving with JAVA: Walls and Mirrors Carrano / Prichard Linked Lists
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Basics about Object Reference Programming with Linked Lists Variations (if time allows) Applcation Linked Lists
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.1 a) A linked list of integers; b) insertion; c) deletion
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Object References Resizeable Arrays Reference-based Linked Lists Basics about Object References
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Reference variable contains the location (address in memory) of an object Integer intRef; intRef = new Integer(5); Object References
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.2 A reference to an Integer object
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Integer p, q; p = new Integer(6); q = p; Object References
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.3a-d a) Declaring reference variables; b) allocating an object; c) allocating another object, with the dereferenced object marked for garbage collection
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.3e-g e) allocating an object; f) assigning null to a reference variable; g) assigning a reference with a null value
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley public class MyNumber { private int num; public MyNumber(int n) { num = n; } // end constructor public String toString() { return "My number is " + num; } // end toString } // end class MyNumber MyNumber.java
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley MyNumber x = new MyNumber(9); MyNumber y = new MyNumber(9); MyNumber z = x; Although objects x and y contain the same data, the == (equals to) operator returns false, since x and y refer to different objects. However, x == z returns true because both x and z refer to the same object. Using the class MyNumber
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Suppose a method is defined public void changeNumber(MyNumber n) { n = new MyNumber(5); } // end changeNumber What will the following Java statements produce? MyNumber x = new MyNumber(9); changeNumber(x); // attempts to assign 5 to x System.out.println(x); Using the class MyNumber
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.4 The value of a parameter does not affect the argument’s value
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley int capacityIncrement = 0; int capacity = 10; double [] myArray = {1, 2, 3, 4, 5}; if (capacityIncrement == 0) { capacity *= 2; } else { capacity += capacityIncrement; } Resizable Arrays
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley // now create a new array using the updated // capacity value double [] newArray = new double[capacity]; // copy the contents of the original array // to the new array for (int i = 0; i < myArray.length; i++) { newArray[i] = myArray[i]; } // end for // now change the reference to the original array // to the new array myArray = newArray; Resizable Arrays - cont’d
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley A linked list contains components that are linked to one another. Each component contains both data and a “link” to the next item. Reference-based Linked List
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.5 A node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley public class IntegerNode { private int item; private IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor IntegerNode.java
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley public void setItem(int newItem) { item = newItem; } // end setItem public int getItem() { return item; } // end getitem public void setNext(IntegerNode nextNode) { next = nextNode; } // end setNext public IntegerNode getNext() { return next; } // end getNext } // end class IntegerNode
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley IntegerNode n1 = new IntegerNode(); IntegerNode n2 = new IntegerNode(); n1.setItem(5); // set item in first node n2.setItem(9); // set itme in second node n1.setNext(n2); // link the nodes Using the class IntegerNode
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.6 The result of linking two instances of IntegerNode
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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 A More Flexible Class - Node.java
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley 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
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Node n = new Node(new Integer(6)); Node first = new Node(new Integer(9), n); Using the class Node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.7 Using the Node constructor to initialize a data field and a link value
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.8 A head reference to a linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.9 A lost node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Let a variable curr reference the first node in the linked list while (the curr reference is not null) { Display the data portion of the current node Set the curr reference to the next field of the current node } // end while Displaying the Contents of a Linked List
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.10 The effect of the assignment curr = curr.getNext( )
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Locate the node to be deleted. Disconnect this node from the linked list by changing references. Return the node to the system. prev.setNext(curr.getNext(); If the node to be deleted is the first node in the list head = head.getNext(); Deleting a Specified Node from a Linked List
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.11 Deleting a node from a linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.12 Deleting the first node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Determine the point of insertion. Create a new node and store the new data in it. Connect the new node to the linked list by changing references. newNode.setNext(curr); prev.setNext(newNode); If the node to be inserted is the first node in the list newNode.setNext(head); head = newNode; Inserting a Node into a Specified Position of a Linked List
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.13 Inserting a new node into a linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.14 Inserting at the beginning of a linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.15 Inserting at the end of a linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.16 When prev references the last node and curr is null, insertion will be at the end of the linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.17 When prev is null and curr references the first node, insertion or deletion will be at the beginning of the linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley A Reference-based Implementation of the ADT List
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.18 A reference-based implementation of the ADT list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Array-Based Implementation –fixed size –time consuming to implement resizable list –takes more time to insert and delete –saves storage for each data item, but wastes storage to declare more than necessary Reference-based Implementation –flexible in size –easy to insert and delete –wastes storage to hold links Comparing Array-Based and Reference-based Implementation
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Processing Linked Lists Recursively
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.19 A head reference as an argument
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.20 a) A sorted linked list; b) the assignment made for insertion at the beginning of the list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.21a and 4.21b a) The initial call insert Recursive(head, newItem) ; b) the first recursive call
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.21c c) the second recursive call inserts at the beginning of the list that headNode references
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Tail References Circular Linked Lists Dummy Head Nodes Doubly Linked Lists Variations of the Linked List
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Add an item to the end of a list –Allocate a new node for the linked list. –Set the references in the last node in the list to reference the new node. –Put the new request (reference to the new item) in the new node. –Set the reference in the new node to null. If tail points to the end of the linked list –tail.setNext(new Node(request, null)); Tail References
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.22 A linked list with head and tail references
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.23 A circular linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.24 A circular linked list with an external reference to the last node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.25 A dummy head node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.26 a) A dummy head node with global information; b) a head record with global information
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.27 A doubly linked list
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.28 a) A circular doubly linked list with a dummy head node; b) an empty list with a dummy head node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.29 Reference changes for deletion
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.30 Reference changes for insertion
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Have value: number of videos currently in stock. Want value: number of videos that should be in stock. When the have value is less than the want value, more videos are ordered. Wait list: list of names of people waiting for the title if it is sold out. Application: Maintaining an Inventory
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley The design of a solution The implementation of the solution The final set of refinements to the program Main Design Stages
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.31a and 4.31b a) Inventory list node; b) wait list node
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.31c c) orthogonal structure for the inventory
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.32 Linked list for Self-Test Exercise 2, 3, and 7
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.33 Two circular linked lists
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.34 A sparse polynomial
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4.35 a) An array-based implementation of the linked list in Figure 4-32; b) after inserting D in sorted order; c) after deleting B