Doubly Link List
Doubly Link List
1 December 8, 2
021
Introduction
Doubly-linked list(DLL) is a more sophisticated kind of linked list. In DLL,
each node has two links: one points to previous node and one points to next
node.
You can scan a doubly-linked list in both directions.
There are two NULL: at the first and last nodes in the list
A B C
2 December 8, 202
1
Introduction
Advantages of Doubly Link List
Solves the problem of traversing backwards in an ordinary linked list.
A link to the previous item as well as to the next item is maintained.
Also there is no pointer to the start of the list. Instead, there is simply a pointer to
some position in the list which can be moved left or right.
Disadvantage : is that every time an item is inserted or deleted, two links have to
be changed instead of one.
A doubly-linked list can also be created as a double – ended list.
3 December 8, 202
1
C programs for Doubly Link List
/* ADD NEW NODE AT THE END
typedef struct node
OF LINK LIST */
{
append(list **p,int num)
struct node *prev;
{
int data;
struct node *next;
list *temp, *r;
}list;
temp = (list *)malloc(sizeof(list));
Main()
temp->prev = NULL;
{
temp->data = 5;
list *head;
temp->next = NULL;
head = NULL;
append(&head, 10); 5
}
4 December 8, 202
1
Append the Node
if(*p == NULL)
*p = temp;
10
else
{ Temp
r=*p; p
while(r->next!=NULL)
{ r=r->next; }
r->next=temp;
r r
temp->prev=r;
}
return; 5 15
}
5 December 8, 202
1
Append the Node
5 15 10
Add Node at begging of Link List
/* ADD NEW NODE AT THE
BEGINNING OF THE LINK LIST */
5 10
addbeg(list **p,int num)
{
list *temp,*r; p
temp=(list *)malloc(sizeof(list));
temp->prev=NULL;
temp->data = 2;
temp->next = *p;
2
(*p)->prev=temp;
*p=temp; Temp
return;} p
7 December 8, 202
1
Add Node at begging of Link List
2 5 10
8 December 8, 202
1
Intermediate Insert
temp->next = r->next;
temp->prev = r;
5 10
r->next = temp;
temp->next->prev=temp;
r
100
Temp
9 December 8, 202
1
Delete First Node
r=*p;
*p = r->next;
5 10
(*p)->prev = NULL;
r p
10
10 December 8, 202
1
Delete Last Node
if(r->next == NULL)
r->prev->next = NULL;
5 10
r
5
11 December 8, 202
1
Delete Intermediate Node
r->prev->next = r->next;
r->next->prev = r->prev;
2 5 10
2 10
12 December 8, 202
1
Tutorial Problems
Create New Structure: STACKLIST which is a list of linked stacks stored
according to a priority factor i.e. A, B, C etc, where A means highest priority, B the
next and so on. Elements having the same priority are stored as a linked stack. For
deletion only ask the priority.
The following is a structure of the STACKLIST S after the performing given
operations:
insert(3,A,…), insert(4,C,…), insert(6,A,…), deleteFrom(A), insert(2,A),
insert(5,A), insert(1,C).
13 December 8, 202
1
Tutorial Problems
Create New Structure: QUEUELIST which is a list of linked queues stored
according to a priority factor i.e. A, B, C etc, where A means highest priority, B the
next and so on. Elements having the same priority are stored as a linked queue. For
deletion only ask the priority.
The following is a structure of the QUEUELIST S after the performing given
operations:
insert(3,A,…), insert(4,C,…), insert(6,A,…), deleteFrom(A), insert(2,A),
insert(5,A), insert(1,C).
14 December 8, 202
1
Tutorial Problems
Create New Structure: STACKQUEUE Which is a combination of a linked
stack and a linked queue.
The insert operation performs the insertion of an element in the top or rear of
the list based on whether the STACKQUEUE structure is viewed as a stack or
queue respectively.
For example: Operation Insert (Where, top, rear, item) //Here, Where indicates
whether the insertion of Item is to be done as on a stack or as on a queue.
The delete operation performs deletion of first element in the list.
15 December 8, 202
1