Linear Data Structure 517
void show()
{
printf (”n Linked lis t elements are : " );
while (header I«NULL)
{
printf (" %d ", header->num);
header-header- >next;
}
>
OUTPUT;
Operation creation:
Enter number. 12 3 4 5 0
Linked list elements are: 1 2 3 4 5
After deletion:
Linked list elements are: 2 3 4 5
Linked list elements are: 3 4 5
Linked list elements are: 4 5
Explanation In the above program the f orm () and show () operations are the same as
implemented in the last programs. In addition, rem () operation is carried out. In the rem () function
the new object *s is declared. The memory allocated to pointer header is assigned to pointer *s. The
f ree () function deallocates the memory allocated to s. The address of the second node is assigned
to pointer header. The pointer header points to the first node. In this way, when nodes are removed,
memory is released.
14.22 DOUBLE LINKED LIST
In this type of linked list the data structure holds two pointer fields. In the single linked list, address
of only the next element is pointed. In addition, in a double linked list addresses of next as well as
preceding elements are linked with the current node. The single linked list has some limitations that
can be overcome by the double linked list. Consider a case in which the access pointer is in between
the linked list and we need to access the earlier element. Due to lack of previous item link, we cannot
transverse the list in reverse direction and hence we have no option except to transverse the list from
the beginning.
In a double linked list, each node has two link fields and they hold the addresses of the previous
and next nodes which enables us to access not only the next element but also the previous element.
Start
y
r
.......
I
5 I & • &
7
&
00
4
/
I
---
I 1 /
j :
Previous
Next
Fig. 14.23 Double linked list