-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsinglyLL.c
More file actions
195 lines (195 loc) · 4.09 KB
/
singlyLL.c
File metadata and controls
195 lines (195 loc) · 4.09 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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *start = NULL;
void insertAtFront()
{
int data;
struct node *temp;
temp = malloc(sizeof(struct node));
printf("\nEnter value to insert: ");
scanf("%d", &data);
temp->info = data;
temp->link = start;
start = temp;
printf("Element inserted\n");
}
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));
printf("\nEnter value to insert: ");
scanf("%d", &data);
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL)
{
head = head->link;
}
head->link = temp;
printf("Element inserted\n");
}
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc(sizeof(struct node));
printf("\nEnter value to insert and position to insert at: ");
scanf("%d %d", &data, &pos);
temp = start;
newnode->info = data;
newnode->link = 0;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
printf("Element inserted\n");
}
void deleteFirst()
{
struct node *temp;
if (start == NULL)
{
printf("\nThe list is empty, choose a different option\n");
}
else
{
temp = start;
start = start->link;
free(temp);
printf("\nElement deleted\n");
}
}
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
{
printf("\nThe list is empty, choose a different option\n");
}
else
{
temp = start;
while (temp->link != 0)
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
printf("\nElement deleted\n");
}
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;
if (start == NULL)
{
printf("\nThe list is empty, choose a different option\n");
}
else
{
printf("\nEnter the position to delete: ");
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
position = temp->link;
temp->link = position->link;
free(position);
printf("Element deleted\n");
}
}
void displayList()
{
struct node *temp;
if (start == NULL)
{
printf("\nThe list is empty, choose a different option\n");
}
else
{
temp = start;
printf("\n%d <-- Start", temp->info);
temp = temp->link;
while ((temp->link) != NULL)
{
printf("\n%d", temp->info);
temp = temp->link;
}
printf("\n%d <-- End\n", temp->info);
}
}
int main()
{
int choice;
printf("MENU DRIVEN LINKED LIST\n");
printf("\n1. Display current list\n2. Insert an element at the front\n3. Insert an element at the end\n4. Insert an element at a given position\n5. Delete the element at the front\n6. Delete the element at the end\n7. Delete the element at a given position\n8. Exit\n ");
do
{
printf("\n Enter the Choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
displayList();
break;
}
case 2:
{
insertAtFront();
break;
}
case 3:
{
insertAtEnd();
break;
}
case 4:
{
insertAtPosition();
break;
}
case 5:
{
deleteFirst();
break;
}
case 6:
{
deleteEnd();
break;
}
case 7:
{
deletePosition();
break;
}
case 8:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf("Invalid choice, try again");
}
}
} while(choice != 8);
return 0;
}