


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
The implementation of linkedstack and queue data structures using python. It includes the definition of node class, linkedstack class, and queue class. The document also covers the methods for each class such as push, pop, peek, size, isempty, and str. The document also discusses the big-oh notation for each method.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



a
b
c top
bottom
Stack
"Abstract"
_top:
_size: (^3)
'c' 'b'^ 'a'
LinkedStack Object Node Objects
data next^ data^ next^ data next None
a) Complete the push, pop, and str methods.
b) Stack methods big-oh’s? (Assume “n” items in stack)
constructor init:
push(item):
pop()
peek()
size()
isEmpty()
str()
Lecture 5 - Page 1
class Node: def init(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext
class LinkedStack(object): """ Link-based stack implementation.""" def init(self): self._top = None self._size = 0 def push(self, newItem): """Inserts newItem at top of stack."""
def pop(self): """Removes and returns the item at top of the stack. Precondition: the stack is not empty."""
def peek(self): """Returns the item at top of the stack. Precondition: the stack is not empty.""" return self._top.getData() def size(self): """Returns the number of items in the stack.""" return self._size def isEmpty(self): return len(self) == 0 def str(self): """Items strung from top to bottom."""
A FIFO queue is basically what we think of as a waiting line.
"front" "rear"
"dequeue"^ "enqueue"
front item new items to the rear
The operations/methods on a queue object, say myQueue are:
str(myQueue) Returns the string representation of the queue
myQueue.size() Returns the number of items currently in the queue
myQueue.isEmpty() Returns True if the queue is empty, or False otherwise.
myQueue.peek() Returns the front item in the queue without removing it.
myQueue.enqueue(myItem) Adds myItem at the rear of the queue
myQueue.dequeue() Removes and returns the front item in the queue.
Method Call on myQueue object Description
str(myQueue)
myQueue.size()
myQueue.isEmpty()
myQueue.peek()
myQueue.enqueue(myItem)
myQueue.dequeue()
Method Call on myQueue object Precondition(s)
b) What are the Queue methods big-oh’s? (Assume “n” items in the queue)
constructor init:
isEmpty()
enqueue(item)
peek()
size()
str()
Lecture 5 - Page 2
class Queue: def init(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def peek(self):
def size(self): return len(self.items) def str(self):
_front:
_rear:
_size: (^3)
data next^ data^ next^ data next 'w' 'x'^ 'y'
LinkedQueue Object
e) Draw the picture and number the steps for the dequeue method of the “normal” case (non-empty queue) above?
f) Write the dequeue method code for the “normal” case:
g) What “special case(s)” does the dequeue method code need to handle?
h) Draw the picture for each special case and number the steps for the dequeue method in the “special” case(s)
i) Combine the “normal” and special case(s) code for a complete dequeue method.
j) Complete the big-oh notation for the LinkedQueue methods: ("n" is the # items)
Big-oh
init enqueue(item) dequeue( ) peek( ) size( ) isEmpty( ) str