Unit - II Lecture Slides
Unit - II Lecture Slides
Linked lists
Unit – II Syllabus
Let us consider the list of elements 20, 33, 14, 65, 81 needs to store in memory
Array representation
100
102
104
106
108
Linked list processing
Linked list
• 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.
• Deleting an element from an array is not possible
Note: Binary searching is not possible on linked list representation
Linked List is a linear data structure,
1. Single Linked List: A single linked list is one in which all nodes are linked together in some sequential
manner. Hence, it is also called as linear linked list.
2. Double Linked List: A double linked list is one in which all nodes are linked together by multiple links
which helps in accessing both the successor node (next node) and predecessor node (previous
node) from any arbitrary node within the list. Therefore each node in a double linked list has two
link fields (pointers) to point to the left node (previous) and the right node (next). This helps to
traverse in forward direction and backward direction.
3. Circular Linked List: A circular linked list is one, which has no beginning and no end. A single linked
list can be made a circular linked list by simply storing address of the very first node in the link field
of the last node.
4. Circular Double Linked List: A circular double linked list is one, which has both the successor pointer
and predecessor pointer in the circular manner.
Applications of Linked Lists
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.
Before writing the code to build the list, we need to create a start node, used to create and access other
nodes in the linked list.
• Creating a structure with one data item and a next pointer, which will be pointing to next node of the list.
This is called as self-referential structure.
• Initialize the start pointer to be NULL.
The basic operations in a single linked list are:
• Creation.
• Insertion.
• Deletion.
• Traversing.
Creating a node for Single Linked List:
• 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.
• The start pointer is made to point the new node by assigning the address of
the new node.
• 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:
The following steps are to be followed to insert a new node at the beginning of the list:
• Get the new node using getnode().
newnode = getnode(); void insert_at_beg()
• If the list is empty then {
start = newnode. node *newnode;
newnode = getnode();
• If the list is not empty, follow the steps given below: if(start == NULL)
newnode -> next = start; {
start = newnode; start = newnode;
}
else
{
newnode -> next = start;
start = newnode;
}
}
Inserting a node at the end:
The following steps are followed to insert a new node at the end of the list:
• 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.
The following steps are followed, to delete a node at the beginning of the list:
temp = start;
start = start -> next;
free(temp);
Deleting a node at the end:
The following steps are followed to delete a node at the end of the list:
• If list is empty then display ‘Empty List’ message.
• 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);
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).
• If list is empty then display ‘Empty List’ message
• 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..");
}
Traversal and displaying a list (Left to Right):
To display the information, you have to traverse (move) a linked list, node by node from the first node,
until the end of the list is reached.
Traversing a list involves the following steps:
A header node is a special dummy node found at the front of the list.
The use of header node is an alternative to remove the first node in a list.
For example, the picture below shows how the list with data 10, 20 and 30 would be represented using a linked list
without and with a header node:
header
Note that if you do decide to use a header node, you must remember to initialize an empty list to
contain one (dummy) node, you must remember not to include the header node in the count of "real"
nodes in the list.
It is also useful when information other than that found in each node of the list is needed.
For example, imagine an application in which the number of items in a list is often calculated.
In a standard linked list, the list function to count the number of nodes has to traverse the entire list every time.
However, if the current length is maintained in a header node, that information can be obtained very quickly.
Array based linked lists:
Another alternative is to allocate the nodes in blocks.
In fact, if you know the maximum size of a list a head of time, you can pre-allocate the nodes in a single array.
The result is a hybrid structure – an array based linked list.
Figure below shows an example of null terminated single linked list where all the nodes are allocated
contiguously in an array.
Circular Singly Linked List
• In a circular Singly linked list, the last node of the list contains a
pointer to the first node of the list. We can have circular singly linked
list as well as circular doubly linked list.
• We traverse a circular singly linked list until we reach the same node
where we started. The circular singly liked list has no beginning and
no ending. There is no null value present in the next part of any of the
nodes.
Circular linked list are mostly used in task maintenance in operating systems.
There are many examples where circular linked list are being used in computer science
including browser surfing where a record of pages visited in the past by the user, is
maintained in the form of circular linked lists and can be accessed again on clicking the
Creating a circular single Linked List with ‘n’ number of nodes:
A double linked list is a two-way list in which all nodes will have two links. This
helps in accessing both successor node and predecessor node from the given node
position.
It provides bi-directional traversing.
Each node contains three fields:
• Left link.
• Data.
• Right link.
The left link points to the predecessor node and the right link points to the
successor node. The data field stores the required data.
Many applications require searching forward and backward thru nodes of a list.
For example searching for a name in a telephone directory would need forward
and backward scanning thru a region of the whole list.
The basic operations in a double linked list are:
• Creation.
• Insertion.
• Deletion.
• Traversing
The beginning of the double linked list is stored in a "start" pointer which points to the
first node.
The first node’s left link and last node’s right link is set to NULL.
Creating a node for Double Linked List:
• The left field of the new node is made to point the previous node.
• The previous nodes right field must be assigned with address of the new node.
• Repeat the above steps ‘n’ times.
The following steps are to be followed to insert a new node at the beginning of the list:
The following steps are followed, to insert a new node in an intermediate position in
the list:
The following steps are followed, to delete a node at the beginning of the list:
temp = start;
start = start -> right;
start -> left = NULL;
free(temp);
The function dbl_delete_beg(), is used for deleting the first node in the list.
Deleting a node at the end:
The following steps are followed to delete a node at the end of the list:
• If list is empty then display ‘Empty List’ message
• If the list is not empty, follow the steps given below:
temp = start;
while(temp -> right != NULL)
{
temp = temp -> right;
}
temp -> left -> right = NULL;
free(temp);
The function dbl_delete_last(), is used for deleting the last node in the list.
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 nodes).
• If list is empty then display ‘Empty List’ message.
• If the list is not empty, follow the steps given below:
• Get the position of the node to delete.
• Ensure that the specified position is in between first node and last node. If not, specified position is invalid.
Then perform the following steps:
if(pos > 1 && pos < nodectr)
{
temp = start;
i = 1;
while(i < pos)
{
temp = temp -> right;
i++;
}
temp -> right -> left = temp -> left;
temp -> left -> right = temp -> right;
free(temp);
printf("\n node deleted..");
}
The function delete_at_mid(), is used for deleting the intermediate node in the list
Traversal and displaying a list (Right to Left):
To display the information from right to left, you have to traverse the list, node by node
from the first node, until the end of the list is reached.
The function traverse_right_left() is used for traversing and displaying the information
stored in the list from right to left.
The following steps are followed, to traverse a list from right to left:
• If list is empty then display ‘Empty List’ message.
• If the list is not empty, follow the steps given below:
temp = start;
while(temp -> right != NULL)
temp = temp -> right;
while(temp != NULL)
{
print temp -> data;
temp = temp -> left;
}
Counting the Number of Nodes:
The following code will count the number of nodes exist in the list (using recursion).
A Complete Source Code for the Implementation of Double Linked List
Circular double Linked List
• Circular doubly linked list is a more complexed type of data structure
in which a node contain pointers to its previous node as well as the
next node.
• Circular doubly linked list doesn't contain NULL in any of the node.
• The last node of the list contains the address of the first node of the
list.
• The first node of the list also contain address of the last node in its
previous pointer.
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.
The basic operations in a
circular single linked list are:
• Creation.
• Insertion.
• Deletion.
• Traversing.
Creating a Circular Double Linked List with ‘n’ number of nodes:
The following steps are followed, to traverse a list from left to right:
• If list is empty then display ‘Empty List’ message.
• If the list is not empty, follow the steps given below:
temp = start;
Print temp -> data;
temp = temp -> right;
while(temp != start)
{
print temp -> data;
temp = temp -> right;
}
Traversing a circular double linked list from right to left: