Monday, June 11, 2012

C Program for deletion of node from a circular doubly linked list


Today I will discuss about the program to delete a node from a circular doubly linked list.

#include< stdio.h >
#include< stdlib.h >
struct list
{
int num;
struct list *next;
struct list *prev;
};
struct list *node;
int i=0;
void create(struct list *n)
{
char ch='y';
node=n;
do
{
i++;
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
} while(ch!='n');
node->next=n;
n->prev=node;
}
void displayL(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}
void displayR(struct list *n)
{
node=n->prev;
while(node!=n)
{
printf("%d\n",node->num);
node=node->prev;
}
}
void delL(struct list *n)
{
node=n->next;
while(node->next!=n)
{
node=node->next;
}
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
i--;
}
void delA(struct list *n)
{
int c=1,count;
node=n->next;
while(1)
{
printf("\nWhich location to be deleted:");
scanf("%d",&count);
if(count>=0 && count<=i)
break;
else
printf("\nRe-enter the location, you have only %d nodes.",i);
}
while(node!=n)
{
if(c==count)
break;
node=node->next;
c++;
}
node- >prev- >next=node->next;
node->next->prev=node->prev;
free(node);
}
void delF(struct list *n)
{
struct list *p;
node=n->next;
n->next=node->next;
node->next->prev=n;
free(node);
i--;
}

void main()
{
struct list *start;
char ch='F';
clrscr();
start=(struct list*)malloc(sizeof(struct list));
/* header node without any value */
create(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
while(ch!='E')
{
do
{
printf("\nPress 'F' for first location deletion, 'L' for last location deletion ,\n'A' for any location deletion and 'E' for exit:");
ch=toupper(getche());
}while(strchr("FLAE",ch)==NULL);
if(ch=='F'&& i!=0)
{
delF(start);
printf("\nAfter deletion\n");
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
}
else if(ch=='L'&& i!=0)
{
delL(start);
printf("\nAfter deletion\n");
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
}
else if(ch=='A'&& i!=0)
{
delA(start);
printf("\nAfter deletion\n");
printf("Now display the value of the circular lists(L to R):-\n");
displayL(start);
printf("Now display the value of the circular lists(L to R):-\n");
displayR(start);
}
else if(ch=='E')
break;
else
printf("\nOnly header node is existing, deletion not possible");
}
getch();
}

Detail study of this circular doubly linked list program

  
In this program firstly I have created a header node without any value in the main () function and the header node is passed to create () function as an argument. Prototype of the create () function is void create (struct list *n). In this function, nodes are created and the connection is done using a do while loop.

After creating the circular doubly linked list, the list is displayed from both the ends using displayL () and displayR () functions. Prototype of both the functions are same, void displayL (struct list *n) and void displayR (struct list *n). While loop is used inside the functions body to display the list from both sides.

Next inside the main () function body a while loop is used to take choice from user whether he/she wants to delete any node and from which location. Another do while loop is used inside the body of the above while loop to confirm correct input. Three deletion functions are defined in this program. Function prototypes are follows:
void delA (struct list *n), void delF (struct list *n) and void delL (struct list *n). These three functions are used to delete node from any location, first location and last location of the list and after every deletion operation the list is displayed from both the ends.

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner