Download Data Abstraction and Problem Solving with JAVA

Document related concepts
no text concepts found
Transcript
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
• 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 intRef;
intRef = 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.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
MyNumber.java
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
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Using the class MyNumber
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley
Using the class MyNumber
• 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);
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 capacityIncrement = 0;
int capacity = 10;
double [] myArray = {1, 2, 3, 4, 5};
if (capacityIncrement == 0) {
capacity *= 2;
}
else {
capacity += capacityIncrement;
}
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 [] 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;
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
IntegerNode.java
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
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
Using the class IntegerNode
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
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
A More Flexible Class Node.java
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
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
Using the class Node
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.getNext( )
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.setNext(curr.getNext();
• If the node to be deleted is the first node in the list
head = head.getNext();
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.
newNode.setNext(curr);
prev.setNext(newNode);
• If the node to be inserted is the first node in the list
newNode.setNext(head);
head = newNode;
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.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
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.setNext(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.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