LinkedStack and Queue Implementation in Python, Study notes of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/30/2013

jut
jut 🇮🇳

4.5

(63)

77 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. The Node class (in node.py) is used to dynamically create storage for a new item added to the stack. The
LinkedStack class (in
linked_stack.py
) uses this Node class. Conceptually, a
LinkedStack
object would
look like:
a
b
c top
bottom
Stack
"Abstract"
_top:
_size:
3
'c' 'b' 'a'
LinkedStack Object
Node Objects
data data
data next next
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()
Data Structures (CS 1520) Lecture 5 Name:_________________
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."""
Docsity.com
pf3
pf4

Partial preview of the text

Download LinkedStack and Queue Implementation in Python and more Study notes Data Structures and Algorithms in PDF only on Docsity!

  1. The Node class (in node.py) is used to dynamically create storage for a new item added to the stack. The LinkedStack class (in linked_stack.py) uses this Node class. Conceptually, a LinkedStack object would look like:

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."""

Docsity.com

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

  1. Complete the following table by indicating which of the queue operations should have preconditions. Write “none” if a precondition is not needed.

str(myQueue)

myQueue.size()

myQueue.isEmpty()

myQueue.peek()

myQueue.enqueue(myItem)

myQueue.dequeue()

Method Call on myQueue object Precondition(s)

  1. The textbook’s Queue implementation use a Python list: a) Complete the _peek, and str methods

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):

Docsity.com

_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

Lecture 5 - Page 4Docsity.com