You are on page 1of 21

Data Structures & Algorithms

Chapter # 5
LINKED LISTS

By:
Muhammad Imran
Assistant Prof. of Computer Science
Govt. Millat Degree College Mumtazabad, Multan

Lecturer (Visiting Faculty) of I.T


University of Education (Multan Campus) Multan
Ch # 5 (Linked Lists) Page # 2

Linked Lists

Linked lists and arrays are similar since they both store collections of data. Array is the
most common data structure used to store collections of elements. Arrays are convenient
to declare and provide the easy syntax to access any element by its index number. Once
the array is set up, access to any element is convenient and fast. The disadvantages of
arrays are:

• The size of the array is fixed. Most often this size is specified at compile time. This
makes the programmers to allocate arrays, which seems "large enough" than required.
• Inserting new elements at the front is potentially expensive because existing elements
need to be shifted over to make room.

Linked lists have their own strengths and weaknesses, but they happen to be strong where
arrays are weak. Generally array's allocates the memory for all its elements in one block
whereas linked lists use an entirely different strategy. Linked lists allocate memory for
each element separately and only when necessary.
Here is a quick review of the terminology and rules of pointers. The linked list code will
depend on the following functions:

Linked List Concepts:


A linked list is a non-sequential collection of data items. It is a dynamic data structure.
Linked List consists of group of nodes in a sequence.

Each node holds its own data and the address of the next node hence forming a chain
like structure. Linked Lists are used to create trees and graphs.

The data items in the linked list are not in consecutive memory locations. They may be
anywhere, but the accessing of these data items is easier as each data item contains the
address of the next data item.

Advantages of linked lists:


Linked lists have many advantages. Some of the very important advantages are:
Prepared By: Muhammad Imran
Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 3

1. Linked lists are dynamic data structures. i.e., they can grow or shrink during the
execution of a program.

2. Linked lists have efficient memory utilization. Here, memory is not pre-allocated.
Memory is allocated whenever it is required and it is de-allocated (removed) when it is
no longer needed.

3. Insertion and Deletions are easier and efficient. Linked lists provide flexibility in
inserting a data item at a specified position and deletion of the data item from the
given position.
4. Many complex applications can be easily carried out with linked lists.

Disadvantages of linked lists:

1. The memory is wasted as pointers require extra memory for storage.

2. No element can be accessed randomly; it has to access each node sequentially.

3. Reverse Traversing is difficult in linked list.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 4

Applications of linked list:


1. Linked lists are used to represent and manipulate polynomial. Polynomials are
expression containing terms with non zero coefficient and exponents. For example:
P(x) = a0 Xn + a1 Xn-1 + …… + an-1 X + an
2. Represent very large numbers and operations of the large number such as addition,
multiplication and division.
3. Linked lists are to implement stack, queue, trees and graphs.
4. Implement the symbol table in compiler construction

Types of Linked Lists:


Basically we can put linked lists into the following four items:

1. Single Linked List.


Prepared By: Muhammad Imran
Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 5

2. Double Linked List.

3. Circular Linked List.

Single Linked List:

A linked list allocates space for each element separately in its own block of memory
called a "node". The list gets an overall structure by using pointers to connect all its
nodes together like the links in a chain. Each node contains two fields; a "data" field to
store whatever element, and a "next" field which is a pointer used to link to the next node.

The basic operations in a single linked list are:


• Creation.
• Insertion.
• Deletion.
• Traversing.

Algorithm for Creating a Singly Linked List with ‘n’ number of nodes:
The following steps are to be followed to create ‘n’ number of nodes:
1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty, assign new node as start.
start = newnode;
3. If the list is not empty, follow the steps given below:
4. The next field of the new node is made to point the first node (i.e. start node) in the list
by assigning the address of the first node.
5. The start pointer is made to point the new node by assigning the address of the new
Prepared By: Muhammad Imran
Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 6

node.
6. Repeat the above steps ‘n’ times.

Insertion of a Node:

One of the most primitive operations that can be done in a singly linked list is the
insertion of a node. Memory is to be allocated for the new node (in a similar way that is
done while creating a list) before reading the data. The new node will contain empty data
field and empty next field. The data field of the new node is then stored with the
information read from the user. The next field of the new node is assigned to NULL. The
new node can then be inserted at three different places namely:

• Inserting a node at the beginning.


• Inserting a node at the end.
• Inserting a node at intermediate position.

Algorithm for Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the list:
1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty then start = newnode.
3. If the list is not empty, follow the steps given below:
newnode -> next = start;
4. start = newnode;

Algorithm for Inserting a node at the end:

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 7

The following steps are followed to insert a new node at the end of the list:
1. Get the new node using getnode()
newnode = getnode();
2. If the list is empty then start = newnode.
3. If the list is not empty follow the steps given below:
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;

Algorithm for Inserting a node at intermediate position:

