forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_Linked_List.c
72 lines (63 loc) · 1 KB
/
Circular_Linked_List.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
#include<stdio.h>
#include<stdlib.h>
typedef struct circ buff;
struct circ{
int val;
buff *prev;
buff *next;
};
buff *head;
buff *tail;
buff *insert(buff *head,int x){
buff *tmp;
tmp=(buff*)malloc(sizeof(buff));
tmp->val=x;
tmp->next=tmp;
tmp->prev=tmp;
if(head==NULL){
tail=tmp;
return tmp;
}
else{
tmp->prev=tail;
tmp->next=head;
tail->next=tmp;
head->prev=tmp;
tail=tmp;
}
return head;
}
buff *print(buff *head){
buff *start=head;
while(head!=tail){
printf("%d ",head->val);
head=head->next;
}
printf("%d\n",head->val);
return start;
}
int main(){
int x,i,j;
tail=head=NULL;
//char c;
// scanf("%c",&c);
// if(c=='i'){
while(1){
// scanf("%d",&x);
head=insert(head,5);
head=insert(head,6);
head=insert(head,7);
head=insert(head,8);
head=insert(head,9);
head=insert(head,0);
head=insert(head,4);
head=print(head);
printf("head-> %d\n",tail->next->val);
printf("tail-> %d\n",head->prev->val);
break;
}
return 0;
}
//output: 5 6 7 8 9 0 3 4
// head-> 5
// tail-> 4