-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.c
More file actions
214 lines (192 loc) · 5.25 KB
/
Copy pathlink.c
File metadata and controls
214 lines (192 loc) · 5.25 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
#include <stdio.h>
#include <stdlib.h>
void inputFirst(int);
void inputEnd(int);
void inputParticular(int, int);
void deleteFirst();
void deleteEnd();
void deleteParticular(int);
void display();
struct node {
int data;
struct node *ptr;
};
struct node *start;
int main() {
int option, value, position;
printf("The linked list operations are : \n");
printf("1) Enter at the first position\n2) Enter at the end position\n3) Enter at a particular position\n4) Delete from the first position\n5) Delete from the end position\n6) Delete from a particular position\n7) Display\n8) Exit\n");
do {
printf("\nSelect your option : ");
scanf("%d", &option);
switch(option) {
case 1:
printf("Enter the data : ");
scanf("%d", &value);
inputFirst(value);
display();
break;
case 2:
printf("Enter the data : ");
scanf("%d", &value);
inputEnd(value);
display();
break;
case 3:
printf("Enter the data : ");
scanf("%d", &value);
printf("Enter the position : ");
scanf("%d", &position);
inputParticular(value, position);
display();
break;
case 4:
deleteFirst();
display();
break;
case 5:
deleteEnd();
display();
break;
case 6:
printf("Enter the position : ");
scanf("%d", &position);
deleteParticular(position);
display();
break;
case 7:
display();
break;
case 8:
printf("Exited\n");
break;
default:
printf("Invalid option");
}
} while(option!=8);
}
void inputFirst(int value) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node *));
if(start == NULL) {
temp->ptr = NULL;
temp->data = value;
start = temp;
} else {
temp->ptr = start;
temp->data = value;
start = temp;
}
}
void inputEnd(int value) {
struct node *temp, *p;
temp = (struct node *)malloc(sizeof(struct node *));
if(start == NULL) {
temp->ptr = NULL;
temp->data = value;
start = temp;
} else {
p = start;
while(p->ptr != NULL) {
p = p->ptr;
}
p->ptr = temp;
temp->ptr = NULL;
temp->data = value;
}
}
void inputParticular(int value, int position){
struct node *temp, *p;
temp = (struct node *)malloc(sizeof(struct node *));
if(start == NULL) {
temp->ptr = NULL;
temp->data = value;
start = temp;
} else if(position <= 1) {
temp->ptr = start;
temp->data = value;
start = temp;
} else {
p = start;
for(int i=1; i<position-1; i++) {
p = p->ptr;
}
temp->ptr = p->ptr;
p->ptr = temp;
temp->data = value;
}
}
void deleteFirst() {
struct node *temp;
if(start == NULL) {
printf("Cannot delete\n");
} else if(start->ptr == NULL) {
temp = start;
start = NULL;
printf("Deleting data = %d\n", temp->data);
free(temp);
} else {
temp = start;
start = start->ptr;
printf("Deleting data = %d\n", temp->data);
free(temp);
}
}
void deleteEnd() {
struct node *temp;
if(start == NULL) {
printf("Cannot delete\n");
} else if(start->ptr == NULL) {
temp = start;
start = NULL;
printf("Deleting data = %d\n", temp->data);
free(temp);
} else {
struct node *p;
temp = start;
p = start->ptr;
while(p->ptr != NULL){
p = p->ptr;
temp = temp->ptr;
}
temp->ptr = NULL;
printf("Deleting data = %d\n", p->data);
free(p);
}
}
void deleteParticular(int position) {
struct node *temp, *t;
if(start == NULL)
printf("Cannot delete\n");
else if(position==1 && start->ptr==NULL) {
temp = start;
start = NULL;
printf("Deleted data = %d\n", temp->data);
free(temp);
} else if(position == 1) {
temp = start;
start = start->ptr;
printf("Deleted data = %d\n", temp->data);
free(temp);
} else {
temp = start;
for(int i=1; i<position-1; i++) {
temp = temp->ptr;
}
t = temp->ptr;
temp->ptr = t->ptr;
printf("Deleted data = %d\n", t->data);
free(t);
}
}
void display(){
struct node *temp;
if(start == NULL){
printf("List is empty\n");
} else {
printf("\nThe linked list : \n");
for(temp=start; temp!=NULL; temp=temp->ptr)
printf("%d ", temp->data);
printf("\n");
}
}