Presentation is loading. Please wait.

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.

Presentation on theme: "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."— Presentation transcript:

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

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); 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 8 myArray: 0123

17 Resizable Arrays Textbook, pp. 158 731 8 myArray : 0123 tempArray: 0123 4567

18 Resizable Arrays Textbook, pp. 158 731 8 myArray: 0123 731 8 tempArray : 0123 4567

19 Resizable Arrays Textbook, pp. 158 731 8 myArray : 0123 731 8 tempArray : 0123 4567

20 Resizable Arrays Textbook, pp. 158 731 8 myArray : 0123 731 8 tempArray : 0123 4567 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 Objectitem; private Nodenext; } 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 3 Textbook, pp. 175

27 Node class public class Node { private Objectitem; private Nodenext; 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

Download ppt "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."

Similar presentations

Ads by Google