-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathlinkedlistinC.c
More file actions
248 lines (237 loc) · 3.72 KB
/
linkedlistinC.c
File metadata and controls
248 lines (237 loc) · 3.72 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*mid,*last;
void createlist();
void insertfirst(int);
void insertlast(int);
void insertmiddle(int,int);
void delfirst();
void dellast();
void delmiddle(int);
void search();
void reverselist();
void printlist();
void main()
{
clrscr() ;
printf("-----SINGLE LINKED LIST-----\n");
printf("elements in the list are: ");
createlist();
printlist();
printf("\ninsert the element 100 at beginning\n");
insertfirst(100);
printlist();
printf("\n");
printf("\ninsert the element 200 at last\n");
insertlast(200);
printlist();
printf("\n");
printf("\ninsert the element 300 at the 3 position\n ");
insertmiddle(300,3);
printlist();
printf("\n");
printf("\ndeleting the first node\n" );
delfirst();
printlist();
printf("\n");
printf("\ndeleting the last node\n");
dellast();
printlist();
printf("\n");
printf("\ndeleting the node at position 3\n");
delmiddle(3);
printlist();
printf("\n");
printf("\nsearching of element\n");
search();
printf("\n");
printf("\nReversing the list\n");
reverselist();
printlist();
getch();
}
void createlist()
{
head=malloc(sizeof(struct node));
mid=malloc(sizeof(struct node));
last=malloc(sizeof(struct node));
head->data=10;
mid->data=20;
last->data=30;
head->next=mid;
mid->next=last;
last->next=NULL;
}
void insertfirst(int x)
{
struct node *newnode=malloc(sizeof(struct node));
newnode->data=x;
newnode->next=head;
head=newnode;
}
void insertlast(int x)
{
struct node *newnode=malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void insertmiddle(int x,int pos)
{
int i;
struct node *temp;
struct node *newnode=malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
temp=head;
for(i=1;i<pos-1;i++)
{
temp=temp->next;
if(temp==NULL)
{
break;
}
}
if(temp!=NULL)
{
newnode->next=temp->next;
temp->next=newnode;
}
else{
printf("\nthe position does not exist\n");
}
}
void delfirst()
{
struct node *todelete=head;
printf("data deleted is: %d\n",todelete->data);
if(head==NULL)
{
printf("list is empty");
}
else
{
head=head->next;
free(todelete);
}
}
void dellast()
{
struct node *todelete=head;
struct node *prevnode;
if(head==NULL)
{
printf("the list is empty cannot delete");
}
else
{
while(todelete->next!=NULL)
{
prevnode=todelete;
todelete=todelete->next;
}
printf("element to be deleted:%d\n",todelete->data);
prevnode->next=NULL;
free(todelete);
}
}
void delmiddle(int pos)
{
int i;
struct node *todelete,*prevnode;
todelete=head;
prevnode=head;
for(i=1;i<pos;i++)
{
prevnode=todelete;
todelete=todelete->next;
if(todelete==NULL)
{
break;
}
}
if(todelete->next!=NULL)
{
prevnode->next=todelete->next;
todelete->next=NULL;
free(todelete);
}
else
{
printf("the position does not exist");
}
}
void search()
{
struct node *temp;
int a,i=0,flag;
temp=head;
if(temp==NULL)
{
printf("the list is empty");
}
else
{
printf("\nenter the element to be searched:\n");
scanf("%d",&a);
while(temp!=NULL)
{
if(temp->data==a)
{
printf("element found at location:%d",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
temp=temp->next;
}
}
if(flag==1)
{
printf("the element not found");
}
}
void reverselist()
{
struct node *prev,*current,*next;
current=head;
prev=NULL;
while(current!=NULL)
{
next=current->next;
current->next=prev;
prev=current;
current=next;
}
head=prev;
}
void printlist()
{
struct node*temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}