Data Structures UNIT 2
Data Structures UNIT 2
LINKED LIST
Array Vs Linked List – Singly linked list - Representation of a linked list in memory -
Operations on a singly linked list - Merging two singly linked lists into one list - Reversing a
singly linked list – Polynomial Manipulation using List - Advantages and disadvantages of
singly linked list - Circular linked list - Doubly linked list - Circular Doubly Linked List.
In an array, elements are stored In a linked list, new elements can be stored
in contiguous memory location or anywhere in the memory. Address of the memory
consecutive manner in the memory. location allocated to the new element is stored in
the previous node of linked list, hence forming a
link between the two nodes/elements.
In array, Insertion and Deletion operation In case of linked list, a new element is stored at the
takes more time, as the memory locations first free and available memory location, with only
are consecutive and fixed. a single overhead step of storing the address of
memory location in the previous node of linked
list.Insertion and Deletion operations are fast in
linked list.
Memory is allocated as soon as the array Memory is allocated at runtime, as and when a new
is declared, at compile time. It's also node is added. It's also known as Dynamic
known as Static Memory Allocation. Memory Allocation.
In array, each element is independent and In case of a linked list, each node/element points to
can be accessed using it's index value. the next, previous, or maybe both nodes.
Size of the array must be specified at time Size of a Linked list is variable. It grows at
of array declaration. runtime, as more nodes are added to it.
Array gets memory allocated in Whereas, linked list gets memory allocated
the Stack section. in Heap section.
The first node is the head node and it points to next node in the sequence.
The last node’s reference is null indicating the end of the list is shown in Fig.2.1
Every node has two pointers, one for pointing to next node and the other for pointing to
the previous node.
The next pointer of the last node and the previous pointer of the first node (head) are null
is shown in Fig 2.2
Circular Linked List is very similar to a singly linked list except that, here the last node
points to the first node making it a circular list as shown in fig 2.3
A linked list is represented by a pointer to the first node of the linked list. The first node is
called the head. If the linked list is empty, then the value of the head is NULL. In C, we
can represent a node using structures.
Algorithm
Addatbeg()
Begin
Newnode->data=K
Newnode->next=NULL
If(Head==NULL)
Head=Newnode
Else
Newnode->next=Head
Head=Newnode
Endif
End
The fig.2.5 shows how a node is added at the beginning of the linked list.
Fig 2.5 Adding node at the beginning of the linked list
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).
Algorithm
Addatend()
Begin
Newnode->data=K
Newnode->next=NULL
If(Head==NULL)
Head=Newnode
Else
Temp=Head
While(temp->next !=NULL)
temp=temp->next
endwhile
temp->next=Newnode
Endif
end
The Fig 2.6 shows how the node is added at the end of the list.
Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location is
the node value after which we want to insert the newNode).
Step 6 - Every time check whether temp is reached to last node or not. If it is reached to
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
The operation ‘Insert’ inserts the given element x in the k th position. A temporary pointer Temp
th
is created and made to point to Head. Now the Temp pointer is moved to the k – 1 node. A
new node with value x is created and the link of the new node is made to point to the position
where the link of temp is pointing. Then the link of temp is made to point to the new node. Thus
the given element is inserted in the position k is shown in fig.2.7.
Fig 2.7 Inserting node at position
Algorithm
Function insertin_mid()
Begin
Write "Enter the position:"
Read pos;
If head==NULL AND pos=1
then
Insert the node at the beginning
End if
If head->next==NULL AND pos=2
then
insert the node
End if
Else if pos>=2 AND pos<=ct
then
prev=head
for I = 2 to pos - 1 step +1
do
prev=prev->link
END FOR
next=prev->link
temp=new node
Write”Enter the data:"
Read temp->data
temp->link=next
prev->link=temp
ct=ct+1
else
Write "Enter a valid position & try again"
End if
End
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
follows...
We can use the following steps to delete a node from beginning of the single linked list...
Step 4 - Check whether list is having only one node (temp → next == NULL)
Step 4 - Check whether list has only one Node (temp1 → next == NULL)
Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until it reaches to the last node in the list. (until temp1 →
next == NULL)
We can use the following steps to delete a specific node from the single linked list...
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the
last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next
node.
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 function.
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 to be deleted, then
set head = NULL and delete temp1 (free(temp1)).
Step 8 - If list contains multiple nodes, then check whether temp1 is the first node in the
list (temp1 == head).
Step 9 - If temp1 is the first node then move the head to the next node (head = head →
next) and delete temp1.
Step 10 - If temp1 is not first node then check whether it is last node in the list (temp1 →
next == NULL).
Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 →
next and delete temp1 (free(temp1)).
Algorithm
Function del()
Begin
if(head= NULL)
then
Write “Empty list"
else
Write “Enter the position:"
Read pos
if(pos==1)
then
next=head->link
head=next
ct=ct-1
else
if((pos>=2)&&(pos<=ct))
prev=head
for(int i=2;i<=pos-1;i++)
Do
prev=prev->link
End FOR
temp=prev->link
next=temp->link
prev->link=next
ct=ct-1
End if
End If
End
We can use the following steps to display the elements of a single linked list...
Step 1 - Check whether list is Empty (head == NULL)
Counting
count()
begin
int co=0;
do
co++;
c=c->link;
while(c!=NULL);
return co;
end
Searching
The operation Search( x ), searches the given value x in the list. If found returns the node
position where it is found. A temporary pointer Temp is created and made to point to the Head.
Now info part of Temp is compared with the value x. If the value matches the node position
number is returned otherwise Temp pointer is moved to the next node. This is repeated till the
end of the list is reached or till the element to be searched is found.
Algorithm
Function search()
Begin
flag=0
if(head=NULL)
then
Write “Empty list"
else
Write ”Enter the element to be searched:"
Read e
cur=head
FOR I = 1 to ct Step +1
Do
if(cur->data= e)
then
pos=I
flag++
break
else
cur=cur->link
End if
ENDFOR
If (flag =1)
then
Write "Element found in position:" pos
else
End Search
2.3 Merging
if(list2 == null){
return list1;
}
if(list1.data < list2.data){//initialize mergedList pointer to list1 if list1's data is lesser
mergedList = list1;
}else{//initialize mergedList pointer to list2 if list2's data is lesser or equal
mergedList = list2;
}
while(list1!=null && list2!=null){
if(list1.data < list2.data){
mergedList->next = list1;
list1 = list1->next;
}else{
mergedList->next = list2;
list2 = list2->next;
}
}
if(list1 == null){//remaining nodes of list2 appended to mergedList when list1 has
reached its end.
mergedList->next = list2;
}else{//remaining nodes of list1 appended to mergedList when list2 has reached its end
mergedList->next = list1;
}
return mergedList;
}
Traversing
The operation Display( ), displays all the value in each node of the list. A temporary pointer is
created and made to point to Head initially. Now info part of Temp is printed and Temp is
moved to the next node. This is repeated till the end of the list is reached.
Algorithm
Function display()
Begin
cur=head
cout<<"\nNo.of nodes is:"<<ct
cout<<"\nThe data is:"
while (cur< >NULL)
Do
Write "["<<cur->data<<"]->"
cur=cur->link
End while
End
the Requirement will less memory when compared to doubly, circular or doubly
the Singly linked list is the very easy data structure to implement.
Insertion and deletion of elements don’t need the movement of all the elements when
compared to an array.
therefore, Accessing the preceding node of a current node is not possible as there is no
backward traversal.
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;
do
{
printf("%d", temp -> data);
temp = temp -> next;
} while(temp != start);
In a doubly linked list, the head always points to the first node. The prev pointer of the
first node points to NULL and the next pointer of the last node points to NULL shown in
Fig.2.14
Addatbeg(x)
The operation Addatbeg(x) adds a given element x at the beginning of the list. A new
node R is created and the value x is store in the data part of R. The prev pointer of R is made to
point to NULL and the next pointer is made to point to head. Then the prev pointer of head is
made to point to R and head is now moved to point to R making it the first node. Thus the new
node is added at the beginning of the doubly linked list.
Algorithm
Function create()
Begin
temp=new node
Write"Enter the data:"
Read temp->data
End
Function insert_begin()
Begin
Call create()
tmp->flink=head
head=tmp
head->blink=NULL
ct=ct+1
End
Addatend(x)
The Addatend(x) operation adds the given element x at the end of the doubly linked list.
If the given list is empty then create a new node R and store the value of x in the data part of R.
Now make the prev pointer and the next pointer of R point to NULL. Then head is pointed to R.
If the list already contains some elements then, a temporary pointer is created and made to point
to head. The temp pointer is now moved to the last node and then a new node R is created. The
value x is stored in the data part of R and next pointer of R is made to point to NULL. The prev
pointer of R is made to point to temp and next pointer of Temp is made to point to R. Thus the
new node is added at the end of the doubly linked list is shown in fig.2.15.
Algorithm
Function append()
Begin
if(head=NULL)
then
insert_begin()
else
create()
temp->flink=NULL
prev=head
while(prev->flink< >NULL)
Do
prev=prev->flink
End while
prev->flink=temp
temp->blink=prev
ct=ct+1
End if
End
Insert(x, k)
The Insert(x) operation inserts a given element x at the specified position k. A temporary
pointer is created and made to point to head. Then it is moved to the k-1 th node. Now a new
node R is created and the data part is stored with value of x. The next pointer of R is made to
point to next(temp) and the prev pointer of next(temp) is made to point to R. Thus the links on
the right side of the new node is established. Now the next of Temp is made to point to R and
the prev pointer of R is made to point to temp thus establishing the links on the left side of the
new node. Now the new node is inserted at the position k is shown in Fig.2.16.
Algorithm
Function insertin_mid()
Begin
Write "Enter the position:"
Read pos;
If head->flink=NULL AND pos=1
then
Call insert_begin()
End if
If head->flink==NULL AND pos=2
then
Call append()
End if
Else if pos>=2 AND pos<=ct
then
prev=head
for I = 2 to pos - 1 step +1
do
prev=prev->flink
END FOR
next=prev->link
temp=new node
Write”Enter the data:"
Read temp->data
temp->flink=next
prev->flink=temp
temp->blink=prev;
next->blink=temp;
ct=ct+1
else
Write "Enter a valid position & try again"
End if
End
Fig.2.16 Insert at mid
Delete(x)
The Delete(x) operation deletes the element x from the doubly linked list. A temporary
pointer is created and made to point to head. Now the data of temp is compared with x and if it
matches that is the node to be deleted otherwise move to the next node and again compare. If the
node to be deleted is first node, then prev(next(temp)) is made to point to NULL and head is
pointed to next(temp). The node pointed by temp is deleted. When the node to be deleted is not
the first node, then next(prev(temp)) is made to point to next(temp) and prev(next(temp)) is
made to point to prev(temp). The node pointed by temp is deleted is shown in Fig.2.17.
Algorithm
Function del()
Begin
if(head= NULL)
then
cout<<"Empty list"
else
Write Enter the position\n"
Read pos
pre=head
if(pos<1 OR pos>ct)
Write Enter a valid position"
else
if(pos==1)
then
pre=pre->flink
head=pre
Write "node gets deleted\n"
Ct=ct -1
else
for(i=2;i<pos;i++)
pre=pre->flink
End For
tmp=pre->flink
nxt=tmp->flink
pre->flink=nxt
nxt->blink=pre
Write”node gets deleted\n"
Ct=ct -1
End if
End if
Fig.2.17 Deletion
Create node
create_node(int info)
begin
new->val = info;
new->next = NULL;
new->prev = NULL;
return new;
end
insert_at_end()
begin
Read info;
new = create_node(info);
if (first == last && first == NULL)
begin
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
endif
else
begin
last->next = new;
new->prev = last;
last = new;
first->prev = last;
last->next = first;
endif
end
Searching
search()
begin
int count = 0, key, i, f = 0;
read the value to be searched in the variable key
if (first == last && first == NULL)
print "list is empty no elemnets in list to search"
else
for (ptr = first,i = 0;i < number;i++,ptr = ptr->next)
begin
count++;
if (ptr->val == key)
begin
Print " the value is found at position count ”
f = 1;
end
end
if (f == 0)
print "the value is not found in linkedlist"
end
end
DISPLAYING IN BEGINNING
Algorithm
display_from_beg()
begin
int i;
if (first == last && first == NULL)
print "list is empty no elemnts to print"
else
begin
Store total number of node in the variable , number
for (ptr = first, i = 0;i < number;i++,ptr = ptr->next)
print ptr->val
end
end