Circular link list is special case of normal (one way link list) in this last node point to first node.whether in one way link list last node point to NULL.
This special property make one way link list circular.
About all operation of circular link list is similar to one way list .only few changes occurred . You can see this in C code.
C code :-
OUTPUT :-
This special property make one way link list circular.
About all operation of circular link list is similar to one way list .only few changes occurred . You can see this in C code.
circular link list |
// Program of circular linked list
#include <stdio.h>
#include <malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
typedef struct node node;
main()
{
int choice,n,m,po,i;
head=NULL; // initialise head to NULL
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after \n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Quit\n");
while(1)
{
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0; i < n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
addafter(m,po);
break;
case 4:
del();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
int create_list(int num)
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=num;
if(head==NULL) // insert as first node
{
head=temp;
temp->link=head;
}
else //insert as last node (append)
{
node *r;
r=head;
while(r->link!=head)
{
r=r->link;
}
temp->link=r->link;
r->link=temp;
}
}
int addatbeg(int num)
{
node *temp,*r;
r=head;
temp=(node*)malloc(sizeof(node)); // creat new node
temp->data= num;
while(r->link!=head)
{
r=r->link;
} // nor r points to last node of list
temp->link=head;
head=temp;
r->link=head; // last element point to first element
}
int addafter(int num,int pos)
{
int k=1;
node *temp,*r;
temp=head;
r=(node*)malloc(sizeof(node));
r->data=num;
while(k < pos) // move to node where we have to insert
{
k++;
if(head == temp->link)
{
printf("\nThere are less than %d elements\n",pos);
return;
}
temp=temp->link;
}
r->link=temp->link;
temp->link=r;
}
int del()
{
int num;
printf("Enter the number for deletion : ");
scanf("%d",&num);
node *temp,*r;
temp=head;
r=head;
if(head==NULL)
{
printf("\n list is empty\n");
}
else
{
if(temp->data == num)
{
while(r->link!=head) // after this r point to last elemnt
{
r=r->link;
}
head=temp->link;
r->link=head; // last element point to first element
free(temp);
return;
}
else
{
if(temp->link==head && temp->data==num) //only one element in list have same volue num
{
head==NULL;
free(temp);
}
else
{
while(temp->link!=head)
{
temp=temp->link;
if(temp->data==num)
{
r->link=temp->link;
free(temp);
return;
}
else
r=r->link;
}
printf("\nentered element not found\n");
}
}
}
}
int display()
{
int count=0;
node *temp;
if(head==NULL)
{
printf("\n list is empty\n");
return;
}
else
{
printf("\n list is :\n");
temp=head;
count++;
while(temp->link!=head)
{
printf(" %d ",temp->data);
temp=temp->link;
count++;
}
printf(" %d \n",temp->data);
printf("\n no of element = %d\n",count);
}
}
OUTPUT :-
circular link list output |
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT