After working on singly linked list lets move on to circular linked list.It is very much similar to linear linked list except that here the last node contains the address of the start node again instead of NULL value.
So,lets code...:)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void append(struct node **,int);
void display(struct node *);
int main()
{
struct node *start=NULL;
append(&start,10);
append(&start,25);
append(&start,5);
append(&start,16);
append(&start,22);
append(&start,24);
append(&start,8);
append(&start,31);
append(&start,12);
printf("Original list\n");
display(start);
return 0;
}
void append(struct node **ps,int x)
{
struct node *temp,*temp1;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=x;
if(*ps==NULL)
{
*ps=temp1;
(*ps)->next=*ps;
return;
}
temp=*ps;
while(temp->next!=*ps)
temp=temp->next;
temp->next=temp1;
temp1->next=*ps;
}
void display(struct node *ps)
{
struct node *temp;
temp=ps;
while(temp->next!=ps)
{
printf("%d\n",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
The code simply is to add some nodes and display them in a circular linked list.
No comments:
Post a Comment