1 / 34

Linked Lists

Linked Lists. Just another data structure Dynamic Grows and shrinks as necessary A “perfect fit” in memory Allocate memory when we Add() A little bit more code “Slow” access to data. Nodes. Each node has a piece/set of data. Nodes. Each node has a piece/set of data. head. 75. Nodes.

coty
Download Presentation

Linked Lists

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. Linked Lists • Just another data structure • Dynamic • Grows and shrinks as necessary • A “perfect fit” in memory • Allocate memory when we Add() • A little bit more code • “Slow” access to data

  2. Nodes • Each node has a piece/set of data

  3. Nodes • Each node has a piece/set of data head 75

  4. Nodes • Each node has a piece/set of data • Each node has a reference to the “next” thing in the list head 75 “next” is null – meaning “end of list”

  5. Code class Node { int data; Node next;} head 75

  6. Code class Node { int data; Node next; // Is this recursive? Not really…} head 75

  7. Adding Nodes We can add to the end of the list… head 75

  8. Adding Nodes We can add to the end of the list… head 75 21

  9. Adding Nodes We can add to the end of the list… head 75 21 99

  10. Adding Nodes … we can add to the beginning… head 75

  11. Adding Nodes … we can add to the beginning… head 75 temp 13

  12. Adding Nodes … we can add to the beginning… head 75 temp 13

  13. Adding Nodes … we can add to the beginning… head 75 temp 13

  14. Adding Nodes … we can add to the beginning… head 75 13

  15. Adding Nodes … we can add to the beginning… head 75 13

  16. Adding Nodes … or we can add to the middle head 21 75 99

  17. Adding Nodes … or we can add to the middle We want to add 80 head 21 75 99

  18. Adding Nodes … or we can add to the middle Special case: Is 80 < 21? If so, add to the beginning! head 21 75 99

  19. Adding Nodes … or we can add to the middle Look at this node’s next pointer. If so, add to the beginning! head 21 75 99

  20. Adding Nodes … or we can add to the middle Is 80 < 75? Nope… head 21 75 99

  21. Adding Nodes … or we can add to the middle So update the current node and look at the next pointer head 21 75 99

  22. Adding Nodes … or we can add to the middle Is 80 < 99? Yes! head 21 75 99

  23. Adding Nodes … or we can add to the middle Create a temp node for the 80 head 21 75 99 temp 80

  24. Adding Nodes … or we can add to the middle Have temp.next point to the 99 head 21 75 99 temp 80

  25. Adding Nodes … or we can add to the middle Have the 75 node point to the 80 head 21 75 99 temp 80

  26. The Importance of Order What if instead… Have the 75 node’s next pointer point to the 80 head 21 75 99 temp 80

  27. The Importance of Order What if instead… Create a temp node for the 80 head 21 75 99 temp 80

  28. The Importance of Order What if instead… Have temp’s next pointer point to the 75’s next node? head 21 75 99 temp 80

  29. The Importance of Order What if instead… Have temp’s next node point to the 75’s next node? head 21 75 99 temp 80

  30. The Importance of Order What if instead… We’ve lost the rest of our list! head 21 75 99 temp 80

  31. Why? • We only have access to one variable • Traversing from beginning to end head 21 75 99 temp 80

  32. One Other Note head 21 75 80 99 101

  33. One Other Note 75 101 80 head 21 99

  34. Summary • Dynamic • Grows and shrinks as necessary • A “perfect fit” in memory • Insert • Beginning/end/middle • Order is important • “Slow” access to information because of traversal

More Related