new and deletepoint object and a pointer, p, that points to it, we
could write:
point* p = new point;
To delete the point that p points to, you
use the usual delete statement, except without the
square brackets.
delete p;
->| Using dereference operator * | Using operator -> (recommended) |
|
|
-> operator. The two code chunks above are equivalent.
new, so you always have to know how many objects you want to
store ahead of time.
ints that data would be an int, in a
linked list of strings that data would be a string,
etc)

LIST" in the picture. From there you simply
follow the links (arrows) from one piece of data to the next.
Node.
ints
in our list, then a Node will consist of a int,
which we might call "data".
Node - i.e. we need a member of type Node*.
struct Node
{
int data;
Node* next;
};
next field of the last element in the
list is set to the value NULL (which is just defined to be
0) to indicate that it's not pointing to anything. In particular, consider the
following code:
if( 0 == NULL )
cout << "equal" << endl;
else
cout << "not equal" << endl;
The program will output equal. We often call NULL a "null
pointer".
Now, we might have a variable of type Node* called
LIST:
Initially LIST points to NULL, which means the list contains no nodes.
|
|
LIST then points to the first node in our linked
list of integers. Our list might look something like this:
cout << LIST->data;
which says "print the data field of the
Node pointed to by LIST". You can
simply follow that in the picture, it prints out 87.
LIST, which points to the first
node in the list, we need a pointer to the second node in the list. Then we
could write:
cout << (pointer to the second node)->data;
Now, if you look back at the picture, you see that there is a
pointer to the second node in the list, it is the
next field from the first node in the list, i.e.
LIST->next is a pointer to the second node in the
list. So, to print out the second node in the list you write:
cout << (LIST->next)->data; // Drag your mouse and see if the answer matches your guessActually you don't need the parentheses, because the
-> is left associative.
cout << LIST->next->next->data;
Node
data and next fields
LIST to point to it, since it's supposed to be the
new "first node in the list".
| Initially we have: | ![]() |
| diagram |
| diagram |
| diagram |
| diagram |
| At the end we have: | ![]() |
val to the
front of a
linked list LIST, the same sequence of steps will
do the job:
Node* temp = new Node;
temp->data = val;
temp->next = LIST;
LIST = temp;
When speaking of lists, it's really handy to have the concept of an
empty list, i.e. a list of zero nodes.
Recall: Representing an empty listRecall that in our programs, we represent an empty list by a null pointer, i.e. with a pointer whose value is NULL.The same code works!The sequence of steps we used to add a new element to the front of a list works perfectly well even when the list is empty. In the example below,
|
|
add2front() function // WARNING! THIS VERSION DOESN'T WORK!!
void add2front(int val, Node* oldlist)
{
Node* temp = new Node; // Make a new node
temp->data = val; // Set the node’s value field
temp->next = oldlist; // Set the node’s next to the beginning of the oldlist
oldlist = temp; // Set the beginning of the list to our new node
}
So what’s the problem? Well everything here is correct except for the last step, where we want to “set the beginning of the list to our new node”. The issue is that in C, function arguments are a copy of the original using pass by value.
In this case, the oldlist argument itself is just a copy
of the original LIST pointer that might be inside our
main. So changing the value of oldlist on the last
line of the function just changes the copied pointer, not the original pointer
inside main or wherever this function was called from!
// correct version: returns a pointer to the new list
Node* add2front(int val, Node* oldlist)
{
Node* temp = new Node;
temp->data = val;
temp->next = oldlist;
return temp; // return the new list!
}
add2front()Now in your program you have to use this return value! Typically, if you have your linked list defined as
Node* LIST;
inside main, then you would write
LIST = add2front(3, LIST);
to add a new node with value 3 to the front of LIST. Notice that
the pointer variable LIST shows up in two places in that line of
code - once as the argument to the function (the “old list”), and once as the
left hand side getting the new front of the list assigned to it. This is a very
common pattern in functions that manipulate linked lists, so learn to love
it!
Armed with thie add2front function, the very tedious example
above of making a linked list with 4 numbers can be done much, much more
simply:
Which results in the picture on the right: |
|
Notice: we added the elements to the list in reverse order from how they ended up! Since we are always adding to the front of the list, the last thing we add has to be the first thing in the list.
add2front function.
data fields of
the nodes in a linked list are pretty much irrelevant to the
problem of how to manipulate a list. Therefore, we're going
to concentrate on basic linked list manipulations, which
provide us with a wealth of interesting problems. These
typically boil down to functions you'd like to define, like: