-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_display.c
More file actions
executable file
·227 lines (187 loc) · 3.58 KB
/
linked_list_display.c
File metadata and controls
executable file
·227 lines (187 loc) · 3.58 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
}*first;
void create(int A[],int n)
{
int i;
struct node* t;
struct node* last;
first = (struct node*)malloc(sizeof(struct node));
first->data=A[0];
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
t=(struct node*)malloc(sizeof(struct node));
t->data=A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void display(struct node* p)
{
printf("\n");
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n\nNo. of nodes= %d",count(first));
}
int count(struct node* p)
{ int i =0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
void search(struct node* p,int r)
{ int z=0;
while(p!=NULL)
{
if(p->data==r)
{
printf("\n\nElement found %d",p->data);
z=z+1;
break;
}
p=p->next;
}
if(z!=1){
printf("\n\nElement not found");
}
}
void insert(struct node*p,int index,int data)
{
struct node*t;
int x;
if(index<0||index>count(p))
return;
t=(struct node*)malloc(sizeof(struct node));
t->data=data;
if(index==0)
{
t->next=first;
first=t;
}
else
{
for(x=0;x<index-1;x++)
p=p->next;
t->next=p->next;
p->next=t;
}
}
void sortedinsert(struct node* p,int r)
{
struct node*t,*q=NULL;
t=(struct node*)malloc(sizeof(struct node));
t->data=r;
t->next=NULL;
if(p==NULL)
{
first=t;
}
else{
while(p&&(p->data<r))
{
q=p;
p=p->next;
}
if(p==first)
{
t->next=first;
first=t;
}
else
{
t->next=q->next;
q->next=t;
}
}
}
int Delete(struct node *p,int index)
{
struct node *q=NULL;
int x=-1,i;
if(index < 1 || index > count(p))
return -1;
if(index==1)
{
q=first;
x=first->data;
first=first->next;
free(q);
return x;
}
else
{
for(i=0;i<index-1;i++)
{
q=p;
p=p->next;
}
q->next=p->next;
x=p->data;
free(p);
return x;
}
}
int main()
{ int a,q,r,s,elem;
int A[]={3,4,5,12,23,26,29,34,37,39,40,42};
create(A,12);
display(first);
printf("\ndo you want to insert any element to the sorted list?\ny=1,n=2\n");
scanf("%d",&r);
while(r==1)
{ printf("\nEnter the data\n ");
scanf("%d",&s);
sortedinsert(first,s);
display(first);
printf("\n\nDo you want to insert another element in the sorted list?\ny=1,n=2\n");
scanf("%d",&r);
}
printf("\n\nDo you want to insert an element in the list?\ny=1,n=2\n");
scanf("%d",&a);
while(a==1){ int b=0,c=0;
printf("\n\nEnter the position you want it to be inserted\n ");
scanf("%d",&b);
printf("\n\nEnter the data you want to insert\n ");
scanf("%d",&c);
insert(first,b,c);
display(first);
printf("\n\nDo you want to insert another element in the list?\ny=1,n=2\n");
scanf("%d",&a);
}
printf("\n\nDo you want to search an element in the list?\ny=1,n=2\n");
scanf("%d",&q);
while(q==1)
{
printf("\nEnter the element\n");
scanf("%d",&elem);
search(first,elem);
printf("\n\nDo you want to search another element in the list?\ny=1,n=2\n");
scanf("%d",&q);
}
printf("\n\nDo you want to delete an element in the list?\ny=1,n=2\n");
scanf("%d",&q);
while(q==1)
{
printf("\nEnter the position\n");
scanf("%d",&elem);
Delete(first,elem);
display(first);
printf("\n\nDo you want to delete another element in the list?\ny=1,n=2\n");
scanf("%d",&q);
}
display(first);
return 0;
}