-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLinkedListStack.py
43 lines (32 loc) · 1.01 KB
/
LinkedListStack.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
from LinkedList import LinkedList
from base import StackBase
class LinkedListStack(StackBase):
def __init__(self):
self._list = LinkedList()
def get_size(self):
return self._list.get_size()
def is_empty(self):
return self._list.is_empty()
def push(self, e):
self._list.add_first(e)
def pop(self):
return self._list.remove_first()
def peek(self):
return self._list.get_first()
def __str__(self):
curr = self._list._dummy_head.next
data = []
while curr:
data.append(str(curr.e))
curr = curr.next
return '<chapter_06_LinkedList.linkedlist_stack.LinkedListStack>: (Top) ' + ' -> '.join(data)
def __repr__(self):
return self.__str__()
if __name__ == '__main__':
linkedlist_stack = LinkedListStack()
linkedlist_stack.push(1)
linkedlist_stack.push(3)
linkedlist_stack.push(999)
print(linkedlist_stack)
print(linkedlist_stack.pop())
print(linkedlist_stack)