The following steps are followed, to insert a new node in an intermediate position in the
list:
1. Get the new node using getnode().
newnode = getnode();
2. Ensure that the specified position is in between first node and last node. If
3. not, specified position is invalid. This is done by countnode() function. Store the
starting address (which is in start pointer) in temp and prev pointers. Then traverse
the temp pointer upto the specified position followed by prev pointer.
4. After reaching the specified position, follow the steps given below:
prev -> next = newnode;
newnode -> next = temp;
5. Let the intermediate position be 3.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 8

Deletion of a node:

Another primitive operation that can be done in a singly linked list is the deletion of a
node. Memory is to be released for the node to be deleted. A node can be deleted from
the list from three different places namely.
• Deleting a node at the beginning.
• Deleting a node at the end.
• Deleting a node at intermediate position.

Algorithm for Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:
1. If list is empty then display ‘Empty List’ message.
2. If the list is not empty, follow the steps given below:
temp = start;
start = start -> next;
free(temp);

Algorithm for Deleting a node at the end:

The following steps are followed to delete a node at the end of the list:
1. If list is empty then display ‘Empty List’ message.
Prepared By: Muhammad Imran
Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 9

2. If the list is not empty, follow the steps given below:


temp = prev = start;
while(temp -> next != NULL)
{
prev=temp;
temp = temp -> next;
}
prev -> next = NULL;
free(temp);

Algorithm for Deleting a node at Intermediate position:


The following steps are followed, to delete a node from an intermediate position in the
list (List must contain more than two node).
1. If list is empty then display ‘Empty List’ message
2. If the list is not empty, follow the steps given below.
if(pos > 1 && pos < nodectr)
{
temp = prev = start;
ctr=1;
while(ctr<pos)
{
prev=temp;
temp=temp->next;
ctr++;
}
prev -> next = temp -> next;
free(temp);
printf("\n node deleted..");
}

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 10

Algorithm For the Traversal / displaying a list (Left to Right):

Algorithm For Counting the Number of Nodes:

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 11

What is Double Linked List?


In a single linked list, every node has a link to its next node in the sequence. So, we
can traverse from one node to another node only in one direction and we can not
traverse back. We can solve this kind of problem by using a double linked list. A
double linked list can be defined as follows...

Double linked list is a sequence of elements in which every element has links to its
previous element and next element in the sequence.

In a double linked list, every node has a link to its previous node and
next node. So, we can traverse forward by using the next field and can
traverse backward by using the previous field. Every node in a double
linked list contains three fields and they are shown in the following
figure...

Here, 'link1' field is used to store the address of the previous node in the
sequence, 'link2' field is used to store the address of the next node in the
sequence and 'data' field is used to store the actual value of that node.
Example

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 12

Operations on Double Linked List


In a double linked list, we perform the following operations...

Insertion
Deletion
Display / Traversing

Insertion
In a double linked list, the insertion operation can be performed in three
ways as follows...

Inserting At Beginning of the list


Inserting At End of the list
Inserting At Specific location in the list
Inserting At Beginning of the list

We can use the following steps to insert a new node at beginning of


the double linked list...

Step 1 - Create a newNode with given value and newNode → previous


as NULL.
Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then, assign NULL to newNode → next and


newNode to head.
Step 4 - If it is not Empty then, assign head to newNode → next and
newNode to head.
Prepared By: Muhammad Imran
Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 13

Inserting At End of the list

We can use the following steps to insert a new node at end of the
double linked list...

Step 1 - Create a newNode with given value and newNode → next as


NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty, then assign NULL to newNode → previous and
newNode to head.
Step 4 - If it is not Empty, then, define a node pointer temp and initialize
with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last
node in the list (until temp → next is equal to NULL).
Step 6 - Assign newNode to temp → next and temp to newNode →
previous.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in
the double linked list...

Step 1 - Create a newNode with given value.


Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to both newNode → previous
& newNode → next and set newNode to head.

Step 4 - If it is not Empty then, define two node pointers temp1 & temp2
and initialize temp1 with head.
Step 5 - Keep moving the temp1 to its next node until it reaches to the
node after which we want to insert the newNode (until temp1 → data is

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 14

equal to location, here location is the node value after which we want to
insert the newNode).
Step 6 - Every time check whether temp1 is reached to the last node. If it
is reached to the last node then display 'Given node is not found in the
list!!! Insertion not possible!!!' and terminate the function. Otherwise
move the temp1 to next node.
Step 7 - Assign temp1 → next to temp2, newNode to temp1 → next,
temp1 to newNode → previous, temp2 to newNode → next and
newNode to temp2 → previous.

Deletion

In a double linked list, the deletion operation can be performed in three


ways as follows...

Deleting from Beginning of the list


Deleting from End of the list
Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of
the double linked list...

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and
initialize with head.
Step 4 - Check whether list is having only one node (temp → previous is
equal to temp → next)
Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting
Empty list conditions)

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 15

Step 6 - If it is FALSE, then assign temp → next to head, NULL to head


→ previous and delete temp.
Deleting from End of the list

