Chapter 3 - CS
Chapter 3 - CS
Chapter 3
2
Linked Lists
Objective 3
Linked lists
Even though arrays are simple and fast they have their own disadvantages.
Fixed size
Why Linked lists?
General purpose storage data structures and are versatile.
6
More complex to code and manage than arrays, but they have some distinct
advantages.
With a linked list, no need to move other nodes. Only need to reset some
pointers.
Linked Lists 7
A B C
Head
node
A
data pointer
Linked Lists 8
Each Link object contains a reference to the next link in the list of items.
In an array
In a list
A linked list is a data structure that is built from structures and pointers.
It forms a chain of "nodes" with pointers representing the links of the chain and
holding the entire thing together.
First, a little reminder on structure 10
The struct keyword creates a new user defined data type that is used to declare
variables of an aggregate data type.
11
Self Referential structures are those structures that have one or more pointers which
point to the same type of structure, as their member.
Defining the data structure for a linked list 14
The key part of a linked list is a structure, which holds the data for each node, and, most
importantly, a pointer to the next node.
we will assume that the node has to be added to the end of the list, although it
could be added anywhere in the list (a problem we will deal with later on).
Firstly, space for a pointer item is declared and assigned to a temporary pointer.
node *temp;
temp = new node;
16
Having declared the node, we ask the user to fill in the details of the person, i.e.
the name, age, address or whatever:
Having set up the information, we have to decide what to do with the pointers. If
the list is empty to start with, there's no problem - just set the Start pointer to point
to this node.
if (start_ptr == NULL)
start_ptr = temp;
18
It is harder if there are already nodes in the list. In this case, the secret is to declare
a second pointer, temp2, to step through the list until it finds the last node.
temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{ temp2 = temp2->nxt; // Move to next link in chain
}
+ 19
temp2->nxt = temp;
The full code 20
Steps
1. Set a temporary pointer to point to the same thing as the start pointer.
2. If the pointer points to NULL, display the message "End of list" and stop.
3. Otherwise, display the details of the node pointed to by the start pointer.
4. Make the temporary pointer point to the same thing as the nxt pointer of the node it
is currently indicating.
5. Jump back to step 2.
Displaying the list of nodes 23
Navigating is necessary when you want to insert or delete a node from somewhere
inside the list, as you will need to specify the position.
Navigation through the list is done with a pointer that moves backwards and
forwards through the list.
25
First of all, a mobile pointer current is declared, and set to the same value as the
start_ptr pointer:
node *current;
current = start_ptr;
It's easy to get the current pointer to point to the next node in the list
current = current->nxt;
26
It is better to check that current isn't pointing to the last item in the list. If it is,
then there is no next node to move to:
if (current->nxt == NULL)
cout << "You are at the end of the list." << endl;
else
current = current->nxt;
27
Moving the current pointer back one step is a little harder. This is because we
have no way of moving back a step automatically from the current node.
The only way to find the node before the current one is to start at the beginning,
work your way through and stop when the node before the one considered is
found.
28
if (current == start_ptr)
cout << "You are at the start of the list" << endl;
else
{ node *previous; // Declare the pointer
previous = start_ptr;
while (previous->nxt != current)
{ previous = previous->nxt;
}
current = previous;
}
Add a node at a specific position 29
void add_position(int pos) cout << "Please enter the name of the person:
";
{
if(start_ptr!=NULL){ cin >> temp->name;
node *pre; cout << "Please enter the age of the person :
";
node *cur;
cin >> temp->age;
node *temp=new node;
cout << "Please enter the height of the
cur=start_ptr; person : ";
for(int i=1;i<pos;i++) cin >> temp->height;
{ pre->nxt=temp;
pre=cur; temp->nxt=cur;
cur=cur->nxt; }
} }
Deleting a node from the list 30
A node can be deleted from the start of the list, from the end of the list, or from
somewhere in the middle.
When a node is deleted, the space that it took up should be reclaimed. Otherwise
the computer will eventually run out of memory space. This is done with the
delete instruction:
void delete_start_node()
{ node *temp;
we can't just delete the nodes as it would temp = start_ptr;
6 4 17 42
head
6 4 17 42
head
4 17 42
Delete a node from the end 33
Deleting a node from the end of the list is harder, as the temporary pointer must
find where the end of the list is by jumping along from the start.
It is necessary to maintain two temporary pointers, temp1 and temp2. The pointer
temp1 will point to the last node in the list and temp2 will point to previous to the
last node.
Steps 34
1. Look at the start pointer. If it is NULL, then the list is empty, so print out a "No
nodes to delete" message.
2. Make temp1 point to whatever the start pointer is pointing to.
3. If the nxt pointer of what temp1 indicates is NULL, then we've found the last node
of the list, so jump to step 7.
4. Make another pointer, temp2, point to the current node in the list.
5. Make temp1 point to the next item in the list.
6. Go to step 3.
7. If you get this far, then the temporary pointer, temp1, should point to the last item
in the list and the other temporary pointer, temp2, should point to the last-but-one
item.
8. Delete the node pointed to by temp1.
9. Mark the nxt pointer of the node pointed to by temp2 as NULL - it is the new last
node.
35
36
Deletion Description 37
head
6 4 17 42
head
6 4 17 42
head
6 4 17
38
Insertion and deletion at the beginning of the list are very fast, O(1).
Finding, deleting or inserting in the list requires searching through half the items
in the list on an average, requiring O(n) comparisons.
Although arrays require same number of comparisons, the advantage lies in the
fact that no items need to be moved after insertion or deletion.
As opposed to fixed size of arrays, linked lists use exactly as much memory as is
needed and can expand.
40
A doubly linked list is one where there are links from each node in both
directions.
Each node in the list has two pointers, one to the next node and one to the
previous one - again, the ends of the list are defined by NULL pointers.
42
There is no pointer at the start of the list. Instead, there is simply a pointer to some
position in the list that can be moved left or right.
The reason a start pointer is needed in ordinary linked list is because, having
moved on from one node to another, we can't easily move back, so without the
start pointer, we would lose track of all the nodes in the list that we have already
passed.
Creating Doubly Linked Lists 43
struct node{
char name[20];
node *nxt; // Pointer to next node
node *prv; // Pointer to previous node
};
node *current;
Linked List
struct node{ void add_node_at_start () {
}; temp->prev = NULL;
The End