-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
157 lines (121 loc) · 3.69 KB
/
main.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
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
class Node:
def __init__(self, data=None, next=None, prev=None):
self.data = data
self.next = next
self.prev = prev
class DoublyLinkedList:
def __init__(self):
self.head = None
def print_forward(self):
if self.head is None:
print("Linked list is empty")
return
itr = self.head
count = 1
while itr:
print(f"\t{count}) {itr.data}")
itr = itr.next
count += 1
def print_backward(self):
if self.head is None:
print("Linked list is empty")
return
last_node = self.get_last_node()
itr = last_node
count = 1
while itr:
print(f"\t{count}) {itr.data}")
itr = itr.prev
count += 1
def get_last_node(self):
itr = self.head
while itr.next:
itr = itr.next
return itr
def get_length(self):
count = 0
itr = self.head
while itr:
count+=1
itr = itr.next
return count
def insert_at_begining(self, data):
if self.head == None:
node = Node(data, self.head, None)
self.head = node
else:
node = Node(data, self.head, None)
self.head.prev = node
self.head = node
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None, itr)
def insert_at(self, index, data):
if index<0 or index>self.get_length():
raise Exception("Invalid Index")
if index==0:
self.insert_at_begining(data)
return
count = 0
itr = self.head
while itr:
if count == index - 1:
node = Node(data, itr.next, itr)
if node.next:
node.next.prev = node
itr.next = node
break
itr = itr.next
count += 1
def remove_at(self, index):
if index<0 or index>=self.get_length():
raise Exception("Invalid Index")
if index==0:
self.head = self.head.next
self.head.prev = None
return
count = 0
itr = self.head
while itr:
if count == index:
itr.prev.next = itr.next
if itr.next:
itr.next.prev = itr.prev
break
itr = itr.next
count+=1
def insert_values(self, data_list):
self.head = None
for data in data_list:
self.insert_at_end(data)
dl = DoublyLinkedList()
dl.insert_values([25, 50, 40, 83])
while True:
print("\n\nSelect your option:\n1) View transactions in newest order\n2) View transactions in oldest order\n3) Add transaction\n4) Remove transaction\n5) Exit")
choice = int(input("\nChoice: "))
if choice == 1:
print("\nTransactions in newest order:")
dl.print_forward()
elif choice == 2:
print("\nTransactions in oldest order:")
dl.print_backward()
elif choice == 3:
amt = int(input("Enter amount spent: "))
idx = int(input("Enter index to add at: "))
dl.insert_at(idx, amt)
print(f"\nAdded {amt} at index {idx}")
elif choice == 4:
idx = int(input("Enter index to remove transaction: "))
dl.remove_at(idx)
print(f"\nRemoved transaction at index {idx}")
elif choice == 5:
print("\nExiting")
break
else:
print("\nInvalid choice")
continue