Data Abstraction and Problem Solving with JAVA Walls

Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Linked Lists Data Abstraction and Problem Solving with JAVA: Walls and Mirrors Carrano / Prichard

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Linked Lists • • Basics about Object Reference Programming with Linked Lists Variations (if time allows) Applcation

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 Basics about Object References • Resizeable Arrays • Reference-based Linked Lists

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Object References • Reference variable contains the location (address in memory) of an object Integer int. Ref; int. Ref = new Integer(5);

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 Object References Integer p, q; p = new Integer(6); q = p;

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4. 3 a-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. 3 e-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 My. Number. java public class My. Number { private int num; public My. Number(int n) { num = n; } // end constructor public String to. String() { return "My number is " + num; } // end to. String } // end class My. Number

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Using the class My. Number x = new My. Number(9); My. Number y = new My. Number(9); My. Number 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.

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Using the class My. Number • Suppose a method is defined public void change. Number(My. Number n) { n = new My. Number(5); } // end change. Number • What will the following Java statements produce? My. Number x = new My. Number(9); change. Number(x); // attempts to assign 5 to x System. out. println(x);

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 Resizable Arrays int capacity. Increment = 0; int capacity = 10; double [] my. Array = {1, 2, 3, 4, 5}; if (capacity. Increment == 0) { capacity *= 2; } else { capacity += capacity. Increment; }

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Resizable Arrays - cont’d // now create a new array using the updated // capacity value double [] new. Array = new double[capacity]; // copy the contents of the original array // to the new array for (int i = 0; i < my. Array. length; i++) { new. Array[i] = my. Array[i]; } // end for // now change the reference to the original array // to the new array my. Array = new. Array;

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Reference-based Linked List • A linked list contains components that are linked to one another. • Each component contains both data and a “link” to the next item.

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 Integer. Node. java public class Integer. Node { private int item; private Integer. Node next; public Integer. Node(int new. Item) { item = new. Item; next = null; } // end constructor public Integer. Node(int new. Item, Integer. Node next. Node) { item = new. Item; next = next. Node; } // end constructor

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley public void set. Item(int new. Item) { item = new. Item; } // end set. Item public int get. Item() { return item; } // end getitem public void set. Next(Integer. Node next. Node) { next = next. Node; } // end set. Next public Integer. Node get. Next() { return next; } // end get. Next } // end class Integer. Node

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Using the class Integer. Node n 1 = new Integer. Node(); Integer. Node n 2 = new Integer. Node(); n 1. set. Item(5); // set item in first node n 2. set. Item(9); // set itme in second node n 1. set. Next(n 2); // link the nodes

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 Integer. Node

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley A More Flexible Class Node. java public class Node { private Object item; private Node next; public Node(Object new. Item) { item = new. Item; next = null; } // end constructor public Node(Object new. Item, Node next. Node) { item = new. Item; next = next. Node; } // end constructor

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley public void set. Item(Object new. Item) { item = new. Item; } // end set. Item public Object get. Item() { return item; } // end get. Item public void set. Next(Node next. Node) { next = next. Node; } // end set. Next public Node get. Next() { return next; } // end get. Next } // end class Node

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Using the class Node n = new Node(new Integer(6)); Node first = new Node(new Integer(9), n);

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 Displaying the Contents of a Linked List 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

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. get. Next( )

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Deleting a Specified Node from a Linked List • Locate the node to be deleted. • Disconnect this node from the linked list by changing references. • Return the node to the system. prev. set. Next(curr. get. Next(); • If the node to be deleted is the first node in the list head = head. get. Next();

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 Inserting a Node into a Specified Position of a Linked List • 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. new. Node. set. Next(curr); prev. set. Next(new. Node); • If the node to be inserted is the first node in the list new. Node. set. Next(head); head = new. Node;

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 Comparing Array-Based and Reference-based Implementation • 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

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. 21 a and 4. 21 b a) The initial call insert Recursive(head, new. Item); 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. 21 c c) the second recursive call inserts at the beginning of the list that head. Node references

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Variations of the Linked List • • Tail References Circular Linked Lists Dummy Head Nodes Doubly Linked Lists

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Tail References • 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. set. Next(new Node(request, null));

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 Application: Maintaining an Inventory • 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.

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Main Design Stages • The design of a solution • The implementation of the solution • The final set of refinements to the program

Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 4. 31 a and 4. 31 b 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. 31 c 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
- Slides: 65