410 likes | 528 Views
CompSci 105 SS 2005 Principles of Computer Science. Lecture 11: Linked Lists. The Wall. ADT Operations. Program that uses the ADT. ADT Implementation. public class AreaCircle implements Circle { private float x, y, radius, area; public void setRadius( float newR ) {
E N D
CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists
The Wall ADT Operations Program that uses the ADT ADT Implementation
public class AreaCircle implements Circle { private float x, y, radius, area; public void setRadius( float newR ) { radius = newRadius; area = Math.PI * radius * radius ); } public void getArea() { return ( area ); } }
public class AreaCircle implements Circle { private float x, y, radius; public void setRadius( float newR ) { radius = newRadius; } public void getArea() { return (Math.PI * radius * radius ); } }
public class AreaCircle implements Circle { public void setRadius( float newR ) { } public void getArea() { return (Math.PI * Math.random()*50 ); } }
Lists What is a list?
List ADT • createList() • isEmpty() • size() • add( index, item) • remove(index) • removeAll() • get(index) • Milk • Eggs • Butter • Apples • Bread • Chicken
A few list ADT operations • list.get(3)
A few list ADT operations • list.get(3) • list.remove(3)
A few list ADT operations • list.get(3) • list.remove(3) • list.add(3, butter)
A few list ADT operations • list.get(3) • list.remove(3) • list.add(3, butter) • list.add(3, beer)
Resizable Arrays 0 1 2 3 1 3 7 8 myArray:
Resizable Arrays 0 1 2 3 1 3 7 8 myArray: tempArray: 0 1 2 3 4 5 6 7
Resizable Arrays 0 1 2 3 1 3 7 8 myArray: 0 1 2 3 4 5 6 7 1 3 7 8 tempArray:
Resizable Arrays 0 1 2 3 1 3 7 8 myArray: 0 1 2 3 4 5 6 7 1 3 7 8 tempArray:
Resizable Arrays 0 1 2 3 1 3 7 8 myArray: 0 1 2 3 4 5 6 7 1 3 7 8 tempArray:
Advantages/Disadvantagesof Using Arrays??How could we do this “better”?
Linked Lists The Node class Lists as Recursive Structures Displaying a Linked List Inserting and Deleting Elements Implementing the ADT List
List ADT • createList() • isEmpty() • size() • add( index, item) • remove(index) • removeAll() • get(index)
A few list ADT operations • list.get(int itemNum) • list.get(3); • list.remove(int itemNum) • list.remove(3); • list.add(int itemNum, Object data) • list.add(3, “beer”);
We know already that an ArrayList works this way. • What other ways could we satisfy this Abstract implementation?
A Node Data Structure public class Node { private Object item; private Node next; } Beer Wine
Recursive Definition • Milk • Eggs • Butter • Apples • Bread • Chicken A List is the first item, followed by a List
Terminology Head Node Node Node Beer Milk Wine
ListReferenceBased Class public class ListReferenceBased { private Node head; private int numItems; } Beer Milk Wine 3
Node class public 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
Linked Lists The Node class Lists as Recursive Structures Displaying a Linked List Inserting and Deleting Elements Implementing the ADT List
public void printList( ) { Node curr = head; while (curr != null ) { System.out.println( curr.getItem() ); curr = curr.getNext(); } } Beer Milk Wine head
Linked Lists The Node class Lists as Recursive Structures Displaying a Linked List Inserting and Deleting Elements Implementing the ADT List
How would we insert and delete an element from an ArrayList??
Deleting the First Node Beer Milk Wine head
Deleting the First Node public void deleteFirstNode() if ( head == null ) // Handle error condition else head = head.getNext(); } Beer Milk Wine head
Inserting at Front Milk Wine head
Inserting at Front public void insertAtFront( Object item ) Node temp = head; head = new Node(); head.setItem( item ); head.setNext( temp ); } Milk Wine head
public void remove(int index) Node prev = null; Node curr = head; while ( index > 1 ) { prev = curr; curr = curr.getNext(); index = index-1; } prev.setNext( curr.getNext() ); } prev curr Beer Milk Wine head
What does this list look like? • Node n5 = new Node(15); • Node n6 = new Node(34); • Node n7 = new Node(12); • Node n8 = new Node(84); • n6.next = n5; • n7.next = n8; • n8.next = n6; • n5.next = null;
Other Types of List • Doubly Linked Lists • Circular Linked Lists • ...more to come later
Doubly Linked Lists Beer Milk Wine head
Circular Linked Lists Beer Milk Wine head