-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIRCULAR.CPP
173 lines (169 loc) · 2.45 KB
/
CIRCULAR.CPP
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
} ;
struct node *head;
void insert_atbeg(int);
void traversal();
void insert_atend(int);
//void reversal();
void delete_atbeg();
void delete_atend();
void insert_after(int,int);
void delete_after(int);
void main()
{
clrscr();
int ch,ele,val;
head=NULL;
printf("1:insertion at begin\n");
printf("2:traversal\n");
printf("3 insertion at end\n");
//printf("4: reversal\n");
printf("5:deletion at begin\n");
printf("6:deletion at end\n");
printf("7:insertion after node\n");
printf("8:deletion after node \n");
printf("9:exit");
do
{
printf("enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter ele you want to insert ");
scanf("%d",&ele);
insert_atbeg(ele);
break;
case 2:
traversal();
break;
case 3:
printf("enter ele you want to insert ");
scanf("%d",&ele);
insert_atend(ele);
break;
/*case 4 :
reversal();
break;
*/
case 5:
delete_atbeg();
break;
case 6 :
delete_atend();
break;
case 7: printf("enter ele you want to insert ");
scanf("%d",&ele);
printf("enter elenent after you want to insert ");
scanf("%d",&val);
insert_after(ele,val);
break;
case 8:
printf("enter elenent after you want to delete");
scanf("%d",&val);
delete_after(val);
break;
case 9 : exit(1);
break;
}
}
while(ch!=9);
getch();
}
void insert_atbeg(int item)
{
struct node *q,*p;
p=head;
q=(struct node*)malloc(sizeof(struct node));
q->data=item;
q->next=head;
while(p->next!=head)
{
p=p->next;
}
head=q;
p->next=q;
}
void traversal()
{
struct node *p;
p=head;
while(p->next!=head)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
void insert_atend(int item)
{
struct node *q,*p;
p=head;
q=(struct node*)malloc(sizeof(struct node));
q->data=item;
while(p->next!=head)
{
p=p->next;
}
p->next=q;
q->next=head;
}
void delete_atbeg()
{
struct node *p,*q;
p=head;
q=head;
while(p->next!=head)
{
p=p->next;
}
p->next=q->next;
head=q->next;
}
void delete_atend()
{
struct node *p;
p=head;
while(p->next->next!=head)
{
p=p->next;
}
p->next=head;
}
void delete_after(int e)
{
struct node *p;
p=head;
while(p->data!=e)
{
p=p->next;
}
p->next=p->next->next;
printf("deleted element=%d",p->next->data);
}
void insert_after(int b,int v)
{
struct node *r,*p;
p=(struct node *)malloc(sizeof(struct node));
r=head;
while(r!=NULL)
{
if(r->data==v)
{
p->data=b;
p->next=r->next;
r->next=p;
printf("insertion completed");
break;
}
else
r=r->next;
}
}