CompSci 100e Program Design and Analysis II April 26, 2011 Prof. Rodger CompSci 100e, Spring20111.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh...
-
Upload
pablo-michelman -
Category
Documents
-
view
213 -
download
0
Transcript of 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh...
1CompSci 105 SS 2005
Principles of Computer Science
Lecture 11: Linked Lists
Lecturer: Santokh Singh
Assignment 2 due
tomorrow, i.e. Friday
21 Jan 2005by 4 pm
2
Java Reference Variables
Integer intRef = new Integer(5);
intRef:
Textbook, pp. 153-154
3
What does equal mean?
4
Equality of References
Integer p, q;
p = new Integer(5);
q = new Integer(5);
if ( p == q )
println(“Equal”);
else
println(“Not”);
p: q:
5
Equality of References
Integer p, q;
p = new Integer(5);
p = new Integer(5);
if ( p.equals(q) )
println(“Equal”);
else
println(“Not”);
p: q:
6
References and Objects
Example: Parameter Passing
Example: Resizable Arrays
Linked Lists
The Node class
Lists as Recursive Structures
Displaying a Linked List
Inserting and Deleting Elements
Implementing the ADT List
7
Example: Parameters
public void changeNumber( Integer n );
{
n = new Integer(5);
}
Integer p;
p = new Integer(9);
changeNumber(p);
println(p);
Textbook, pp. 157
8
Example: Arrays
int[] a = new int[5];
a[0] = 3;
9
What is the output?
int a = 3;
int b = a;
b = 4;
System.out.println(a);
int[] c = {3};
int[] d = c;
d[0] = 4;
System.out.println(c[0]);
Textbook, pp. 155
Exercise L10.1
10
References and Objects
Example: Parameter Passing
Example: Resizable Arrays
Linked Lists
The Node class
Lists as Recursive Structures
Displaying a Linked List
Inserting and Deleting Elements
Implementing the ADT List
11
List ADT
1. Milk
2. Eggs
3. Butter
4. Apples
5. Bread
6. Chicken
Textbook, p. 111ff
• createList()• isEmpty()• size()• add( index, item)• remove(index)• removeAll()• get(index)
12
A few list ADT operations
• list.get(3)
13
A few list ADT operations
• list.get(3)• list.remove(3)
14
A few list ADT operations
• list.get(3)• list.remove(3)• list.add(3, butter)
15
A few list ADT operations
• list.get(3)• list.remove(3)• list.add(3, butter)• list.add(3, beer)
16
Resizable Arrays
Textbook, pp. 158
731 8myArray:0 1 2 3
17
Resizable Arrays
Textbook, pp. 158
731 8myArray:0 1 2 3
tempArray: 0 1 2 3
4 5 6 7
18
Resizable Arrays
Textbook, pp. 158
731 8myArray:0 1 2 3
731 8tempArray: 0 1 2 3
4 5 6 7
19
Resizable Arrays
Textbook, pp. 158
731 8myArray:0 1 2 3
731 8tempArray: 0 1 2 3
4 5 6 7
20
Resizable Arrays
Textbook, pp. 158
731 8myArray:0 1 2 3
731 8tempArray: 0 1 2 3
4 5 6 7
See Java code in textbook on p. 158
21
References and Objects
Example: Parameter Passing
Example: Resizable Arrays
Linked Lists
The Node class
Lists as Recursive Structures
Displaying a Linked List
Inserting and Deleting Elements
Implementing the ADT List
22
A few list ADT operations
• list.get(3)• list.remove(3)• list.add(3, butter)• list.add(3, beer)
23
A Node Data Structure
public class Node {
private Object item;
private Node next;
}
Beer Wine
Textbook, pp. 161
24
Recursive Definition
• Milk• Eggs• Butter• Apples• Bread• Chicken
A List is a first item,
followed by a List
25
Terminology
myList
Beer Wine Milk
26
ListReferenceBased Class
public class ListReferenceBased {
private Node head;
private int numItems;
}
Beer Wine Milk
3Textbook, pp. 175
27
Node classpublic class Node {
private Object item;
private Node next;
public Object getItem() {return item;}
public Node getNext() {return next;}
public void setItem(Object i) {item=i;}
public void setNext(Node n) {next=n;}
}
Beer
Textbook, pp. 161
28
References and Objects
Example: Parameter Passing
Example: Resizable Arrays
Linked Lists
The Node class
Lists as Recursive Structures
Displaying a Linked List
Inserting and Deleting Elements
Implementing the ADT List
29
public void printList( ) {
Node curr = head;
while (curr != null )
{
System.out.println( curr.getItem() );
curr = curr.getNext();
}
}
Beer Wine Milk
head
Textbook, pp. 164