1 / 41

CompSci 105 SS 2005 Principles of Computer Science

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 ) {

treva
Download Presentation

CompSci 105 SS 2005 Principles of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists

  2. The Wall ADT Operations Program that uses the ADT ADT Implementation

  3. 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 ); } }

  4. 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 ); } }

  5. public class AreaCircle implements Circle { public void setRadius( float newR ) { } public void getArea() { return (Math.PI * Math.random()*50 ); } }

  6. Lists What is a list?

  7. List ADT • createList() • isEmpty() • size() • add( index, item) • remove(index) • removeAll() • get(index) • Milk • Eggs • Butter • Apples • Bread • Chicken

  8. A few list ADT operations • list.get(3)

  9. A few list ADT operations • list.get(3) • list.remove(3)

  10. A few list ADT operations • list.get(3) • list.remove(3) • list.add(3, butter)

  11. A few list ADT operations • list.get(3) • list.remove(3) • list.add(3, butter) • list.add(3, beer)

  12. Resizable Arrays 0 1 2 3 1 3 7 8 myArray:

  13. Resizable Arrays 0 1 2 3 1 3 7 8 myArray: tempArray: 0 1 2 3 4 5 6 7

  14. Resizable Arrays 0 1 2 3 1 3 7 8 myArray: 0 1 2 3 4 5 6 7 1 3 7 8 tempArray:

  15. Resizable Arrays 0 1 2 3 1 3 7 8 myArray: 0 1 2 3 4 5 6 7 1 3 7 8 tempArray:

  16. Resizable Arrays 0 1 2 3 1 3 7 8 myArray: 0 1 2 3 4 5 6 7 1 3 7 8 tempArray:

  17. Advantages/Disadvantagesof Using Arrays??How could we do this “better”?

  18. Linked Lists The Node class Lists as Recursive Structures Displaying a Linked List Inserting and Deleting Elements Implementing the ADT List

  19. List ADT • createList() • isEmpty() • size() • add( index, item) • remove(index) • removeAll() • get(index)

  20. 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”);

  21. We know already that an ArrayList works this way. • What other ways could we satisfy this Abstract implementation?

  22. A Node Data Structure public class Node { private Object item; private Node next; } Beer Wine

  23. Recursive Definition • Milk • Eggs • Butter • Apples • Bread • Chicken A List is the first item, followed by a List

  24. Terminology Head Node Node Node Beer Milk Wine

  25. ListReferenceBased Class public class ListReferenceBased { private Node head; private int numItems; } Beer Milk Wine 3

  26. 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

  27. Linked Lists The Node class Lists as Recursive Structures Displaying a Linked List Inserting and Deleting Elements Implementing the ADT List

  28. public void printList( ) { Node curr = head; while (curr != null ) { System.out.println( curr.getItem() ); curr = curr.getNext(); } } Beer Milk Wine head

  29. Linked Lists The Node class Lists as Recursive Structures Displaying a Linked List Inserting and Deleting Elements Implementing the ADT List

  30. How would we insert and delete an element from an ArrayList??

  31. Deleting the First Node Beer Milk Wine head

  32. Deleting the First Node public void deleteFirstNode() if ( head == null ) // Handle error condition else head = head.getNext(); } Beer Milk Wine head

  33. Inserting at Front Milk Wine head

  34. Inserting at Front public void insertAtFront( Object item ) Node temp = head; head = new Node(); head.setItem( item ); head.setNext( temp ); } Milk Wine head

  35. 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

  36. 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;

  37. Other Types of List • Doubly Linked Lists • Circular Linked Lists • ...more to come later

  38. Doubly Linked Lists Beer Milk Wine head

  39. Circular Linked Lists Beer Milk Wine head

  40. How would we add/remove in this scenario?

More Related