-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinkedlist1.c
229 lines (208 loc) · 3.77 KB
/
linkedlist1.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
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
// Linked list.
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *new1;
int noe=0;
void insertatbeg()
{
if(start==NULL)
{
start = new1;
start->next=NULL;
}
else
{
new1->next = start;
start=new1;
}
};
void insertatend()
{
struct node *temp;
if(start==NULL)
{
new1->next=NULL;
start=new1;
}
else
{
new1->next=NULL;
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next = new1;
}
};
void insertatmid()
{
struct node *temp,*prev;
int pos,i;
if(start==NULL)
{
printf("\nThere are no elements present in list to insert in middle..\n");
return;
}
printf("\nEnter the position in which you want to enter: ");
scanf("%d",&pos);
if(pos==1 || pos==0 || pos>=noe)
{
printf("\nPlease Enter a valid position\n");
return;
}
temp=start;
for(i=0;i<pos-1;i++)
{
prev=temp;
temp=temp->next;
}
new1->next=temp;
prev->next=new1;
};
void deletefrombeg()
{
struct node *temp;
if(start==NULL)
printf("\nList is Empty!!\n");
else
{
temp=start;
start= temp->next;
printf("\nDeleted element is : %d\n",temp->data);
free(temp);
}
};
void deletefromend()
{
struct node *temp=start;
if(start==NULL)
printf("\nToo less element to Delete!");
else
{
while(temp->next!=NULL)
temp=temp->next;
printf("Delted Element is : %d\n",temp->next->data);
temp->next=NULL;
}
};
void deletefrommid()
{
struct node *temp, *prev;
int i, pos;
if(start==NULL)
{
printf("\nToo less data to delete\n");
return;
}
printf("\nEnter the node position to delete: ");
scanf("%d",&pos);
if(pos==1 || pos==0 || pos>noe)
{
printf("\nPlease Enter Valid POsition\n");
return ;
}
temp = start;
for(i=0;i<pos-1;i++)
{
prev=temp;
temp=temp->next;
}
prev->next = temp->next;
printf("\n Deleted element is %d\n",temp->data);
free(temp);
};
void display()
{
struct node *temp1;
if(start==NULL)
printf("\nList is Empty\n");
else
{
for(temp1=start;temp1!=NULL;temp1=temp1->next)
printf("%d-->",temp1->data);
printf("NULL\n");
}
};
int main()
{
int ch1 , ch2 , ch3;
char ch4;
printf("//////////////////////LINKED LIST IMPLEMENTATION///////////////////////\n\n\n");
do{
printf("\n1.Insert Data\n");
printf("2.Delete Data\n");
printf("3.Display\n");
printf("Enter your choice:- ");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
{
noe++;
printf("\nEnter the data: ");
new1 = (struct node *) malloc(sizeof(struct node));
scanf("%d",&new1->data);
printf("\n1.Insert at Beginning\n");
printf("2.Insert at End\n");
printf("3.Insert at Middle\n");
printf("\nEnter your choice:- ");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
insertatbeg();
break;
case 2:
insertatend();
break;
case 3:
insertatmid();
break;
default:
exit(0);
}
break;
}
case 2:
{
printf("\n1.Delete from Front\n");
printf("2.Delete from End\n");
printf("3.Delete from Middle\n");
printf("Enter your choice:- ");
scanf("%d",&ch3);
switch(ch3)
{
case 1:
deletefrombeg();
break;
case 2:
deletefromend();
break;
case 3:
deletefrommid();
break;
default:
exit(0);
}
break;
}
case 3:
{
display();
break;
}
default:
exit(0);
}
printf("\nWant to continue?Y or N \n");
ch4 = getch();
}
while(ch4=='y' || ch4=='Y');
return 0;
}