-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue-stack-implementation.py
58 lines (41 loc) · 1.43 KB
/
queue-stack-implementation.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
import sys
sys.path.append("../chapter-2-linked-lists") # to allow for implementation of the linked list module
from LL_implementation import LinkedList, LLNode
""" Implement Stack and Queue classes from Python list object. Stack should contain pop(), push(item), peek(), and is_empty() methods & Queue should contain enqueue(item) (add/push), dequeue() (remove/pop), peek(), and is_empty() methods. """
class Stack(object):
def __init__(self, items=[]):
self.items = items
def is_empty(self):
if len(self.items) == 0:
return True
return False
def push(self, data):
self.items.append(data)
return
def peek(self):
if self.is_empty():
return "Stack is empty"
return self.items[-1]
def pop(self):
if self.is_empty():
return "Stack is empty"
return self.items.pop(-1)
class Queue(object):
def __init__(self, items=[]):
self.items = items
def is_empty(self):
if len(self.items) == 0:
return True
return False
def enqueue(self, data):
self.items.append(data)
return
def dequeue(self):
if self.is_empty():
return "Queue is empty"
return self.items.pop(0)
def peek(self):
if self.is_empty():
return "Queue is empty"
return self.items[0]
""" Implement Stack and Queue classes from LinkedList object. """