-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_ex1.py
73 lines (57 loc) · 1.76 KB
/
queue_ex1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Date Created: 11/23/2020
Author: Nicholas Cable, el cablecito
Purpose: Create a tutorial about queues that allow the reader to
understand it's format and have enough functions to
demonstrate how it works.
"""
import random
class Queue:
"""
This class holds the basic outline of a queue []. The main functions will
be declared here.
"""
class Node:
"""
This will hold the data of the queue. For now, there will only be one
value held in the __init__().
"""
def __init__(self, value):
self.value = value
# need __str__ to print out object's value
# if this is not added, would return place in memory of object
def __str__(self):
return f'{self.value}'
# define the class as being a []
def __init__(self):
self.queue = []
# str function overwrite
# return str(node) to make everything string
# (who knows what values could be put in here)
def __str__(self):
result = '['
for node in self.queue:
result += str(node) + ', '
result = result[:-2]
result += ']'
return result
# create a function to add values to a queue
def enqueue(self, value):
new_node = Queue.Node(value)
self.queue.append(new_node)
# write a dequeue function
def dequeue(self):
# set a var as the value to be removed
removal = self.queue[0]
del self.queue[0]
print(f'The value being removed is {removal}.\n')
# Test Cases!
print(f"{' TEST CASE 1 ':=^40}\n")
q1 = Queue()
q1.enqueue(random.randint(1, 10))
q1.enqueue(random.randint(10, 22))
q1.enqueue(random.randint(20, 30))
print(q1)
# Test case 2
print(f"\n{' TEST CASE 2 ':=^40}\n")
q1.dequeue()