This repository was archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLQueue.py
More file actions
89 lines (81 loc) · 2.55 KB
/
LLQueue.py
File metadata and controls
89 lines (81 loc) · 2.55 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# ---------------------------
# - Ryan Williamson -
# - Advanced Higher Project -
# ---------------------------
"""This class contains the data and a pointer to the nextNode"""
class Node:
"""Class Constructor"""
def __init__(self, data):
self.data = data
self.nextNode = None
"""This class contains functionality for a first in first out data structure"""
class Queue:
"""Class Constructor"""
def __init__(self):
self.length = 0
self.head = None
self.tail = None
"""
Returns the length of the Linked List using python's built-in
len() function
"""
def __len__(self):
return self.length
"""Adds an item to the queue"""
def Enqueue(self, data):
current = Node(data)
self.length += 1
# If this is the first Node
if self.head == None:
self.head = current
self.tail = current
else:
# Set Node after tail to current
self.tail.nextNode = current
# Set new tail to current
self.tail = current
"""
Removes an item from the queue and returns the value
If the queue is empty a node is returned with an empty queue string
"""
def Dequeue(self):
if self.head == None:
return Node("Empty queue")
# If there is only 1 item in the Queue
if self.head == self.tail:
# Store current head Node
temp = self.head
# Clear Head and Tail Node
self.head = None
self.tail = None
self.length -= 1
# Return the stored head node
return temp
# When there is more than 1 item in the queue
temp = self.head
# Set new head to the Node after the head Node
self.head = self.head.nextNode
self.length -= 1
return temp
"""Display the queue as a series of print's"""
def debugDisplay(self):
current = self.head
while (current != None):
print(str(current.data))
current = current.nextNode
"""Converts the queue to a tuple"""
def convertToTuple(self):
# Create empty tuple
tuple = ()
# Start at the head node
current = self.head
# While there is a node to opereate on
while (current != None):
tuple = tuple + (current.data, )
# Move onto next Node
current=current.nextNode
return tuple
"""Clear the queue"""
def clearQueue(self):
self.head = None
self.tail = None