1 / 130

Understanding Linked Lists in C++

Dive into the world of Linked Lists in C++ with Stanford University's CS106B Lecture. Learn about the architecture, nodes, Big O complexity, and implementation of Queues using linked lists.

asmall
Download Presentation

Understanding Linked Lists in C++

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. Programming Abstractions Spring 2017 Stanford University Computer Science Department Lecturer: Chris Gregg reading: Programming Abstractions in C++, Chapter 11 CS 106B Lecture 16: Linked Lists Monday, April 8, 2017

  2. Today's Topics • Logistics • Tiny feedback: • favorite type of dog? Tough choice...probably Corgis. Or Labs. • favorite programming language? Tough choice...depends on the situation! • C++: fast, robust, powerful • C: less to worry about than C++ • Python: so fast to code in, but can be slow • Javascript: need something on the web? • Haskell: so different from regular programming • Piet: ??? • Linked Lists • Could you architect a Queue? • Nodes • Linked Lists • The Towers of Gondor • Do nodes have names? • Big O? • Stack and Queue made from a Linked List

  3. Midterm

  4. Midterm

  5. Your job: Architect a Queue

  6. Easiest Solution... class QueueInt { // in QueueInt.h public: QueueInt (); // constructor void enqueue(int value); // append a value int dequeue(); // return the first-in value private: Vector<int> data; // member variables };

  7. You're next! back front dequeue()

  8. You're next! back front dequeue()

  9. Excuse Me, Coming Through back front

  10. Excuse Me, Coming Through back front enqueue(42)

  11. Excuse Me, Coming Through back front enqueue(42)

  12. Excuse Me, Coming Through back front enqueue(42)

  13. Excuse Me, Coming Through back front enqueue(42)

  14. Excuse Me, Coming Through back front enqueue(42)

  15. Excuse Me, Coming Through back front enqueue(42)

  16. Excuse Me, Coming Through back front enqueue(42)

  17. Excuse Me, Coming Through back front enqueue(42)

  18. Excuse Me, Coming Through back front enqueue(42)

  19. Queue as Vector: Big O • Enqueue: O(n) • Dequeue: O(1)

  20. And Now for Something Completely Different int * data enqueue(7) 9 8

  21. And Now for Something Completely Different int * data 7 enqueue(7) 9 8

  22. And Now for Something Completely Different int * data 7 6 enqueue(6) 9 8

  23. And Now for Something Completely Different int * data 7 6 enqueue(6) 9 8

  24. And Now for Something Completely Different int * data 7 6 enqueue(6) 9 8

  25. And Now for Something Completely Different int * data 7 6 9 8 Now we have a way to add to the front in O(1) time!

  26. Linked List 3 7 6 int * data • A linked list is a chain of nodes. • Each node contains two pieces of information: • Some piece of data that is stored in the sequence • A link to the next node in the list. • We can traverse the list by starting at the first cell and repeatedly following its link.

  27. Linked Lists 1 2 3 • A linked list is a data structure for storing a sequence of elements. • Each element is stored separately from the rest. • The elements are then chained together into a sequence.

  28. Linked Lists 1 2 3 • A linked list is a data structure for storing a sequence of elements. • Each element is stored separately from the rest. • The elements are then chained together into a sequence.

  29. Linked Lists 4 1 2 3 • A linked list is a data structure for storing a sequence of elements. • Each element is stored separately from the rest. • The elements are then chained together into a sequence.

  30. Linked Lists 4 1 2 3 • A linked list is a data structure for storing a sequence of elements. • Each element is stored separately from the rest. • The elements are then chained together into a sequence.

  31. Linked Lists 3 4 1 2 • A linked list is a data structure for storing a sequence of elements. • Each element is stored separately from the rest. • The elements are then chained together into a sequence.

  32. Linked Lists 137 3 4 1 2 • A linked list is a data structure for storing a sequence of elements. • Each element is stored separately from the rest. • The elements are then chained together into a sequence.

  33. Linked Lists 137 3 4 1 • A linked list is a data structure for storing a sequence of elements. • Each element is stored separately from the rest. • The elements are then chained together into a sequence.

  34. Why Linked Lists? • Can efficiently splice new elements into the list or remove existing elements anywhere in the list. • Never have to do a massive copy step; • Has some tradeoffs; we'll see this later.

  35. Linked List Structure • For simplicity, let's assume we're building a linked list of strings. • We can represent a node in the linked list as a structure: struct Node { string value; /* ? */ next; }; The structure is defined recursively!

  36. Linked List of Strings • For simplicity, let's assume we're building a linked list of strings. • We can represent a node in the linked list as a structure: struct Node { string value; Node* next; }; The structure is defined recursively!

  37. Linked List of Strings • For simplicity, let's assume we're building a linked list of strings. • We can represent a node in the linked list as a structure: struct Node { string value; Node* next; }; • The structure is defined recursively!

  38. Always! Draw a picture

  39. Lord of the Linked Lists In a scene that was brilliantly captured in Peter Jackson’s film adaptation of The Return of the King, Rohan is alerted to the danger to Gondor by a succession of signal fires moving from mountain top to mountain top. This scene is a perfect illustration of the idea of message passing in a linked list. Minas Tirith Amon Dîn Eilenach Nardol Erelas Min-Rimmon Calenhad Halifirien Rohan

  40. https://www.youtube.com/watch?v=i6LGJ7evrAg

  41. Lord of the Linked Lists Step 1: Make this linked list Santa Clara Palo Alto Redwood City Bayshore San Francisco San Jose Millbrae Mountain View Menlo Park Step 2: Light the fires…. Lighting the fire of San Francisco!

  42. Lord of the Linked Lists struct Tower { string name; /* The name of this tower */ Tower *link; /* Pointer to the next tower */ };

  43. Lord of the Linked Lists // add the first tower Tower * head = new Tower; head->name = “San Jose”; head->link = NULL;

  44. Lord of the Linked Lists // add the first tower Tower * head = new Tower; head->name = “San Jose”; head->link = NULL;

  45. Lord of the Linked Lists // add the first tower Tower * head = new Tower; head->name = “San Jose”; head->link = NULL;

  46. Lord of the Linked Lists // add the first tower Tower * head = new Tower; head->name = “San Jose”; head->link = NULL;

  47. Lord of the Linked Lists

  48. Linked List Trace struct Tower{ string name; Tower *link; }; // main Tower * head = new Tower; head->name = “San Jose”; head->link = NULL; head = createTower(“Santa Clara”, head); head = createTower(“Mountain View”, head); head = createTower(“Palo Alto”, head); head = createTower(“Menlo Park”, head); head = createTower(“Redwood City”, head); head = createTower(“Millbrae”, head); head = createTower(“Bayshore”, head); head = createTower(“San Francisco”, head); Tower *createTower(string name, Tower *link) { Tower *tp = newTower; tp->name = name; tp->link = link; return tp; }

More Related