-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathqueueUsingLinkedList.py
More file actions
59 lines (58 loc) · 1.54 KB
/
queueUsingLinkedList.py
File metadata and controls
59 lines (58 loc) · 1.54 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
class Node:
# constructor
def __init__(self, data = None, next = None):
self.data = data
self.next = next
# A Linked List class with a single head node
class LinkedList:
def __init__(self):
self.head = None
# insert at the tail of the linked list
def insert(self, data):
newNode = Node(data)
if(self.head):
current = self.head
while(current.next):
current = current.next
current.next = newNode
else:
self.head = newNode
# remove an element from the head of the linked list
def remove(self):
temp = self.head
if (temp is not None):
element = temp.data
self.head = temp.next
temp = None
return element
# dispay an element from the tail of the linked list
def peek(self):
if(self.head):
current = self.head
while(current.next):
current = current.next
return current.data
# print method for the linked list
def printLL(self):
current = self.head
while(current):
print(current.data)
current = current.next
# A queue implemented using a linked list
Queue = LinkedList()
# Enqueue 3
Queue.insert(88)
# Display element at the tail
print("Element at the tail:", Queue.peek())
# Enqueue 4
Queue.insert(23)
# Display element at the tail
print("Element at the tail:", Queue.peek())
# Enqueue 5
Queue.insert(59)
# Display element at the tail
print("Element at the tail:", Queue.peek())
# Dequeue elements
print("Element dequeued:", Queue.remove())
print("Element dequeued:", Queue.remove())
print("Element dequeued:", Queue.remove())