1 / 38

Question of the Day

Question of the Day. While walking across a bridge I saw a boat filled with people . Nobody boarded or left the boat, but on board the boat there was not a single person . How is this possible?. Question of the Day.

candie
Download Presentation

Question of the Day

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. Question of the Day While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not a single person. How is this possible?

  2. Question of the Day While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not a singleperson. How is this possible? Everybody on the boat was married.

  3. CSC 212 – Data Structures Lecture 21:Arrays versus Linked-lists

  4. How Could We Do This? • Already know simple way to hold data:array • Primitives or references can be stored in entries • Multi-dimensional arrays organize into tables (or more) • Arrays of generic type can limit code rewriting public class ArrayStack<T> {private inttop;private T[] Stack;public ArrayStack(){ s = (T[]) new Object[100]; }public void push(Telem) {stack[top] = elem; top += 1;}public T pop() { ... return stack[top]; }// More code goes here, but I’m out of space!

  5. Why Not Arrays? • Many limitations arise when using arrays • Must specify unchangeable size when createdPirate[] rum = new Pirate[1];Pirate[] h2o = new Pirate[variable];rum = new Pirate[60]; // old rum was lost!h2o = rum; // h20 now alias to rum’s instance • Waste memory requiring maximum size for array// Each Amazon.com customer uses 400+MB of RAM!Rating[] hertz = new Rating[100000000];

  6. Arrays • Provide simple way of storing & accessing data • When needed, moving data around is difficult • Cannot resize an array without copying entire thing • If array allocated too small, then program crashes • Waste memory if too large an array is allocated • But access times are really, really fast

  7. Arrays • Provide simple way of storing & accessing data • When needed, moving data around is difficult • Cannot resize an array without copying entire thing • If array allocated too small, then program crashes • Waste memory if too large an array is allocated • But access times are really, really fast

  8. When Memory Is Too Large

  9. Better Option • Linked lists adjust size to reflect current needs • Implementation that avoids array’s sizing problems • First recursivestructure you will use this year • There exist many linked list implementations • Best depends on situation and how it will be used • Each approach has own strengths & weaknesses

  10. Better Option • Linked lists adjust size to reflect current needs • Implementation that avoids array’s sizing problems • First recursivestructure you will use this year • There exist many linked list implementations • Best depends on situation and how it will be used • Each approach has own strengths & weaknesses • Recurring refrain: best determined by situation • Understanding each implementation very important

  11. Singly Linked List LinearNode elem next • Linked lists are linear sequence of nodes • “Thingy” is definition of “Node” • Each LinearNode contains: • Element reference to data stored in LinearNode • Link to next LinearNode in linked list

  12. Singly Linked List LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next • Linked lists are linear sequence of nodes • “Thingy” is definition of “Node” • Each LinearNode contains: • Element reference to data stored in LinearNode • Link to next LinearNode in linked list

  13. 1st Node Class (of many) public class LinearNode<T> {private Telement;private LinearNode<T> next;public LinearNode(Te, LinearNode<T> n) {element = e;next= n;}public TgetElement() { return element;}public LinearNode<T> getNext() { return next;}public void setElement(TnewE) {element = newE;}public void setNext(LinearNode<T> newNext) {next = newNext;} }

  14. 1st Node Class (of many) public class LinearNode<T> {private Telement;private LinearNode<T> next;public LinearNode(Te, LinearNode<T> n) {element = e;next = n;}public TgetElement() { return element;}public LinearNode<T> getNext() { return next;}public void setElement(TnewE) {element = newE;}public void setNext(LinearNode<T> newNext) {next = newNext;} }

  15. Singly Linked List public class SLinkedList<T> {private LinearNode<T> head;private intsize;public SLinkedList() {head = null; // Make an empty listsize = 0;}public booleanisEmpty() { return (head == null);}public TgetFirstElement() {// Handle situation when list is empty return head.getElem();} }

  16. Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size  3 LinearNode LinearNode LinearNode elem elem elem next next next elem

  17. Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem

  18. Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem

  19. Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem

  20. Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem

  21. Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size  4 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next n elem

  22. Inserting at Head Algorithm addFirst(elem)LinearNode<T> n = new LinearNode<T>();n.setElement(elem);n.setNext(head);head = nsize += 1 SLinkedList head size  4 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next

  23. Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all

  24. Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size  4 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next

  25. Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next

  26. Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next

  27. Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next

  28. Removing an Internal Node • Linked list's length equals number of elements • Requires unlinking LinearNodefrom linked list • Nothing fancy needed, just adjust previous next link • LinearNode 0nly existed via links, so this does it all SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next

  29. Removing the Head

  30. Removing the Head

  31. Removing the Head • Resized with each addition or removal • Linked list's headnode has nothing to unlink • No previous node whose nextfield to be updated • Instead, just need to advance where head refershead = head.getNext(); SLinkedList head size  3 LinearNode LinearNode LinearNode elem elem elem next next next

  32. Removing the Head • Resized with each addition or removal • Linked list's headnode has nothing to unlink • No previous node whose nextfield to be updated • Instead, just need to advance where head refershead = head.getNext(); SLinkedList head size  3 LinearNode LinearNode LinearNode elem elem elem next next next

  33. Removing the Head • Resized with each addition or removal • Linked list's headnode has nothing to unlink • No previous node whose nextfield to be updated • Instead, just need to advance where head refershead = head.getNext(); SLinkedList head size  2 LinearNode LinearNode LinearNode elem elem elem next next next

  34. Doubly Linked List DNode Instance prev elem next • Link to previous node in list also in each node • Each DNode contains: • Element (data) reference • Link to nextDNode • Prev(ious) DNodealso linked

  35. Doubly Linked List Sequence of 4 DNodes • Link to previous node in list also in each node • Each DNode contains: • Element (data) reference • Link to next DNode • Prev(ious) DNodealso linked

  36. Doubly Linked List • DNodecould extend LinearNode • next & elem fields are needed by both classes • Only difference is prev field added by DNode • DLinkedListnot subclass of SLinkedList • Both classes define identical methods… • …are entirely different when implemented 1

  37. Your Turn • Get into your groups and complete activity SLinkedList head size  3 LinearNode LinearNode LinearNode LinearNode elem elem elem elem next next next next

  38. For Next Lecture • Read 4.6 (& web article) for Friday • How can we use linked lists for Stacks? • When would we want to use linked list-based Stack? • When would linked list-based Stack suck? • Why use singly- vs. doubly-linked lists? • Angel also has Weekly Assignment #8 • Pulls everything together and shows off your stuff • Keep planning out on your classes for Project #1 • Due week from Sat. and requires actual thought

More Related