-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prac-2.c
142 lines (116 loc) · 3.09 KB
/
Prac-2.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
// P2: Wap to implement LL and CLL ops: [insert, delete] at start, end, index
// Headers
#include <string.h>
#include "headers/LL.h" // required
#include "headers/Utils.h"
// Declarations
int opsMenu(char *);
void mainMenu(bool);
// Main
int main()
{
int choice;
do
{
system("cls");
choice = input(
"Select List Type:\n"
"1. Linear Single Linked List.\n"
"2. Circular Single Linked List.\n"
"3. Exit.\n");
switch (choice)
{
case 1: // same menu for both
case 2: // linear & circular
mainMenu(choice == 2);
break;
case 3:
exit(0);
default:
break;
}
} while (1);
}
/// @brief Main Menu Driver Code
/// @param isCircular whether linked list is circular or linear
void mainMenu(bool isCircular)
{
Node *list = NULL;
int op, pos, data, after;
do
{
system("cls");
op = opsMenu(isCircular ? "Circular" : "Linear");
switch (op)
{
case 1: // Insert
pos = posMenu("Insert");
if (pos < 0 || pos > 3)
continue;
data = input("\nEnter Data: ");
switch (pos)
{
case 1: // Insert at Start
insertAtStart(&list, data, isCircular);
break;
case 2: // Insert After ?
traverse(list);
after = input("\nInsert Node After (value): ");
insertAfter(&list, data, after, isCircular);
break;
case 3: // Insert at End
insertAtEnd(&list, data, isCircular);
break;
default:
break;
}
traverse(list);
break;
case 2: // Remove
pos = posMenu("Remove");
if (pos < 0 || pos > 3)
continue;
switch (pos)
{
case 1: // Remove from Start
removeFromStart(&list, isCircular);
break;
case 2: // Remove After ?
after = input("\nRemove Node After (value): ");
removeAfter(&list, after, isCircular);
break;
case 3: // Remove from End
removeFromEnd(&list, isCircular);
break;
default:
break;
}
traverse(list);
break;
case 3: // Traverse
traverse(list);
break;
case 4:
return;
case 5:
exit(0);
default:
continue;
}
printf("\n");
system("pause");
} while (1);
}
/// @brief Operations Menu Driver Code
/// @param listType Linked List type: Linear | Circular
/// @return Operation choice[1-3]: Insert | Remove | Traverse etc.
int opsMenu(char *listType)
{
printf("%s Single Linked List Menu:\n", listType);
return input(
"1. Insert\n"
"2. Remove\n"
"3. Traverse\n"
"4. Back\n"
"5. Exit\n");
}