class DoublyLinkedList: def __init__(self): self.head = None def is_empty(self): if self.head == None: return True else: return False def enqueue_front(self, data): new_node = Node(data) new_node.next = self.head if self.head is not None: self.head.prev = new_node self.head = new_node def enqueue_rear(self, data): new_node = Node(data) new_node.next = None if self.head is None: new_node.prev = None self.head = new_node return last = self.head while(last.next is not None): last = last.next last.next = new_node new_node.prev = last return def peek(self): return self.head.data def dequeue_front(self): if self.head is None: return temp = self.head self.head = self.head.next self.head.prev = None return temp.data def dequeue_rear(self): if self.head is None: return temp = self.head if self.head.next is None: self.head = None return temp.data while(temp.next is not None): temp = temp.next temp.prev.next = None return temp.data def is_full(self): return False def queue(self): if self.head is None: print("Queue is empty") return temp = self.head while(temp): print(temp.data, end=" ") temp = temp.next print() def clear(self): self.head = None # Driver program dll = DoublyLinkedList() print(dll.is_empty()) dll.enqueue_front(15) dll.enqueue_rear(28) print(dll.peek()) dll.queue() print(dll.dequeue_front()) print(dll.is_empty()) print(dll.dequeue_rear()) print(dll.dequeue_front()) dll.enqueue_front(31) dll.enqueue_front(47) dll.enqueue_rear(54) print(dll.is_full()) dll.queue() print(dll.peek()) dll.clear() dll.enqueue_rear(66) dll.enqueue_rear(79) dll.enqueue_front(88) dll.queue() FIX THIS CODE THE OUTPUT SHOULD BE EXACTLY LIKE THIS OUTPUT: False 15 15 28 15 False 28 31 False 47 31 54 47
class DoublyLinkedList:
def __init__(self):
self.head = None
def is_empty(self):
if self.head == None:
return True
else:
return False
def enqueue_front(self, data):
new_node = Node(data)
new_node.next = self.head
if self.head is not None:
self.head.prev = new_node
self.head = new_node
def enqueue_rear(self, data):
new_node = Node(data)
new_node.next = None
if self.head is None:
new_node.prev = None
self.head = new_node
return
last = self.head
while(last.next is not None):
last = last.next
last.next = new_node
new_node.prev = last
return
def peek(self):
return self.head.data
def dequeue_front(self):
if self.head is None:
return
temp = self.head
self.head = self.head.next
self.head.prev = None
return temp.data
def dequeue_rear(self):
if self.head is None:
return
temp = self.head
if self.head.next is None:
self.head = None
return temp.data
while(temp.next is not None):
temp = temp.next
temp.prev.next = None
return temp.data
def is_full(self):
return False
def queue(self):
if self.head is None:
print("Queue is empty")
return
temp = self.head
while(temp):
print(temp.data, end=" ")
temp = temp.next
print()
def clear(self):
self.head = None
# Driver program
dll = DoublyLinkedList()
print(dll.is_empty())
dll.enqueue_front(15)
dll.enqueue_rear(28)
print(dll.peek())
dll.queue()
print(dll.dequeue_front())
print(dll.is_empty())
print(dll.dequeue_rear())
print(dll.dequeue_front())
dll.enqueue_front(31)
dll.enqueue_front(47)
dll.enqueue_rear(54)
print(dll.is_full())
dll.queue()
print(dll.peek())
dll.clear()
dll.enqueue_rear(66)
dll.enqueue_rear(79)
dll.enqueue_front(88)
dll.queue()
FIX THIS CODE
THE OUTPUT SHOULD BE EXACTLY LIKE THIS
OUTPUT:
False
15
15 28
15
False
28
31
False
47 31 54
47
Step by step
Solved in 4 steps with 3 images