Sunday, November 16, 2008

doubly linked list

Write a C program to support the following operations on a doubly linked list where each node consists of integers.
a] Create a doubly linked list by adding each node at the front.
b] Insert new nodes to the left of the node whose key value is read as an input
c] Delete the node of the given data, if it is found, otherwise display appropriate message.
d] Display the contents of the file.

Program:
#include"stdio.h" /*PREPROCESSOR DIRECTIVES*/
#include"conio.h"
#include"alloc.h"
struct node /*STRUCTURE TEMPLATE DECLARATION*/
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *nod;

/*FUNCTION PROTOTYPE DECLARATION*/
nod getnode();
void freenode(nod);
nod insertf(int,nod);
nod insertat(int,nod);
nod del(int,nod);
void display(nod);
void main()
{
nod head; /*VARIABLE DECLARATION*/
int item,ch,choice;
clrscr();
head=getnode();
head->info=0;
head->rlink=head->llink=head;
do /*DO-WHILE LOOP*/
{
printf("1:Insert front\n");
printf("2:Insert to left of given node\n");
printf("3:Delete a node\n");
printf("4:Display\n");
printf("5:Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice) /*SWITCH STATEMENT*/
{
case 1:printf("Enter the item to insert\n");
scanf("%d",&item);
head=insertf(item,head);
break;
case 2:printf("Enter the key\n");
scanf("%d",&item);
head=insertat(item,head);
getch();
break;
case 3:printf("Enter the item to be deleted\n");
scanf("%d",&item);
head=del(item,head);
getch();
break;
case 4:display(head);
getch();
break;
} /*END OF SWITCH STATEMENT*/
}
while(choice!=5); /*END OF DO-WHILE LOOP*/
getch();
}

/*FUNCTION TO ALLOCATE MOEMORY DYNAMICALLY*/
nod getnode()
{
nod temp;
temp=(nod)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Memory not available\n");
exit(0);
}
return temp;
}

/*FUNCTION TO DEALLOCATE MEMORY OCCUPIED BY A NODE*/
void freenode(nod p)
{
free(p);
}

/*FUNCTION TO INSERT A NODE AT THE FRONT*/
nod insertf(int item,nod head)
{
nod temp;
nod cur;
temp=getnode();
temp->info=item;
cur=head->rlink;
head->rlink=temp;
temp->llink=head;
temp->rlink=cur;
cur->llink=temp;
return head;
}

/*FUNCTION TO INSERT A NEW NODE TO THE LEFT OF A NODE*/
nod insertat(int item,nod head)
{
nod temp,cur,prev;
if(head->rlink==head)
{
printf("List is empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("Key not found\n");
return head;
}
prev=cur->llink;
temp=getnode();
printf("Enter the item to insert towards left of %d:",item);
scanf("%d",&temp->info);
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
return head;
}

/*FUNCTION TO DELETE A NODE OF GIVEN DATA*/
nod del(int item,nod head)
{
nod prev,cur,next;
if(head->rlink==head)
{
printf("List is Empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
cur=cur->rlink;
if(cur==head)
{
printf("\nItem not found\n");
return head;
}
prev=cur->llink;
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
freenode(cur);
return head;
}

/*FUNCTION TO DISPLAY THE CONTENTS OF THE LIST*/
void display(nod head)
{
nod temp;
if(head->rlink==head)
{
printf("\n list is empty\n");
return;
}
temp=head->rlink;
printf("\nList Contents\n");
while(temp!=head)
{
printf("%d\t",temp->info);
temp=temp->rlink;
}
printf("\n");
return;
}

No comments:

Post a Comment