Doubly Linked List
In a Doubly Linked List, also called two-way list, each node is divided into three parts:
- The first Part, called previouspointer field, contains the address of the preceding element in the list.
- The second part, contains the information of the element,
- The third Part, called nextpointer field, contains the address of the succeeding element in the list.
Representation of Doubly linked list
typedef struct nodetype{
struct nodetype *prev;
int info;
struct nodetype *next;
}node;
node *head,*tail;
Operation on Doubly Linked List
Create an Empty List
The list will be empty in the beginning, the variable head and tail are assigned NULL value.
Void createEmptyList(node**head, node **tail)
{
*head=*tail=NULL;
}
Traversing a list
Doubly linked list can be traversed in two ways. These are
- In-order traversal
- Reverse-order traversal
In-order Traversal
To traverse the linear linked list, we move along the pointer, and process each element till we reach the last element.The following listing shows the various steps required.
void traverseinorder(node *head)
{
while(head!=null){
printf(“%d”, head->info);
head=head->next;
}
}
reverse-order Traversal
To traverse the linear linked list, we move last element to first element.The following listing shows the various steps required.
void traverseinreverseorder(node *tail)
{
while(tail!=null)
{
printf(“%d”, tail->info);
tail=tail->prev;
}
}
Searching an element
In searching we traverse in list in any order and search position specified information of list
node * search(node *head, int item){
while(head!=null){
if(head->info==item)
return head;
head=head->next;
}
return NULL;
}
Inserting an element
To insert an element in the list, the first task is to get a new node, assign the element to be inserted to the info field of the node, and then new node is placed at the appropriate position by adjusting the appropriate pointer. The insertion in the list can take place at the following position:
- at beginning of the list
- at end of the list
- after a given element
Inserting at the Beginning of the List
Insert an element at the beginning of the list, first we test whether the linked list is initially empty. If yes, then the element is inserted as the first and only element of the list by performing the following steps:
- Assign NULL value to the next pointer field of the new node
- Assign address of the new node to head
However, if the list is initially not empty, then the element is inserted as the first element of the list by performing the following steps:
- Assign value of the head variable (the address of the first element of the existing list) to the next pointer field of the new node.
- Assign address of the new node to head
void insertAtBegenning(node **head, int item)
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*head==NULL)
Ptr->next=NULL;
else
ptr->next=*head;
*head=ptr;
}
Inserting at the End of the List
To insert the element at the end of the list, first we test whether the linked list is initially empty. If yes, then the element is inserted as the first and only element of the list by performing the following steps:
- Assign NULL value to the next pointer field of the new node
- Assign address of the new node to head
However, if the list is initially not empty, then the list is traversed to reach the last element, and then the element is inserted as the last element of the list by performing the following steps.
- Assign NULL, value to the next pointer field of the new node.
- Assign address of the new node to next pointer field of the last node.
void insert AtEnd (node **head, int item)
{
node *ptr,*loc;
ptr= (node*) malloc( sizeof( node ));
ptr->info = item;
ptr->next= NULL;
if(*head == NULL) /* list initially empty */
*head= ptr;
else
{
loc =*head;
while (loc->next != NULL)
loc = loc->next;
loc->next = ptr;
}
}
Inserting after the Given Element of the List
To insert the new element after the given element first we find the location. say loc, of the given clement in the list, and then the element is inserted in the list by performing the following steps:
- Assign the next pointer field of the node pointed by loc to the next pointer field of the new node.
- Assign address of the new node to the next pointer field of the node pointed by loc.
Void insertAftertElement(node *head, int item, int after)
{
node *ptr, *loc;
loc=search(head,after);
If(loc==(node *)NULL)
return;
Ptr=(node*)malloc(sizeof(node));
Ptr->info=item;
Ptr->next= loc->next;
loc->next =ptr;
}