-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_linked_list.c
151 lines (151 loc) · 2.56 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *ptr;
};
struct node *start=NULL;
struct node *insert()
{
struct node *new_node,*q;
int num;
printf("\nEnter -1 to stop.\n");
printf("\nEnter elements for circular linked list.\n");
scanf("%d",&num);
while(num!=-1)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
if(start==NULL)
{
start=new_node;
q=start;
new_node->ptr=start;
}
else
{
q->ptr=new_node;
new_node->ptr=start;
q=q->ptr;
}
scanf("%d",&num);
}
return start;
}
struct node *display()
{
struct node *q;
q=start;
while(q->ptr!=start)
{
printf("\t%d",q->data);
q=q->ptr;
}
printf("\t%d",q->data);
return start;
}
struct node *insert_beg()
{
struct node *new_node,*a;
int num;
printf("\nEnter no. to add at the starting of the circular linked list: ");
scanf("%d",&num);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
a=start;
while(a->ptr!=start)
a=a->ptr;
a->ptr=new_node;
new_node->ptr=start;
start=new_node;
return start;
}
struct node *insert_end()
{
struct node *new_node,*z;
int n;
printf("\nEnter element to add at end of list: ");
scanf("%d",&n);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=n;
z=start;
while(z->ptr!=start)
z=z->ptr;
z->ptr=new_node;
new_node->ptr=start;
return start;
}
struct node *insert_after()
{
struct node *preptr,*new_node,*x;
int num,b;
printf("\nEnter element to be added: ");
scanf("%d",&num);
printf("\nEnter no after which you want to add: ");
scanf("%d",&b);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
x=start;
preptr=x;
while(preptr->data!=b) {
preptr=x;
x=x->ptr;
}
preptr->ptr=new_node;
new_node->ptr=x;
return start;
}
struct node *del_first()
{
struct node *x,*z;
z=start;
while(z->ptr!=start)
z=z->ptr;
x=start;
start=start->ptr;
z->ptr=start;
free(x);
printf("\n");
return start;
}
struct node *del_last()
{
struct node *z,*preptr;
z=start;
while(z->ptr!=start)
{
preptr=z;
z=z->ptr;
}
preptr->ptr=start;
free(z);
printf("\n");
return start;
}
struct node *del()
{
struct node *z;
z=start;
while(z->ptr!=start)
start=del_last();
free(z);
return start;
}
int main()
{
insert();
display();
insert_beg();
display();
insert_end();
display();
insert_after();
display();
del_first();
display();
del_last();
display();
del();
return 0;
}