Skip to content

Commit 505c524

Browse files
committed
Implemented two-way linked list, queue and stack data structures
1 parent 4d2d905 commit 505c524

File tree

4 files changed

+212
-2
lines changed

4 files changed

+212
-2
lines changed

data-structures/OneWayLinkedList.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# With Head only
12
class OneWayLinkedList:
23
class Element:
34
def __init__(self, value):
@@ -50,7 +51,7 @@ def length(self):
5051
return count
5152

5253
def remove(self, index):
53-
if index < 0 or index > self.length():
54+
if index < 0 or index >= self.length():
5455
return None
5556
elem = self.head
5657
temp = 0
@@ -73,7 +74,7 @@ def remove(self, index):
7374
return temp
7475

7576
def add(self, val, index):
76-
if index < 0 or self.length() < index:
77+
if index < 0 or self.length() <= index:
7778
return False
7879
elif index == 0:
7980
elem = self.head

data-structures/Queue.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Since Python doesn't have a built-in array data structure
2+
# I'm going to use a regural list with capacity (to make it interesting)
3+
class Queue:
4+
def __init__(self, capacity=10):
5+
self.list = []
6+
self.capacity = capacity
7+
8+
def enqueue(self, value):
9+
if len(self.list) != self.capacity:
10+
self.list.append(value)
11+
return True
12+
else:
13+
return "QueueOverflowException"
14+
15+
def dequeue(self):
16+
if len(self.list) != 0:
17+
val = self.list.pop(0)
18+
return val
19+
else:
20+
return "IndexOutOfBoundsException"
21+
22+
23+
if __name__ == "__main__":
24+
x = Queue(3)
25+
print(x.enqueue(10))
26+
print(x.enqueue(69))
27+
print(x.enqueue(2137))
28+
print(x.enqueue(423))
29+
print(x.dequeue())
30+
print(x.dequeue())
31+
print(x.dequeue())
32+
print(x.dequeue())

data-structures/Stack.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Since Python doesn't have a built-in array data structure
2+
# I'm going to use a regural list with capacity (to make it interesting)
3+
class Stack:
4+
def __init__(self, capacity=10):
5+
self.list = []
6+
self.capacity = capacity
7+
8+
def push(self, val):
9+
if len(self.list) != self.capacity:
10+
self.list.append(val)
11+
return True
12+
else:
13+
return "StackOverflowException"
14+
15+
def pop(self):
16+
if len(self.list) != 0:
17+
val = self.list.pop()
18+
return val
19+
else:
20+
return "IndexOutOfBoundsException"
21+
22+
23+
if __name__ == "__main__":
24+
x = Stack(3)
25+
print(x.push(10))
26+
print(x.push(69))
27+
print(x.push(2137))
28+
print(x.push(423))
29+
print(x.pop())
30+
print(x.pop())
31+
print(x.pop())
32+
print(x.pop())

data-structures/TwoWayLinkedList.py

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# With Head and Tail
2+
class TwoWayLinkedList:
3+
class Element:
4+
def __init__(self, val):
5+
self.value = val
6+
self.next = None
7+
self.previous = None
8+
9+
def __init__(self):
10+
self.head = None
11+
self.tail = None
12+
13+
def push(self, val):
14+
if self.head is None:
15+
newElem = self.Element(val)
16+
self.head = newElem
17+
self.tail = newElem
18+
else:
19+
newElem = self.Element(val)
20+
self.tail.next = newElem
21+
newElem.previous = self.tail
22+
self.tail = newElem
23+
24+
def print_list(self):
25+
elem = self.head
26+
if elem is None:
27+
return
28+
else:
29+
while elem != self.tail:
30+
print(elem.value)
31+
elem = elem.next
32+
print(self.tail.value)
33+
34+
def print_reverse(self):
35+
elem = self.tail
36+
if elem is None:
37+
return
38+
else:
39+
while elem != self.head:
40+
print(elem.value)
41+
elem = elem.previous
42+
print(self.head.value)
43+
44+
def pop(self):
45+
elem = self.tail
46+
if elem is None:
47+
return None
48+
else:
49+
newTail = elem.previous
50+
val = elem.value
51+
self.tail = newTail
52+
return val
53+
54+
def contains(self, val):
55+
elem = self.head
56+
if elem is None:
57+
return False
58+
else:
59+
while elem:
60+
if elem.value == val:
61+
return True
62+
else:
63+
elem = elem.next
64+
return False
65+
66+
def length(self):
67+
count = 0
68+
elem = self.head
69+
if elem is None:
70+
return 0
71+
else:
72+
while elem:
73+
count += 1
74+
elem = elem.next
75+
return count
76+
77+
def add(self, val, index):
78+
if index < 0 or index >= self.length():
79+
return None
80+
else:
81+
if index == 0:
82+
elem = self.head
83+
newElem = self.Element(val)
84+
newElem.next = elem
85+
elem.previous = newElem
86+
self.head = newElem
87+
return
88+
else:
89+
elem = self.head
90+
ind = 1
91+
while ind != index:
92+
ind += 1
93+
elem = elem.next
94+
newElem = self.Element(val)
95+
if elem.next:
96+
newElem.next = elem.next
97+
newElem.previous = elem
98+
elem.next.previous = newElem
99+
elem.next = newElem
100+
else:
101+
newElem.next = None
102+
newElem.previous = elem
103+
elem.next = newElem
104+
105+
def remove(self, index):
106+
if index < 0 or index >= self.length():
107+
return None
108+
else:
109+
if index == 0:
110+
elem = self.head
111+
val = elem.value
112+
self.head = elem.next
113+
if self.head is None:
114+
self.tail = None
115+
return val
116+
else:
117+
elem = self.head
118+
ind = 0
119+
while ind != index:
120+
ind += 1
121+
elem = elem.next
122+
if self.tail != elem:
123+
val = elem.value
124+
elem.previous.next = elem.next
125+
elem.next.previous = elem.previous
126+
return val
127+
else:
128+
val = elem.value
129+
elem.previous.next = None
130+
self.tail = elem.previous
131+
return val
132+
133+
134+
if __name__ == "__main__":
135+
x = TwoWayLinkedList()
136+
x.push(3)
137+
x.push(69)
138+
x.push(2137)
139+
print(x.contains(69))
140+
print(x.length())
141+
x.print_list()
142+
x.add(32, 1)
143+
x.print_list()
144+
x.remove(3)
145+
x.print_list()

0 commit comments

Comments
 (0)