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

Info iconThis 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”
Background image of page 1

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

View Full DocumentRight 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.
Background 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
Background image of page 3

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

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

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 13

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online