forked from ishita0302/Code-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion in circular link list.c
178 lines (159 loc) · 5.5 KB
/
Insertion in circular link 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
struct node
{ int info;
struct node*next;
};
int main()
{
int ch,n1,c1,c2,y,x,p,z,count=1;
struct node *head,*temp,*prev,*nw;
//making first node
nw=(struct node*)malloc(sizeof(struct node));
if(nw==NULL)
{ printf("memory is not allocated.");
}
else
{
printf("enter first node: ");
scanf("%d", &n1);
nw->info=n1;
head=nw;
nw->next=head;
}
printf("\n1. insert an element at begining.\n2. insert an element at end.\n3. insert an element at given position.\n4. exit. ");
scanf("%d", &ch);
switch(ch)
{ case 1: //inserting at begining
do
{ struct node*b1;
b1=(struct node*)malloc(sizeof(struct node));
if(b1==NULL)
{ printf("memory is not allocated.");
}
else
{
printf("enter value: ");
scanf("%d", &x);
b1->info=x;
b1->next=NULL;
temp=head;
do
{
temp=temp->next;
}while(temp->next!=head);
temp->next=b1;
b1->next=head;
head=b1;
}
printf("\nPress 1 for inserting more and 0 to stop.");
scanf("%d", &c1);
}while(c1==1);
printf("\nthe list is: ");
temp=head;
do
{
printf("%d ", temp->info);
temp=temp->next;
}while(temp!=head);
break;
case 2: //inserting at end
do
{
struct node*b2;
b2=(struct node*)malloc(sizeof(struct node));
if(b2==NULL)
{ printf("memory is not allocated.");
}
else
{
printf("enter value: ");
scanf("%d", &y);
b2->info=y;
b2->next=NULL;
temp=head;
do
{
temp=temp->next;
}while(temp->next!=head);
b2->next=head;
temp->next=b2;
}
printf("\nPress 1 for inserting more and 0 to stop.");
scanf("%d", &c2);
}while(c2==1);
printf("the list is: ");
temp=head;
do
{
printf("%d ", temp->info);
temp=temp->next;
}while(temp!=head);
break;
case 3: //making list
do
{
struct node*b2;
b2=(struct node*)malloc(sizeof(struct node));
if(b2==NULL)
{ printf("memory is not allocated.");
}
else
{
printf("enter value: ");
scanf("%d", &y);
b2->info=y;
b2->next=NULL;
temp=head;
do
{
temp=temp->next;
}while(temp->next!=head);
b2->next=head;
temp->next=b2;
count++;
}
printf("\nPress 1 for inserting more and 0 to stop.");
scanf("%d", &c2);
}while(c2==1);
//inserting at given position
printf("\nenter the position: ");
scanf("%d", &p);
if(p>count)
printf("\tinvalid position");
else
{
struct node*b3;
b3=(struct node*)malloc(sizeof(struct node));
if(b3==NULL)
{ printf("memory is not allocated.");
}
else
{
printf("enter value: ");
scanf("%d", &z);
b3->info=z;
temp=head;
for(int i=0;i<p-1;i++)
{
prev=temp;
temp=temp->next;
}
b3->next=temp;
prev->next=b3;
}
printf("the list is: ");
temp=head;
do
{
printf("%d ", temp->info);
temp=temp->next;
}while(temp!=head);
}
break;
case 4: break;
default: printf("\ninvalid entry");
break;
}
return 0;
}