Repository navigation
Expand file tree
/
Copy pathlinkedlist.c
More file actions
225 lines (206 loc) · 5.86 KB
/
Copy pathlinkedlist.c
File metadata and controls
225 lines (206 loc) · 5.86 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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *head, *ptr, *temp, *newnode;
void insertbeginning()
{
newnode=(struct node*) malloc (sizeof(struct node));
printf("Enter the element to be added: ");
scanf("%d",&newnode->data);
if(head==NULL)
{
newnode->next = NULL;
head=newnode;
}
else
{
newnode->next = head;
head=newnode;
}
}
void insertend()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element to be added");
scanf("%d",&newnode->data);
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=NULL;
}
void insertany()
{
int pos;
printf("Enter the position where the element is to be added: ");
scanf("%d", &pos);
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter the element to be added: ");
scanf("%d", &newnode->data);
temp = head;
if (pos == 1)
{
newnode->next = head;
head = newnode;
return;
}
for (int i = 1; i < pos - 1 && temp != NULL; i++)
{
temp = temp->next;
}
if (temp == NULL)
{
printf("Invalid position.\n");
return;
}
}
void deletebeginning()
{
if(head->next==NULL)
{
temp=head;
head=NULL;
printf("\nThe deleted element is: %d",temp->data);
free(temp);
}
else if(head==NULL)
{
printf("\nEmpty list");
}
else
{
temp=head;
head=head->next;
printf("\nDeleted element is: %d",temp->data);
free(temp);
}
}
void deleteend()
{
if(head->next==NULL)
{
temp=head;
head=NULL;
printf("\nThe deleted element is: %d",temp->data);
free(temp);
}
else if(head==NULL)
{
printf("\nEmpty");
}
else
{
temp=head;
ptr=temp->next;
while(ptr->next!=NULL)
{
ptr=ptr->next;
temp=temp->next;
}
printf("\nDeleted element is: %d",ptr->data);
free(ptr);
}
}
void deleteany()
{
int pos;
printf("Enter the position from where the element is to be deleted");
scanf("%d",&pos);
if(pos==1 && head==NULL)
{
temp=head;
head=NULL;
printf("\nThe deleted element is: %d",temp->data);
free(temp);
}
else if(head==NULL)
{
printf("\nEmpty");
}
else if(pos==1)
{
temp=head;
head=head->next;
printf("\nDeleted element is: %d",temp->data);
free(temp);
}
else
{
temp=head;
int i=0;
for(i=0;i<pos-1;i++)
{
temp=temp->next;
}
ptr=temp->next;
temp->next=ptr->next;
printf("\n Deleted element is: %d",ptr->data);
free(ptr);
}
}
void display()
{
if(head==NULL)
{
printf("\nEmpty");
}
else
{
printf("\nThe elements in the list are:");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("\t%d",temp->data);
}
printf("\n");
}
}
int main()
{
int n,i=1;
printf("Enter the operation to be done.\n");
while(i!=0)
{
printf("1:Insertion at beginning.\n2:Insertion at end.\n3.Insertion at any position.\n4.Deletion from beginng.\n5.Deletion from end.\n6.Deletion from any position.\n7.Display the list.\nChoice:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Inserting the element at the beginning\n");
insertbeginning();
break;
case 2:
printf("Inserting the element at the end\n");
insertend();
break;
case 3:
printf("Inserting the element at the required position\n");
insertany();
break;
case 4:
printf("Deleting the element from the beginning\n");
deletebeginning();
break;
case 5:
printf("Deleting the element from the end\n");
deleteend();
break;
case 6:
printf("Deleting the element from the entered position\n");
deleteany();
break;
case 7:
printf("The final linked list is: ");
display();
break;
default:
printf("Invalid choice\n");
}
i++;
}
}