-
Notifications
You must be signed in to change notification settings - Fork 0
/
46john15.c
195 lines (179 loc) · 5.03 KB
/
46john15.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
/*
Roll no:46
Batch:B
Author Name:John Joy
Date:2/09/23
Description: Program to implement insert, delete, display, search in linked list
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *next;
};
// Function to allocate memory for a new node
struct node *getnode(void) {
return (struct node *)malloc(sizeof(struct node));
}
// Function to free memory for a given node
void freenode(struct node *p) {
free(p);
}
struct node *list = NULL; // Initialize the linked list
// Function to insert an element at the beginning of the linked list
void insertbeg(int x) {
struct node *newnode = getnode();
newnode->info = x;
newnode->next = list;
list = newnode;
}
// Function to insert an element at the end of the linked list
void insertend(int x) {
if (list == NULL) {
insertbeg(x);
} else {
struct node *nn, *temp;
nn = getnode();
nn->info = x;
nn->next = NULL;
temp = list;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = nn;
}
}
// Function to display the linked list
void display() {
struct node *temp = list;
if (temp == NULL) {
printf("The list is empty\n");
return;
}
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->info);
temp = temp->next;
}
printf("NULL\n");
}
// Function to search for an element in the linked list
int search(int key) {
struct node *temp = list;
int position = 1;
while (temp != NULL) {
if (temp->info == key) {
return position;
}
temp = temp->next;
position++;
}
return -1; // Element not found
}
// Function to insert an element at a specific location in the linked list
void insertatlocation(int p, int x) {
if (p <= 0) {
printf("Invalid position\n");
return;
}
struct node *nn, *temp;
nn = getnode();
nn->info = x;
if (p == 1) {
nn->next = list;
list = nn;
} else {
temp = list;
for (int i = 1; i < p - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Invalid position\n");
freenode(nn); // Free the allocated node
} else {
nn->next = temp->next;
temp->next = nn;
}
}
}
// Function to delete an element at a specific location in the linked list
void deleteatlocation(int p) {
struct node *temp, *t2;
if (list == NULL) {
printf("List Is Empty\n");
} else {
if (p == 1) {
t2 = list;
list = list->next;
freenode(t2);
} else {
temp = list;
for (int i = 1; i < p - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Invalid position\n");
} else {
t2 = temp->next;
temp->next = t2->next;
freenode(t2);
}
}
}
}
int main() {
int choice, x, position;
while (1) {
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Insert at location\n");
printf("4. Display\n");
printf("5. Search\n");
printf("6. Delete at location\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to insert at the beginning: ");
scanf("%d", &x);
insertbeg(x);
break;
case 2:
printf("Enter element to insert at the end: ");
scanf("%d", &x);
insertend(x);
break;
case 3:
printf("Enter position to insert: ");
scanf("%d", &position);
printf("Enter element to insert: ");
scanf("%d", &x);
insertatlocation(position, x);
break;
case 4:
display();
break;
case 5:
printf("Enter element to search: ");
scanf("%d", &x);
position = search(x);
if (position == -1) {
printf("Element not found in the list\n");
} else {
printf("Element found at position %d\n", position);
}
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteatlocation(position);
break;
case 7:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}