You are on page 1of 15

Experiment No.

11

Objective: Write a program for implementation of Doubly-Link List using Dynamic


memory allocation.

Theory: A doubly-linked list is a linked data structure that consists of a set of data
records, each having two special link fields that contain references to the previous and to
the next record in the sequence, that means each node has two links from both direction
and so it is called doubly linked.
Doubly linked lists is a special case of linked list in which the single node of it hold the
address of the next node to current node and the address of the previous node of the
current node. Here each record of the list is represented by node which in this case
contains three fields, first is the previous field which is a pointer that holds the address of
previous node ie previous and the second field is the information field ie. info which
contains the actual element on the list which may be simple integer, a character, a string
or even a large record, and third is next field ie. next which is a pointer contains the
address of next node the list. The structure of node of a linked list is shown in Fig.1.

Entire linked list can be accessed by an external pointer to the list pointing to the first
node of the list. So we can access the first node through the external pointer and the
second node through the next pointer of the first node and the previous pointer of the
third node. The previous field of the first node is set to be NULL that it holds no address
and the next field of the last node also contains a NULL value.
The whole structure of doubly linked list is shown in Fig.2 below:

Algorithm:

A general Algorithm for linked list


I. Declaring Node structure
1. creating structure dlink_node
2. declaring two fields info of integer type next & previous of structure dlink_node
type
3. declaring a pointer of dlink_node type, start, node, new_node, trail
4. set start = NULL
II. Getnode function
1. start GETNODE( )
2. allocate memory to pointer new_node of size declared in structure dlink_node
through malloc function
3. set, new_node.next:= NULL
4. set, new_node.previuos:= NULL
5. Return new_node
III. Appending Node
1. Enter the value to be inserted and store it in item
2. call GETNODE( ) function and get the new node’s address in new_node
3. set new_node.info:= item
4. if the inserted node is the first node then check for it is as follows
if ( start = = NULL)
then
set start:=new_node
end if
5. if any other node then set node:= start
6. repeat step 4 and 5 while( node!=null AND node.info < item)
7. set trail:= node
8. set node = node.next
9. end loop
10. after completion of this loop we will get the address of node before which the new
node to be inserted and trail hold the address after which the new node to be
inserted
11. now check for last node
if ( node.next = = NULL)
then
trail.next:= new_node
new_node.previous:=trail
else
new_node.next:= trail.next
trail.next= new_node
new_node.previous:= trail
end if
12. Check for more insertion
Message: Do you want to Insert more node (Yes/No)
if ( ans = = Yes)
then
call GETNODE
else
Rretun
IV. Delete Node
1. check for no node condition
if ( start = = NULL)
then
message: No node to delete
return
end if
2. Now ask for the node you want to delete and store it in item
3. initializing
node = start
4. repeat step 5 and 6 while( node != NULL AND node.info!= item)
5. node = node.next
6. end loop
7. after completion of loop we will find node as the node to be deleted
8. check for data for deletion not found
if( node = = NULL)
then
message: data not found
end if
9. check for first node condition
if ( node = = start)
then
start = NULL
free(node)
end if
10. check for last node condition
if ( node.next = = NULL)
then
node.previous.next = NULL
free(node)
else
node.previous.next = node.next
node.next.previous = node.previous
free(node)
end if
12. Return
V. Searching
1. check for list empty
if( start = =NULL)
then
message: List is empty
return
end if

2. ask for the data to be searched and store it in item


3. initialization
node = start
4. repeat step 5 while( node!= NULL AND node.info!= item)
5. node = node.next
6. check for the data searched or not
if ( node = = NULL)
then
message: Data Not Found
else
message: Data Found
end if
7. Return
Program:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>

void create_list(struct list *);


void display_list(struct list *);
void insert_first();
void insert_last();
void insert_desir();
void delete_first();
void delete_last();
void delete_desir();

struct list
{
int info;
struct list *next;
struct list *prev;
}*start=NULL,*last=NULL,*node,*temp,*newnode;

void main()
{
clrscr();
int ch;
printf("\n1. create_list");
printf("\n2. display_list");
printf("\n3. insert_first");
printf("\n4. insert_last");
printf("\n5. insert_desir");
printf("\n6. insert_mid");
printf("\n7. delete_first");
printf("\n8. delete_last");
printf("\n9. delete_desir");
printf("\n10. delete_mid");
printf("\n11. count_node");
printf("\n12. search_node");
printf("\n13. quit");
printf("\n\nEnter choise");
scanf("%d",&ch);
switch(ch)
{
case 1:
node=(struct list *)malloc(sizeof(struct list));
node->prev=NULL;
node->next=NULL;
start=last=node;
create_list(start);
break;
case 2:
display_list(start);
break;
case 3:
insert_first();
break;
case 4:
insert_last();
break;
case 5:
insert_desir();
break;
case 7:
delete_first();
break;
case 8:
delete_last();
break;
case 9:
delete_desir();
break;
case 13:
exit(0);
default:
printf("\nInvalid choise");
break;
}
getch();
main();
}

void create_list(struct list *node)


{
int ans;
printf("\nEnter info");
scanf("%d",&node->info);
printf("\nAnother node y/n");
fflush(stdin);
ans=getchar();
if(ans=='y')
{
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
last=node;
create_list(node);
}
else
node->next=NULL;
}

void display_list(struct list *node)


{
if(node==NULL)
printf("\nList is empty");
else
{
while(node)
{
printf("\n%d",node->info);
node=node->next;
}
}
}

void insert_first()
{
if(start==NULL)
printf("\nList empty");
else
{
newnode=(struct list *)malloc(sizeof(struct list));
printf("\nEnter info");
scanf("%d",&newnode->info);
newnode->prev=NULL;
newnode->next=start;
start->prev=newnode;
start=newnode;
}
}

void insert_last()
{
if (last==NULL)
printf("\nList empty");
else
{
newnode=(struct list *)malloc(sizeof(struct list));
printf("\nEnter info");
scanf("%d",&newnode->info);
newnode->next=NULL;
newnode->prev=last;
last->next=newnode;
last=newnode;
}
}

void insert_desir()
{
if(start==NULL)
printf("\nList empty");
else
{
int nodeno;
printf("\nEnter nodeno after which you want to insert");
scanf("%d",&nodeno);

node=start;
while(node)
{
if(node->info==nodeno)
{
newnode=(struct list *)malloc(sizeof(struct list));
printf("\nEnter info");
scanf("%d",&newnode->info);
newnode->next=node->next;
node->next->prev=newnode;
newnode->prev=node;
node->next=newnode;
if(last==node)
last=newnode;
}
node=node->next;
}
}
}

void delete_first()
{
if(start==NULL)
printf("\nList empty");
else
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
}

void delete_last()
{
if(start==NULL)
printf("\nList empty");
else
{
temp=last;
last=last->prev;
last->next=NULL;
free(temp);
}
}

void delete_desir()
{
if(start==NULL)
printf("\nList empty");
else
{
int nodeno;
printf("\nEnter nodeno which you want to delete");
scanf("%d",&nodeno);

if(start->info==nodeno)
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
else if(last->info==nodeno)
{
temp=last;
last=last->prev;
last->next=NULL;
free(temp);
}
else
{
node=start;
while(node)
{
if(node->info==nodeno)
{
temp=node;
node->prev->next=node->next;
node->next->prev=node->prev;
node=node->next;
free(temp);
}
node=node->next;
}
}
}
}

Program Output:

I.Creating List

II. Display List


III. Insert at First

IV. Insert at Last


V. Insert at Desired Place

VI. Delete from First


VII. Delete form last

VIII. Delete from desired place

You might also like