-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path_5.LinkedListOperations
240 lines (231 loc) · 5.19 KB
/
_5.LinkedListOperations
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head,*newnode,*temp;
struct node* head=0;
//functionToFindNumberOfNodes
int length()
{
int count=0;
struct node *temp;
temp=head;
while(temp!=0)
{
count++;
temp=temp->next;
}
printf("length is %d \n",count);
return count;
}
//functionToInsertFromBeginning
void insertbeg()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=head;
head=newnode;
display();
}
//functionToInsertFromEnd
void insertend()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=0;
temp=head;
while(temp->next !=0)
{
temp=temp->next;
}
temp->next=newnode;
display();
}
//functionToInsertAtAnyPosition
void insertpos()
{
int pos, item, i = 1;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the position at which INSERTION has to be done \n ");
scanf("%d", &pos);
printf("\nEnter the ITEM to be inserted \n");
scanf("%d", &newnode->data);
temp=head;
newnode->next=0;
int l=length();
if(pos>l)
{
printf("Invalid position \n");
}
else
{
while (i < pos - 1)
{
temp = temp->next;
i++;
}
newnode->next = temp->next;
temp->next =newnode;
printf("\nItem inserted\n");
}
display();
}
//functionToDeleteFromFront
void deletefront()
{
if (head == NULL)
printf("\nList is empty\n");
else
{
temp = head;
head = head->next;
int x = temp->data;
printf("\nDeleted item is %d \n",x);
free(temp);
}
display();
}
//functionToDeleteFromEnd
void deleteend()
{
struct node *temp, *prevnode;
if (head == NULL)
printf("\nList is Empty\n");
else
{
temp = head;
while (temp->next != 0)
{
prevnode = temp;
temp = temp->next;
}
int x = temp->data;
printf("\nDeleted item is %d \n",x);
free(temp);
prevnode->next = 0;
}
display();
}
//functionToDeleteFromAnyPosition
void deletepos()
{
struct node *temp, *position;
int i = 1, pos;
if (head == NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter index : ");
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = head;
int l=length();
if(pos<1 || pos>l)
{
printf("\tInvalid Position");
}
else
{
if(pos==1)
{
deletefront();
}
else
{
while (i < pos - 1)
{
temp = temp->next;
i++;
}
position = temp->next;
temp->next= position->next;
int x = position->data;
printf("\nDeleted item is %d \n",x);
free(position);
}
}
}
display();
}
//functionToDisplayLinkedList
void display()
{
temp=head;
while(temp!=0)
{
printf("%d \t",temp->data);
temp=temp->next;
}
printf("\n");
}
//mainFunctin
int main()
{
int choice;
//LinkedListCreation
while(choice)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
printf("Do you want to contnue(0,1)?");
scanf("%d",&choice);
}
temp=head;
while(temp!=0)
{
printf("%d \t",temp->data);
temp=temp->next;
}
int opt;
printf("\nLinked List Operations\n");
while (1)
{
printf("\t1 Insertion at beginning\n");
printf("\t2 Insertion at end\n");
printf("\t3 Insertion at any position\n");
printf("\t4 Deletion at begining\n");
printf("\t5 Deletion at end\n");
printf("\t6 Deletion of element at any position\n");
printf("\t7 Display the list\n");
printf("\t8 To exit\n");
printf("\nEnter Option :");
scanf("%d", &opt);
switch (opt)
{
case 1:insertbeg();
break;
case 2:insertend();
break;
case 3:insertpos();
break;
case 4:deletefront();
break;
case 5:deleteend();
break;
case 6:deletepos();
break;
case 7:display();
break;
case 8:exit(1);
break;
default:printf("Incorrect Option\n");
}
}
return 0;
}