You are on page 1of 21

Linked Lists

Ref.: D.S. Malik, Data Structures Using C++

Linked Lists
collection of components (called nodes) starts with the head (aka first), which is the address of 1st node each node comprises:
data link

address of next node

Linked Lists
e.g.
45 head 65 34 76

Linked Lists
an address is a memory location e.g.
1200 1575 65 34 76 1200 head 45 1575

Linked Lists
Declaration:
struct nodeType { int info; nodeType *link; };

This defines the following structure:

Linked Lists

info link info link info link *link

*link

*link

Linked Lists
We do not define the name of a linked list. Instead we define the name of a pointer to it. To define a variable as a pointer to a linked list use:
nodeType *head;

This will create this sort of structure:


head *head

Linked Lists
Having got the structure, and given the pointer to it a name, the next thing we do is request the computer to allocate a memory location for the first node. head is set to the value of this memory location This is done as follows: head = new nodeType

Linked Lists
Let us assume that part way through the execution of a program, the linked list looks like this:
2000 2000 head 17 2800 info link 2800 92 info etc.

Linked Lists
The variable head equals 2000. The expression head->info equals 17. The expression head->link equals 2800. The expression head->link->info equals 92.

Linked Lists
When extracting data from a linked list we make use of another pointer. e.g. current = head; current = current->link; Now current points to the 2nd node (whose address is 2800).

Linked Lists
2000 2000 head 17 2800 info link 2800 92 1500 63 3600 45 0

current

2800

Student exercise: Write down the values of the following expressions:


Expression current current->info current->link current->link->info head->link->link head->link->link->info head->link->link->link current->link->link->link 0 (i.e. NULL) Value

current->link->link->link->info

Linked Lists
code to move through the linked list:
current = head; while (current != NULL) { current = current->link; }

Student exercise: Amend the code to output all data in the linked list

Linked Lists
current = head; while (current != NULL) { cout << current->info << ; current = current->link; }

Linked Lists
As we noted earlier, we define the structure of a linked list as follows:
struct nodeType { int info; nodeType *link; };

Linked Lists
Here is an example way of defining 4 pointers (head, p, q, and newNode):
nodeType *head, *p, *q, *newNode;

Each of these could be used to point to a linked list. Let us look at the code for inserting a new node in a linked list.

Linked Lists
Consider the following linked list. We want to insert a new third node whose info value is 50.
45 head 65 34 76

First we create a new linked list whose 1st node has an info value of 50. Let us call the pointer to this linked list newNode.

Linked Lists
50

newNode

The code to do this is: newNode = new nodeType; newNode->info = 50; Next we need to adjust the links of head as shown in the next slide.

Linked Lists
45 head 50 newNode 65 34 76

It will simply matters if we use a pointer p.

Linked Lists
45 head p 65 34 76

50
newNode

The code to do this is:


newNode->link = p->link; p->link = newNode;

You might also like