In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge about Data structure and alogorithm in C,Java,Python
and other programming languages that may help you land an online job and make your programmers day successfull.
We use best data structure notes and best programming resources. Here goes the article,dont forget to share with your friends 😃
Circuler Linked list:
Here I include only the functions of creating list,insert an element at first & display the link;the code is as follows:
//Implementation of Circuler linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *last;
void list(int a)
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=a;
if(last==NULL)
{
last=temp;
temp->link=last;
}
else
{
temp->link=last->link;
last->link=temp;
last=temp;
}
}
void addbeg(int b)
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=b;
if(last==NULL)
{
last=temp;
temp->link=last;
}
else
{
temp->link=last->link;
last->link=temp;
}
}
void display()
{
struct node *ptr;
if(last==NULL)
{
printf("\nList is empty!\n");
return;
}
ptr=last->link;
printf("\nList is:\n");
while(ptr!=last)
{
printf("%d->",ptr->data);
ptr=ptr->link;
}
printf("%d\n",last->data);
}
main()
{
int item,i,ch,n;
while(1)
{
printf("\n1.Create list\n2.Insert at first\n3.Insert at perticuler position\n4.Display\n5.Exit\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element:\n");
scanf("%d",&item);
list(item);
}
break;
case 2:
printf("Enter the element:\n");
scanf("%d",&item);
addbeg(item);
break;
case 4:
display();
break;
case 5:
exit(0);
}
}
}
0 Comments