Linkedlist int main linkedlist mylist code to add

This preview shows page 39 - 45 out of 82 pages.

LinkedList() { … } int main() { } LinkedList myList; // code to add nodes myList.printItems(); How do we go about printing the items in the list?
Image of page 39

Subscribe to view the full document.

head 40 2000 Printing the Items in a Linked List class LinkedList { public: private: }; Node *head; int main() { } LinkedList myList; // code to add nodes myList.printItems(); void printItems() { } value next 2000 value next 1200 value next 3700 "toast" "bacon" "eggs" nullptr 1200 3700 So let’s assume we’ve used our class to create a linked list and add some items… How do we go about printing the items in the list?
Image of page 40
Printing the Items in a Linked List p head 2000 2000 class LinkedList { public: private: }; Node *head; { } void printItems() OK, so our goal is to loop through each of the nodes and print out their values, starting with the node pointed to by “head”… 41 Node *p; p = head; // p points to 1 st nod while ( ) { } p points to a valid node print the value in the node p = address of the next node value next 2000 value next 1200 value next 3700 "toast" "bacon" "eggs" nullptr 1200 3700
Image of page 41

Subscribe to view the full document.

Printing the Items in a Linked List p head 2000 class LinkedList { public: private: }; Node *head; { } void printItems() OK, so our goal is to loop through each of the nodes and print out their values, starting with the node pointed to by “head”… 42 Node *p; p = head; // p points to 1 st nod while ( { } p points to a valid node p = address of the next node value next 2000 value next 1200 value next 3700 "toast" "bacon" "eggs" nullptr 1200 3700 2000 ) cout << p->value << endl; p->next; Be careful! You can’t use p++ to move forward in a linked list! You must use the next pointer !
Image of page 42
Printing the Items in a Linked List p head 2000 class LinkedList { public: private: }; Node *head; { } void printItems() 43 Node *p; p = head; // p points to 1 st nod while ( { } p points to a valid node p = address of the next node value next 2000 value next 1200 value next 3700 "toast" "bacon" "eggs" nullptr 1200 3700 2000 cout << p->value << endl; p->next; toast 1200 bacon 3700 eggs nullptr p != nullptr ) And there’s our complete printing loop! Any time we iterate through one or more nodes like this, it’s called a “traversal” . Alright, now let’s learn how to add nodes to our list! When we use the condition: while (p != nullptr ) { … } the loop will process EVERY node in the list and only stop once it’s gone PAST the end of the list. So this answers our question! If p’s value is nullptr , it does NOT point to a valid node. Otherwise it does. This is a linked list traversal!
Image of page 43

Subscribe to view the full document.

44 Adding an Item to a Linked List class LinkedList { public: private: }; Node *head; void addToRear(string v) { … } head 2200 value next 2200 value next 3700 "bacon" "eggs" nullptr 3700 There are three places you can insert a new item into a linked list: at the top of the list at the end of the list somewhere in the middle The algorithm to insert at the top is the easiest to code and also runs the fastest . Let’s see this one first, and add some “OJ” to the top of our list! Allocate a new node void addToFront(string v) { … } Put value v in the node value next 8000 { } Link the new node to the old top node Link the head pointer to our new top node “OJ" 2200 These two steps must be in this order !
Image of page 44
45 Adding an Item to the Front class LinkedList { public: private: }; Node *head; head 2200 value next
Image of page 45
You've reached the end of this preview.
  • Winter '16

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern