Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f573050

Browse files
committedApr 12, 2017
Creativity section of Exercises
1 parent 6712ddc commit f573050

File tree

4 files changed

+391
-212
lines changed

4 files changed

+391
-212
lines changed
 

‎.idea/workspace.xml

+136-212
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class Empty(Exception):
2+
"""Error attempting to access an element from an empty container"""
3+
pass
4+
5+
6+
class LinkedStack:
7+
"""LIFO Stack implementation using a singly linked list for storage"""
8+
9+
# --------------- nested _Node class -----------------
10+
class _Node:
11+
"""Lightweight, nonpublic class for storing a singly linked node."""
12+
__slots__ = '_element', '_next'
13+
14+
def __init__(self, element, next):
15+
self._element = element
16+
self._next = next
17+
18+
# -------------------- stack methods ------------------
19+
20+
def __init__(self):
21+
"""Create an empty stack"""
22+
23+
self._header = self._Node(None,None)
24+
self._size = 0
25+
26+
def __len__(self):
27+
"""Return the number of elements in stack"""
28+
return self._size
29+
30+
def is_empty(self):
31+
"""Return True if stack is empty"""
32+
return self._size == 0
33+
34+
def push(self, e):
35+
"""Add element to the top of stack"""
36+
newest = self._Node(e, self._header._next)
37+
self._header._next = newest
38+
self._size += 1
39+
40+
def top(self):
41+
"""Return the element at the top of the stack.
42+
43+
Raise Empty exception if stack is empty.
44+
"""
45+
if self.is_empty():
46+
raise Empty('Stack is empty')
47+
return self._header._next._element
48+
49+
def pop(self):
50+
""" Return and remove the element at the top of the stack
51+
52+
Raise empty exception if stack is empty.
53+
"""
54+
if self.is_empty():
55+
raise Empty('Stack is empty')
56+
answer = self._header._next._element
57+
self._header = self._header._next
58+
self._size -= 1
59+
return answer
60+
61+
62+
# Testing
63+
if __name__ == "__main__":
64+
stack = LinkedStack()
65+
66+
stack.push(10)
67+
stack.push(20)
68+
stack.push(30)
69+
print(stack.top())
70+
stack.push(40)
71+
print(stack.pop())
72+
print(stack.top())
73+
print(stack.pop())
74+
print(stack.top())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
class Empty(Exception):
2+
"""Error attempting to access an element from an empty container"""
3+
pass
4+
5+
class LinkedQueue:
6+
"""FIFO Queue implementation using a singly linked list for storage"""
7+
8+
# --------------- nested _Node class -----------------
9+
class _Node:
10+
"""Lightweight, nonpublic class for storing a singly linked node."""
11+
__slots__ = '_element', '_next'
12+
13+
def __init__(self, element, next):
14+
self._element = element
15+
self._next = next
16+
17+
18+
# -------------------- queue methods ------------------
19+
20+
def __init__(self):
21+
"""Create an empty queue."""
22+
23+
self._head = None
24+
self._tail = None
25+
self._size = 0
26+
27+
def __len__(self):
28+
"""Return the size of queue."""
29+
return self._size
30+
31+
def is_empty(self):
32+
"""Return True if queue is empty."""
33+
return self._size == 0
34+
35+
def first(self):
36+
"""Return the first element of the queue.
37+
38+
Raise Empty exception if stack is empty.
39+
"""
40+
if self.is_empty():
41+
raise Empty('Queue is empty')
42+
return self._head._element
43+
44+
def enqueue(self,e):
45+
"""Add an element at the back of the queue"""
46+
newest = self._Node(e, None)
47+
if self.is_empty():
48+
self._head = newest
49+
else:
50+
self._tail._next = newest
51+
self._tail = newest
52+
self._size += 1
53+
54+
def dequeue(self):
55+
""" Return and remove the element at the front of the queue.
56+
57+
Raise empty exception if queue is empty.
58+
"""
59+
if self.is_empty():
60+
raise Empty('Queue is empty.')
61+
answer = self._head._element
62+
self._head = self._head._next
63+
if self.is_empty():
64+
self._tail = None
65+
return answer
66+
67+
def concatenate(self, other):
68+
"""Concatenates the other queue"""
69+
self._tail._next = other._head
70+
self._tail = other._tail
71+
self._size += other._size
72+
other._size = 0
73+
other._head = None
74+
75+
def __str__(self):
76+
elements = []
77+
walk = self._head
78+
while walk is not None:
79+
elements.append(str(walk._element) + ' ')
80+
walk = walk._next
81+
return ''.join(elements)
82+
83+
# Testing
84+
if __name__ == "__main__":
85+
queue = LinkedQueue()
86+
87+
queue.enqueue(10)
88+
queue.enqueue(20)
89+
queue.enqueue(30)
90+
print(queue)
91+
queue.enqueue(40)
92+
print(queue)
93+
queue.dequeue()
94+
print(queue)
95+
96+
queue_two = LinkedQueue()
97+
queue_two.enqueue(50)
98+
queue_two.enqueue(60)
99+
print(queue_two)
100+
101+
queue.concatenate(queue_two)
102+
print(queue)
103+
print(queue_two)
104+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class Empty(Exception):
2+
"""Error attempting to access an element from an empty container"""
3+
pass
4+
5+
class LinkedQueue:
6+
"""FIFO Queue implementation using a singly linked list for storage"""
7+
8+
# --------------- nested _Node class -----------------
9+
class _Node:
10+
"""Lightweight, nonpublic class for storing a singly linked node."""
11+
__slots__ = '_element', '_next'
12+
13+
def __init__(self, element, next):
14+
self._element = element
15+
self._next = next
16+
17+
18+
# -------------------- queue methods ------------------
19+
20+
def __init__(self):
21+
"""Create an empty queue."""
22+
23+
self._head = None
24+
self._tail = None
25+
self._size = 0
26+
27+
def __len__(self):
28+
"""Return the size of queue."""
29+
return self._size
30+
31+
def is_empty(self):
32+
"""Return True if queue is empty."""
33+
return self._size == 0
34+
35+
def first(self):
36+
"""Return the first element of the queue.
37+
38+
Raise Empty exception if stack is empty.
39+
"""
40+
if self.is_empty():
41+
raise Empty('Queue is empty')
42+
return self._head._element
43+
44+
def enqueue(self,e):
45+
"""Add an element at the back of the queue"""
46+
newest = self._Node(e, None)
47+
if self.is_empty():
48+
self._head = newest
49+
else:
50+
self._tail._next = newest
51+
self._tail = newest
52+
self._size += 1
53+
54+
def dequeue(self):
55+
""" Return and remove the element at the front of the queue.
56+
57+
Raise empty exception if queue is empty.
58+
"""
59+
if self.is_empty():
60+
raise Empty('Queue is empty.')
61+
answer = self._head._element
62+
self._head = self._head._next
63+
if self.is_empty():
64+
self._tail = None
65+
return answer
66+
67+
# Testing
68+
if __name__ == "__main__":
69+
queue = LinkedQueue()
70+
71+
queue.enqueue(10)
72+
queue.enqueue(20)
73+
queue.enqueue(30)
74+
print(queue.first())
75+
queue.enqueue(40)
76+
print(queue.dequeue())
77+

0 commit comments

Comments
 (0)
Please sign in to comment.