-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
307 lines (282 loc) · 10.1 KB
/
main.py
File metadata and controls
307 lines (282 loc) · 10.1 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Entry point of the project
import sys
from stack_module import Stack
from queue_module import Queue
from linked_list_module import LinkedList
from tree_module import BinaryTree
from utils import Utils
utils = Utils()
def main_menu():
while(True):
utils.clear_screen()
utils.show_info("Main Menu")
print("1: Stack")
print("2: Queue")
print("3: Linked List")
print("4: Binary Tree")
print("5: Sorting")
print("6: Searching")
choice = input("Select Option [or Enter to Exit]: ")
if (choice == "1"):
utils.clear_screen()
stack_menu()
elif (choice == "2"):
utils.clear_screen()
queue_menu()
elif (choice == "3"):
utils.clear_screen()
linked_list_menu()
elif (choice == "4"):
utils.clear_screen()
binary_tree_menu()
elif (choice == "5"):
utils.clear_screen()
sorting_algorithm_menu()
elif (choice == "6"):
utils.clear_screen()
searching_algorithms_menu()
elif (choice == ""):
utils.clear_screen()
sys.exit(0)
else:
print("Invalid Option")
def stack_menu():
stack = Stack()
while (True):
utils.show_info("Stack Operations")
print("1: Push")
print("2: Pop")
print("3: View Stack")
print("4: Peek")
print("5: Back")
choice = input("Select Option: ")
if (choice == "1"):
utils.clear_screen()
while (True):
value = input("Enter Value to Push: ")
if (value != ""):
utils.clear_screen()
stack.push(value)
break
else:
utils.clear_screen()
utils.show_error("Value SHOULD NOT be empty")
elif (choice == "2"):
utils.clear_screen()
stack.pop()
elif (choice == "3"):
utils.clear_screen()
stack.view()
elif (choice == "4"):
utils.clear_screen()
stack.peek()
elif (choice == "5"):
break
else:
utils.clear_screen()
utils.clear_screen("Invalid Option")
def queue_menu():
queue = Queue()
while (True):
utils.show_info("Queue Operations")
print("1: Enqueue")
print("2: Dequeue")
print("3: View Queue")
print("4: Back")
choice = input("Select Option: ")
if (choice == "1"):
utils.clear_screen()
while (True):
value = input("Enter Value to Enqueue: ")
if (value != ""):
utils.clear_screen()
queue.enqueue(value)
break
else:
utils.clear_screen()
utils.show_error("Value SHOULD NOT be empty")
elif (choice == "2"):
utils.clear_screen()
queue.dequeue()
elif (choice == "3"):
utils.clear_screen()
queue.view()
elif (choice == "4"):
break
else:
utils.clear_screen()
utils.show_error("Invalid Option")
def linked_list_menu():
linked_list = LinkedList()
while (True):
utils.show_info("Linked List Operations")
print("1: Insert at Start")
print("2: Insert at End")
print("3: Delete Node")
print("4: View Linked List")
print("5: Back")
choice = input("Select Option: ")
if (choice == "1"):
utils.clear_screen()
while (True):
value = input("Enter Value to Insert at Start: ")
if (value != ""):
utils.clear_screen()
linked_list.insert_at_start(value)
break
else:
utils.clear_screen()
utils.show_error("Value SHOULD NOT be empty")
elif (choice == "2"):
utils.clear_screen()
while (True):
value = input("Enter Value to Insert at End: ")
if (value != ""):
utils.clear_screen()
linked_list.insert_at_end(value)
break
else:
utils.clear_screen()
utils.show_error("Value SHOULD NOT be empty")
elif (choice == "3"):
utils.clear_screen()
while (True):
if(linked_list.list_is_empty()):
utils.show_error("There's Nothing to Delete")
break
utils.show_info(f"Available Values to Delete: {linked_list.current_list()}")
value = input("Enter Value to Delete: ")
if (value != ""):
utils.clear_screen()
linked_list.delete_node(value)
break
else:
utils.clear_screen()
utils.show_error("Value SHOULD NOT be empty")
elif (choice == "4"):
utils.clear_screen()
linked_list.view()
elif (choice == "5"):
break
else:
utils.clear_screen()
utils.show_error("Invalid Option")
def binary_tree_menu():
binary_tree = BinaryTree()
while True:
utils.show_info("Binary Tree Operations")
print("1: Insert Node")
print("2: Inorder Traversal")
print("3: Preorder Traversal")
print("4: Postorder Traversal")
print("5: Search Tree")
print("6: Back")
choice = input("Select Option: ")
if (choice == "1"):
utils.clear_screen()
while True:
value = input("Enter Value to Insert: ")
if (value != ""):
utils.clear_screen()
binary_tree.insert(value)
break
else:
utils.clear_screen()
utils.show_error("Value SHOULD NOT be empty")
elif (choice == "2"):
utils.clear_screen()
binary_tree.inorder_traversal()
elif (choice == "3"):
utils.clear_screen()
binary_tree.preorder_traversal()
elif (choice == "4"):
utils.clear_screen()
binary_tree.postorder_traversal()
elif (choice == "5"):
utils.clear_screen()
while True:
value = input("Enter Node to Search: ")
if (value != ""):
utils.clear_screen()
binary_tree.search_tree(value)
break
else:
utils.clear_screen()
utils.show_error("Value SHOULD NOT be empty")
elif (choice == "6"):
break
else:
utils.clear_screen()
utils.show_error("Invalid Option")
def sorting_algorithm_menu():
while (True):
utils.show_info("Sorting Algorithms")
arr = [45, 23, 54, 77, 33, 16, 88, 2, 27]
print("1: Bubble Sort")
print("2: Selection Sort")
print("3: Insertion Sort")
print("4: Quick Sort")
print("5: Merge Sort")
print("6: Back")
choice = input("Select Option: ")
if (choice in ["1", "2", "3", "4", "5"]):
utils.clear_screen()
utils.show_warning(f"Original Array: {arr}")
if (choice == "1"):
from algorithms.sorting import bubble_sort
utils.show_success(f"Bubble Sorted Array: {bubble_sort(arr)}")
elif (choice == "2"):
from algorithms.sorting import selection_sort
utils.show_success(f"Selection Sorted Array: {selection_sort(arr)}")
elif (choice == "3"):
from algorithms.sorting import insertion_sort
utils.show_success(f"Insertion Sorted Array: {insertion_sort(arr)}")
elif (choice == "4"):
from algorithms.sorting import quick_sort
utils.show_success(f"Quick Sorted Array: {quick_sort(arr)}")
elif (choice == "5"):
from algorithms.sorting import merge_sort
utils.show_success(f"Merge Sorted Array: {merge_sort(arr)}")
elif (choice == "6"):
break
else:
utils.clear_screen()
utils.show_error("Invalid Option")
def searching_algorithms_menu():
while (True):
utils.show_info("Searching Algorithms")
arr = [45, 23, 54, 77, 33, 16, 88, 2, 27]
print("1: Linear Search")
print("2: Binary Search")
print("3: Jump Search")
print("4: Interpolation Search")
print("5: Back")
choice = input("Select Option: ")
if (choice in ["1", "2", "3", "4"]):
utils.clear_screen()
utils.show_info(f"Array: {arr}")
value = int(input("Enter Value to Search from List above: "))
if (choice == "1"):
from algorithms.searching import linear_search
result = linear_search(arr, value)
elif (choice == "2"):
from algorithms.searching import binary_search
result = binary_search(arr, value)
elif (choice == "3"):
from algorithms.searching import jump_search
result = jump_search(arr, value)
elif (choice == "4"):
from algorithms.searching import interpolation_search
result = interpolation_search(arr, value)
utils.clear_screen()
if (result >= 0):
utils.show_success(f"{value} found at index {result}")
else:
utils.show_error(f"{value} not found")
elif (choice == "5"):
break
else:
utils.clear_screen()
utils.show_error("Invalid Option")
if __name__ == "__main__":
main_menu()