We can use the following steps to delete a node from end of the
double linked list...

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and
initialize with head.
Step 4 - Check whether list has only one Node (temp → previous and
temp → next both are NULL)
Step 5 - If it is TRUE, then assign NULL to head and delete temp. And
terminate from the function. (Setting Empty list condition)
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last
node in the list. (until temp → next is equal to NULL)
Step 7 - Assign NULL to temp → previous → next and delete temp.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the
double linked list...

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and
initialize with head.
Step 4 - Keep moving the temp until it reaches to the exact node to be
deleted or to the last node.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 16

Step 5 - If it is reached to the last node, then display 'Given node not
found in the list! Deletion not possible!!!' and terminate the fuction.
Step 6 - If it is reached to the exact node which we want to delete, then
check whether list is having only one node or not
Step 7 - If list has only one node and that is the node which is to be
deleted then set head to NULL and delete temp (free(temp)).
Step 8 - If list contains multiple nodes, then check whether temp is the
first node in the list (temp == head).
Step 9 - If temp is the first node, then move the head to the next node
(head = head → next), set head of previous to NULL (head → previous
= NULL) and delete temp.
Step 10 - If temp is not the first node, then check whether it is the last
node in the list (temp → next == NULL).
Step 11 - If temp is the last node then set temp of previous of next to
NULL (temp → previous → next = NULL) and delete temp
(free(temp)).
Step 12 - If temp is not the first node and not the last node, then set temp
of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next →
previous = temp → previous) and delete temp (free(temp)).

Displaying a Double Linked List


We can use the following steps to display the elements of a double
linked list...

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the
function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and
initialize with head.
Step 4 - Display 'NULL <--- '.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 17

Step 5 - Keep displaying temp → data with an arrow (<===>) until temp
reaches to the last node
Step 6 - Finally, display temp → data with arrow pointing to NULL
(temp → data ---> NULL).
Example

Traversal

Traversal is a technique of visiting each node in the linked list. In a


doubly linked list, we have two types of traversals as we have two
pointers with different directions in the doubly linked list.

Forward traversal – Traversal is done using the next pointer which is


in the forward direction.
Backward traversal – Traversal is done using the previous pointer
which is the backward direction.

Circular Single Linked List:


It is just a single linked list in which the link field of the last node points back to the
address of the first node. A circular linked list has no beginning and no end. It is
necessary to establish a special pointer called start pointer always pointing to the first
node of the list. Circular linked lists are frequently used instead of ordinary linked list
because many operations are much easier to implement. In circular linked list no null
pointers are used, hence all pointers contain valid address.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 18

The basic operations in a circular single linked list are:


• Creation.
• Insertion.
• Deletion.
• Traversing.

Creating a circular single Linked List with ‘n’ number of nodes:

The following steps are to be followed to create ‘n’ number of nodes:


1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty, assign new node as start.
start = newnode;
3. If the list is not empty, follow the steps given below:
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
4. Repeat the above steps ‘n’ times.
5. newnode -> next = start;
Algorithm for Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the
circular list:
1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty, assign new node as start.
start = newnode;
newnode -> next = start;
3. If the list is not empty, follow the steps given below:
last = start;
while(last -> next != start)
last= last -> next;
newnode -> next = start;
start = newnode;
last -> next = start;

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 19

Algorithm for Inserting a node at the end:


The following steps are followed to insert a new node at the end of the list:
1. Get the new node using getnode().
newnode = getnode();
2. If the list is empty, assign new node as start.
start = newnode;
newnode -> next = start;
3. If the list is not empty follow the steps given below:
temp = start;
while(temp -> next != start)
temp = temp -> next;
temp -> next = newnode;
newnode -> next = start;

Algorithm for Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:
1. If the list is empty, display a message ‘Empty List’.
2. If the list is not empty, follow the steps given below:

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 20

last = temp = start;


while(last -> next != start)
last= last -> next;
start = start -> next;
last -> next = start;
3. After deleting the node, if the list is empty then start = NULL.

Algorithm for Deleting a node at the end:


The following steps are followed to delete a node at the end of the list:
1. If the list is empty, display a message ‘Empty List’.
2. If the list is not empty, follow the steps given below:
temp = start;
prev = start;
while(temp -> next != start)
{
prev=temp;
temp = temp -> next;
}
prev -> next = start;
3. After deleting the node, if the list is empty then start = NULL.

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.
Ch # 5 (Linked Lists) Page # 21

Algorithm for Traversing a circular single linked list from left to right:

The following steps are followed, to traverse a list from left to right:
1. If list is empty then display ‘Empty List’ message.
2. If the list is not empty, follow the steps given below:
temp = start;
do
{
printf("%d", temp -> data);
temp = temp -> next;
} while(temp != start);

Prepared By: Muhammad Imran


Assistant Prof. of Computer Science, Govt. Millat Degree College,
Multan.
Lecturer (Visiting Faculty) of I.T, University of Education Multan Campus.

You might also like