-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCircularLinked.c
91 lines (76 loc) · 1.37 KB
/
CircularLinked.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Implementation of Circular Singly Linked List
#include<stdio.h>
#include<stdlib.h>
struct node {
int info;
struct node *next;
};
typedef struct node* nodeptr;
nodeptr list1=NULL;
nodeptr list2=NULL;
nodeptr getnode() {
nodeptr p=(nodeptr)malloc(sizeof(struct node));
if(p==NULL) {
printf("Could not create node\n");
}
else {
p->next=NULL;
return p;
}
}
void insempty(nodeptr *list,int x) {
nodeptr p=getnode();
p->info=x;
*list=p;
p->next=p; // first node should point itself on creation
}
void insbegin(nodeptr *list,int x) {
if(*list==NULL) {
insempty(list,x);
}
else {
nodeptr p=getnode();
p->info=x;
p->next=(*list)->next;
(*list)->next=p;
}
}
void insend(nodeptr *list,int x) {
if(*list==NULL) {
insempty(list,x);
}
else {
nodeptr p=getnode();
p->info=x;
p->next=(*list)->next;
(*list)->next=p;
(*list)=p;
}
}
void view(nodeptr list) {
if(list==NULL) {
printf("List is Empty\n");
return;
}
nodeptr p=list->next;
do {
printf("%d ",p->info);
p=p->next;
}while(p!=list->next);
printf("\n");
}
int main() {
insend(&list1,5);
insbegin(&list1,4);
insbegin(&list1,3);
insbegin(&list1,2);
insbegin(&list1,1);
insend(&list1,6);
view(list1);
insbegin(&list2,10);
insbegin(&list2,9);
insbegin(&list2,8);
insbegin(&list2,7);
view(list2);
return 0;
}