

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Different implementations of a double ended queue (deque) using python lists and linked lists, as well as the concept of a priority queue. Code snippets and discusses the time complexity of various operations.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


A Deque (pronounced “Deck”) is a linear data structure which behaves like a double-ended queue, i.e., it allows adding or removing items from either the front or the rear of the Deque.
removeRear removeFront( )
addRear(newItem) addFront(newItem)
'a' 'b' 'c' 'd'
items:
Deque Object List Object
(rear) (front)
class Deque(object): def init(self):
a) Complete the init method and determine the big-oh, O ( ), for each Deque operation, assuming the above implementation. Let n be the number of items in the Deque. isEmpty addFront removeFront addRear removeRear size
b) Write the methods for the addRear and removeRear operation.
def addRear(self, newItem): def removeRear(self):
LinkedDeque Object
_front:
_rear:
_size: (^4)
data next data next data next data next 'a' 'b' 'c' 'd'
class LinkedDeque(object): def init(self):
a) Complete the init method and determine the big-oh, O ( ), for each Deque operation assuming the above linked implementation. Let n be the number of items in the Deque. isEmpty addFront removeFront addRear removeRear size
b) Suggest an improvement to the above linked implementation of the Deque to speed up some of its operations.
from node import Node class Node2Way(Node): def init(self,initdata): Node.init(self, initdata) self.previous = None def getPrevious(self): return self.previous def setPrevious(self,newprevious): self.previous = newprevious
DoublyLinkedDeque Object
_front:
_rear:
_size: (^4)
previous data next previous data next previous data next previous data next 'a' 'b' 'c' 'd'
a) Determine the big-oh, O ( ), for each Deque operation assuming the above doubly-linked implementation. Let n be the number of items in the Deque. isEmpty addFront removeFront addRear removeRear size
a) Suppose that we have a priority queue with integer priorities such that the smallest integer corresponds to the highest priority. For the following priority queue, which item would be dequeued next?
priority queue:
b) To implement a priority queue, we could use an unorder Python list. If we did, what would be the big-oh notation for each of the following methods: (justify your answer)
enqueue:
dequeue:
c) To implement a priority queue, we could use a Python list order by priorities in decending order. If we did, what would be the big-oh notation for each of the following methods: (justify your answer)
enqueue:
dequeue