Top Banner
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005 by 4 pm
29

1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

Mar 30, 2015

Download

Documents

Pablo Michelman
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 2: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

2

Java Reference Variables

Integer intRef = new Integer(5);

intRef:

Textbook, pp. 153-154

Page 3: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

3

What does equal mean?

Page 4: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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:

Page 5: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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:

Page 6: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 7: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 8: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

8

Example: Arrays

int[] a = new int[5];

a[0] = 3;

Page 9: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 10: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 11: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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)

Page 12: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

12

A few list ADT operations

• list.get(3)

Page 13: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

13

A few list ADT operations

• list.get(3)• list.remove(3)

Page 14: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

14

A few list ADT operations

• list.get(3)• list.remove(3)• list.add(3, butter)

Page 15: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

15

A few list ADT operations

• list.get(3)• list.remove(3)• list.add(3, butter)• list.add(3, beer)

Page 16: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

16

Resizable Arrays

Textbook, pp. 158

731 8myArray:0 1 2 3

Page 17: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

17

Resizable Arrays

Textbook, pp. 158

731 8myArray:0 1 2 3

tempArray: 0 1 2 3

4 5 6 7

Page 18: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

18

Resizable Arrays

Textbook, pp. 158

731 8myArray:0 1 2 3

731 8tempArray: 0 1 2 3

4 5 6 7

Page 19: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

19

Resizable Arrays

Textbook, pp. 158

731 8myArray:0 1 2 3

731 8tempArray: 0 1 2 3

4 5 6 7

Page 20: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 21: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 22: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

22

A few list ADT operations

• list.get(3)• list.remove(3)• list.add(3, butter)• list.add(3, beer)

Page 23: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

23

A Node Data Structure

public class Node {

private Object item;

private Node next;

}

Beer Wine

Textbook, pp. 161

Page 24: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

24

Recursive Definition

• Milk• Eggs• Butter• Apples• Bread• Chicken

A List is a first item,

followed by a List

Page 25: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

25

Terminology

myList

Beer Wine Milk

Page 26: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

26

ListReferenceBased Class

public class ListReferenceBased {

private Node head;

private int numItems;

}

Beer Wine Milk

3Textbook, pp. 175

Page 27: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 28: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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

Page 29: 1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.

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