-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList traversal.py
80 lines (57 loc) · 2.15 KB
/
List traversal.py
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
def insert_first_element(my_list):
first_element = int(input("Enter first number to insert in the list: "))
my_list.insert(0, first_element)
return my_list
def insert_at_last(my_list):
last_element = int(input("Enter a number to insert in the list: "))
my_list.append(last_element)
return my_list
def delete_last_element(my_list):
my_list.pop()
return my_list
def delete_first_element(my_list):
my_list.pop(0)
return my_list
def insert_at_specific_position(my_list):
position = int(input("Enter the index of the element: "))
element = int(input("Enter the element to be inserted: "))
my_list.insert(position, element)
return my_list
def delete_at_specific_position(my_list):
position = int(input("Enter the index of the element: "))
element = int(input("Enter the element to be inserted: "))
my_list.pop(position)
return my_list
mylist = []
print("Enter 1 to to insert first element in the list")
print("Enter 2 to to insert at the end of the list")
print("Enter 3 to insert at a specific position in the list")
print("Enter 4 to delete the first element in the list")
print("Enter 5 to delete the last element in the list")
print("Enter 6 to delete at a specific position in the list")
print("Enter 0 to exit the program")
select = int(input())
while select != 0:
if select == 1:
insert_first_element(mylist)
elif select == 2:
insert_at_last(mylist)
elif select == 3:
insert_at_specific_position(mylist)
elif select == 5:
delete_last_element(mylist)
elif select == 4:
delete_first_element(mylist)
elif select == 6:
delete_last_element(mylist)
elif select == 0:
break
print(mylist)
print("Enter 1 to to insert first element in the list")
print("Enter 2 to to insert at the end of the list")
print("Enter 3 to insert at a specific position in the list")
print("Enter 4 to delete the first element in the list")
print("Enter 5 to delete the last element in the list")
print("Enter 6 to delete at a specific position in the list")
print("Enter 0 to exit the program")
select = int(input())