Repository navigation
Expand file tree
/
Copy pathstack.py
More file actions
56 lines (45 loc) · 1.58 KB
/
Copy pathstack.py
File metadata and controls
56 lines (45 loc) · 1.58 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
"""
A stack is a data structure whose primary purpose is to store and
return elements in Last In First Out order.
1. Implement the Stack class using an array as the underlying storage structure.
Make sure the Stack tests pass.
2. Re-implement the Stack class, this time using the linked list implementation
as the underlying storage structure.
Make sure the Stack tests pass.
3. What is the difference between using an array vs. a linked list when
implementing a Stack?
"""
# class Stack:
# def __init__(self):
# self.size = 0
# # self.storage = ?
# self.storage = []
# def __len__(self):
# return self.size
# def push(self, value):
# self.size +=1
# return self.storage.append(value)
# def pop(self):
# if self.size != 0:
# self.size -= 1
# return self.storage.pop()
# 2. Re-implement the Stack class, this time using the linked list implementation
# as the underlying storage structure.
# Make sure the Stack tests pass.
from singly_linked_list import LinkedList
class Stack():
"""Linked list as the underlying storage structure"""
def __init__(self):
self.size = 0
self.storage = LinkedList()
def __len__(self):
return self.size
def push(self, value):
self.size += 1
return self.storage.add_to_tail(value)
def pop(self):
#!= : this mean there is no elements in the list
if self.size != 0:
# -= : removing one element at a time.
self.size -= 1
return self.storage.remove_tail()