Unit 4-5 Linked List
Unit 4-5 Linked List
Answer: Memory (RAM) is one of the most important resource of a computer. It must be utilized
efficiently.
When we represent a group of items as an array, then memory for all these items are allocated at
compile time in contiguous memory locations and is fixed. This process is also called static memory
allocation and the list so formed is called contiguous list. [Give structure of contiguous list and
the process of insertion and deletion of items]
The disadvantages with this approachare:
It is actually quite difficult to know the exact size of the array in advance (prior to execution).
If the size needed at run time is small than the specified size, then we will have
wastage of memory space.
If the size needed at run time is greater than the specified size, then we will have
shortage of memory space.
Insertion and deletion of items is difficult in this representation.
A linear linked list is defined as a collection of nodes in which each node has two
parts: (i) information part and (ii) link part.
When we represent a group of items as a linear linked list, the items are not
contiguously aligned in memory but instead they are linked to each other via
pointers. The information part of each node of the linear linked list contains the
value of the items and the link part contains the address of the next node in the
linear linked list. The last node of the linear linked list contains the NULL
pointer in its link part indicating the end of the linear linked list. [Give
structure of linked list and the process of insertion and deletion of
items]
1. if head==NULL then {
if(head == NULL)
print “empty list” and exit printf("List is Empty!!! Deletion not possible!!!");
2. else else
{
set hold=head struct Node *temp = head;
set head=head->next if(temp -> previous == temp -> next) // if list have only one node
{
set head->prev=NULL; head = NULL;
free(hold) }
free(temp);
3. End else{
head = temp -> next;
head -> previous = NULL;
free(temp);
}
printf("\nDeletion success!!!");
}}
1. if head==NULL then {
free(hold) if(temp -> previous == temp -> next) // list has single node
3. else {
head = NULL;
set temp=head;
free(temp);
while(temp->next->next !=NULL) }
temp=temp->next else{
end while while(temp -> next != NULL)