Showing posts with label Circular Linked List. Show all posts
Showing posts with label Circular Linked List. Show all posts

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.

Saturday, June 9, 2012

Circular Linked list using C Language


Linear linked list is a very useful data structure, but it has several shortcomings. With a modification in the linear linked list we can get another type of list, Circular linked list.In linear linked list the next address pointer of the last node points to NULL. If the same pointer is made to point the address of the first node of the list a different type of list will be generated which is known as circular linked list.

Such a list does not have any first or last node. So we have to establish a first node and last node in a circular linked list. One useful technique is to point the first node of the list by the external pointer. With the help of the external pointer we can access the nodes. Suppose p is the pointer which will traverse through the linked list and start is another pointer which is pointing the first node of the list. Since the list is circular we will not know when the complete list is traversed. So after every movement of the pointer p from one node to another test the condition p==start. Or we can use header node in circular linked list to identify the starting point. The header node can be identified by a special value in it’s information part.

The following program illustrates to create circular linked list with an external pointer pointing the first node while the other pointer will traverse through the linked list.

#include< stdio.h >
#include< stdlib.h >
struct list
{
int num;
struct list *next;
};
struct list *node;
void create(struct list *n)
{
char ch;
node=n;
printf("\nEnter value for the first node:-");
scanf("%d",&node->num);
fflush(stdin);
node->next=NULL;
printf("\nWant to create another node(y/n):-");
ch=getche();
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)stdlib(sizeof(struct list));
node=node->next;
printf("\nEnter value:-");
scanf("%d",&node->num);
printf("\nAny more (y/n)");
ch=getche();
}
node->next=n;
}
void display(struct list *n)
{
node=n;
do
{
printf("%d\n",node->num);
node=node->next;
}while(node!=n);
}
void main()
{
struct list *start=(struct list *)stdlib(sizeof(struct list));
clrscr();
create(start);
printf("\nNow display the value of the circular lists:-\n");
display(start);
getch();
}

Now we will set a header node in a circular linked list.

The following program will create a circular linked list with a header node and new nodes can be inserted at any location or can be deleted from any location.

#include< stdio.h >
#include< stdlib.h >
#include< string.h>
struct list
{
char name[100];
struct list *next;
};
struct list *start;
void insert()
{
int c=1,count;
struct list *p,*node,*new1;
printf("\nin which location you want to insert:");
scanf("%d",&count);
node=start;
p=node->next;
new1=(struct list *)stdlib(sizeof(struct list));
printf("\Enter Name:-");
scanf("%s",&new1->name);
while(p)
{
if(c==count)
break;
node=node->next;
p=p->next;
c++;
}
node->next=new1;
new1->next=p;
}
void del()
{
int c=1,count;
struct list *p,*node;
printf("\nwhich location you want to delete:");
scanf("%d",&count);
node=start;
p=node->next;
while(p->next!=NULL)
{
if(c==count)
break;
node=node->next;
p=p->next;
c++;
}
node->next=p->next;
free(p);
}
void create(struct list *node)
{
char ch;
start=node;
printf("\nEnter value for the header node (****):-");
scanf("%s",&node->name);
fflush(stdin);
node->next=NULL;
printf("\nWant to create another node(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)stdlib(sizeof(struct list));
node=node->next;
printf("\Enter Name:-");
scanf("%s",&node->name);
fflush(stdin);
node->next=NULL;
printf("\Any more (y/n)");
scanf("%c",&ch);
}
node->next=start;
/*last node is connected with the header node */
}
void display(struct list *node)
{
int x=0;
node=node->next;
while(x==0)
{
printf("%s\n",node->name);
node=node->next;
if(strcmp(node->name,"****")==0)
x=1;
}
}
void main()
{
struct list *node=(struct list *)malloc(sizeof(struct list));
clrscr();
create(node);
printf("Now display the Names of the circular list:-\n");
display(node);
del();
printf("\nAfter deletion\n");
display(node);
insert();
printf("\nAfter insertion\n");
display(node);
getch();
}

Subscribe via email

Enter your email address:

Delivered by FeedBurner