-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularlinkedlist.c
46 lines (43 loc) · 949 Bytes
/
circularlinkedlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include<stdlib.h>
struct node{
char name[10];
int rollno;
struct node *next;
}*n1,*n2,*extra,*n3;
struct node * insert(struct node *n){
printf("Enter extra :\n");
extra=(struct node*)malloc(sizeof(struct node));
scanf("%s%d",extra->name,&extra->rollno);
struct node *p=n->next;
while(p->next!=n1){
p=p->next;
}
extra->next=p->next;
p->next=extra;
n=extra; //head changed
return n;
}
void print(struct node *n){
do{
printf("%s\t%d\n",n->name,n->rollno);
n=n->next;
}while(n!=n1);
}
int main(){
n1=(struct node*)malloc(sizeof(struct node));
n2=(struct node*)malloc(sizeof(struct node));
n3=(struct node*)malloc(sizeof(struct node));
printf("Enter element\n");
scanf("%s%d",n1->name,&n1->rollno);
n1->next=n2;
printf("Enter element\n");
scanf("%s%d",n2->name,&n2->rollno);
n2->next=n3;
printf("Enter element\n");
scanf("%s%d",n3->name,&n3->rollno);
n3->next=n1;
print(n1);
n1=insert(n1);
print(n1);
}