Chapter10

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

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

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”

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

View Full Document
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.
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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern