Chapter10 - Chapter 10 Linked Lists The diagram below...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10 – Linked Lists The diagram below illustrates the basic form of a linked list. In the C++ world, the elements of a linked list are structures (objects), the head and next pointers are pointer variables, and “NULL” is used as the special “no more elements” value. head next data next data next data the “head” pointer tells us where to find the first list element each list element contains some data and also tells us where we can find the next list element in the case of the last list element, some special value is used to mean “no more”
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
For simplicity, we will begin by considering a list of “int” values. In this case, list elements (hereafter called “nodes”) are as follows… struct LNode { int data; LNode *next; }; and the head pointer is a pointer (initially NULL, meaning that the list is completely empty) to a node... LNode *head = NULL; The following code reads in values (until -1 is entered) and inserts each value entered at the beginning of the list. It assumes the above declarations. Keep in mind that “n -> data is equivalent to “(*n).data”. The value is stored into the “data” component of the structure pointed to by “n”. int value; LNode *n; // “n” for “new” for (;;) { cout << “enter a value (-1 to end): ”; cin >> value; if (value == -1) { break; } n = new LNode (); // create a new node n -> data = value; // place the data in the node // insert new node into the list (at the beginning) n -> next = head; // make the node refer to the first // existing list element head = n; // make the head pointer refer to the // newly created node } This particular code always adds new values at the beginning of the list. This isn’t the only option (e.g. we could instead keep the list sorted) but the alternatives are a little more complex. If the way the code operates isn't obvious, try taking a piece of paper and acting out the code for three or so values.
Image of page 2
Insertions can be made more convenient by defining a node constructor. When this is done, we go from having a “struct” to a “class” (recall that, while the two are essentially equivalent, we use “struct” only in the simplest cases). class LNode { public: int data; LNode *next; LNode (int data, LNode * next) { // constructor this -> data = data; this -> next = next; } }; Given this constructor, only one line of code is necessary to create a new node and insert it at the start of a list. // create new node and insert it at the start of the list head = new LNode (value, head); Having built a list, we might want to print out all of the values in it. The following code does this. LNode *c; // c for “current” c = head; // while we’ve more nodes to visit while (c != NULL) { // output the value on the current node cout << c -> data << endl; // and advance to the next one c = c -> next; // advance to next node } Note that this code will function correctly (output nothing) if the list is empty (if the head pointer is null). The process of proceeding through a list node by node (as this code does) is called a “list traversal”. The process can be very neatly implemented with a “for” loop, as shown below.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

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