Note: I do know that Python libraries provide a Linked list and Stack. This implementation has been done to practice Python and some of the data structures and algorithms.
I have implemented Queue as a Single linked list. Feel free to make suggestions. Note: The code works.
Targeted Big O:
Search: O(n), EnQueue and DeQueue: O(1)
Methods:
en_queue(value): insert values
de_queue(): remove values
is_empty(): check to see if Queue is empty
peek_front(): look at what head points to without removing
peek_back(): look at what tail points to without removing
search(value): check to see if a value is in the Queue
length(): return size
Classes:
class Node:
def __init__(self, value):
self.data = value
self.front = None
class Queue:
def __init__(self):
self.head = None
self.tail = None
self.count = 0
def en_queue(self, value):
new_node = Node(value)
if self.tail is not None:
# make the front attribute of old node point to new node
self.tail.front = new_node
else:
# if first ever node in Queue both front and tail will point to it
self.head = new_node
self.tail = new_node
self.count += 1
def de_queue(self):
if not self.is_empty():
# point head to next node
self.head = self.head.front
print("sucess")
self.count -= 1
else:
print("Empty QUEUE")
def __iter__(self):
node = self.head
while node:
yield node
node = node.front
def is_empty(self):
if self.head is None and self.tail is None:
return True
else:
return False
def peek_front(self):
return self.head.data
def peek_back(self):
return self.tail.data
def queue_search(self, value):
# start from the head
p = self.head
while p is not None:
# make p reference to next node
if p.front is not None:
if p.data == value:
print("Found value")
return p.data
p = p.front
else:
print("fail")
return 0
def length(self):
return self.count
Test:
from stack_Queue import Queue
def main():
print("-------Test Queue---------")
print("-------Test En Queue------")
my_queue = Queue()
test_list = [1, 2, 3, 4, -2000000, 'a', 500000, 50]
for i in test_list:
my_queue.en_queue(i)
print("-------En Queue Done-------")
for i in my_queue:
print(i.data)
print("------Queue Print Done-----")
print("------Queue Print Done-----")
print(my_queue.peek_back())
print(my_queue.peek_front())
print("----------De Queue---------")
my_queue.de_queue()
print("--------De Queue Done------")
for i in my_queue:
print(i.data)
print("-----Queue Print Done------")
print("-----Test search-------")
x = my_queue.queue_search('a')
print(x)
print("-------Full De Queue-------")
while my_queue.length() != 0:
my_queue.de_queue()
print("--------De Queue Done------")
for i in my_queue:
print(i.data)
print("-----Queue Print Done------")
if __name__ == "__main__":
main()
Result:
-------Test Queue---------
-------Test En Queue------
-------En Queue Done-------
1
2
3
4
-2000000
a
500000
50
------Queue Print Done-----
------Queue Print Done-----
50
1
----------De Queue---------
sucess
--------De Queue Done------
2
3
4
-2000000
a
500000
50
-----Queue Print Done------
-----Test search-------
Found value
a
-------Full De Queue-------
sucess
sucess
sucess
sucess
sucess
sucess
sucess
--------De Queue Done------
-----Queue Print Done------
Process finished with exit code 0