-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.py
More file actions
51 lines (40 loc) · 1.21 KB
/
LinkedQueue.py
File metadata and controls
51 lines (40 loc) · 1.21 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
# -*- coding: utf-8 -*-
"""
Created on Sat May 17 19:02:20 2014
@author: akshay
"""
class LinkedQueue:
'''FIFO Queue implementation using a singly linked list for storage.'''
class Node:
'''Lightweight, nonpublic class for storing a singly linked node.'''
def __init__(self, element, next):
self.element = element
self.next = next
def __init__(self, head, size, tail):
self.head = head
self.size = size
self.tail = tail
def __len__(self):
return self.size
def is_empty(self):
return self.size == 0
def enqueue(self, e):
node = self.Node(e, self.head)
if self.is_empty():
self.head = node
else:
self.tail.next = node
self.tail = node
self.size += 1
def top(self):
if self.is_empty():
return 'Error, the stack is empty'
return self.head.element
def dequeue(self):
if self.is_empty():
self.tail = None
return 'Error, the stack is empty'
value = self.head.element
self.head = self.head.next
self.size -= 1
return value