Docsity

Abstract - Data Structures - Lecture Notes, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1. An “abstract” view of the stack:
Using an array implementation would look something like:
items: a b c
0 1 2 3 (max-1)
top: max:
2100
Complete the big-oh notation for the following stack methods assuming an array implementation: ("n" is the # items)
Big-oh
ConstructorisFull( )isEmpty( ) size( )peek( )pop( )push(item)
2. Since Python does not have a (directly accessable) built-in array, we can use a list.
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
Since Python uses an array of references (pointers) to list items in their implementation of a list.
a
b
c top
bottom
Stack
"Abstract"
items:
Stack Object
list Object
a b c
0 1 2 3
a) Complete the big-oh notation for the stack methods assuming this Python list implementation: ("n" is the # items)
Big-oh
__init__isEmpty( ) size( )peek( )pop( )push(item)
b) Which operations should have what preconditions?
Data Structures (CS 1520) Lecture 4 Name:_________________
Lecture 4 - Page 1
a
b
c top
bottom
Docsity.com
pf2

Partial preview of the text

Download Abstract - Data Structures - Lecture Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

  1. An “abstract” view of the stack: Using an array implementation would look something like: items: (^) a^0 b^1 c^2 3 (max-1) top: 2 max: 100 Complete the big-oh notation for the following stack methods assuming an array implementation: ("n" is the # items) Big-oh

push(item) pop( ) peek( ) size( ) isEmpty( ) isFull( ) Constructor

  1. Since Python does not have a (directly accessable) built-in array, we can use a list. class Stack: def init(self):self.items = [] def isEmpty(self):return self.items == [] def push(self,self.items.append(item) item): def pop(self):return self.items.pop() def peek(self):return self.items[len(self.items)-1] def size(self):return len(self.items) Since Python uses an array of references (pointers) to list items in their implementation of a list.

ab

c top bottom

"Abstract"Stack items: Stack Object (^) a (^0 1) b (^) c 2 3 list Object

a) Complete the big-oh notation for the stack methods assuming this Python list implementation: ("n" is the # items) Big-oh

push(item) pop( ) peek( ) size( ) isEmpty( ) init

b) Which operations should have what preconditions?

Data Structures (CS 1520) Lecture 4 Name:_________________

Lecture 4 - Page 1

ab

c top bottom

Docsity.com

  1. The text’s alternative stack implementation also using a Python list is: class Stack: def init(self):self.items = [] def isEmpty(self):return self.items == [] def push(self,self.items.insert(0,item) item): def pop(self):return self.items.pop(0) def peek(self):return self.items[0] def size(self):return len(self.items) Since an array is used to implement a Python list, the alternate Stack implementation using a list:

ab

c top bottom

"Abstract"Stack "alternate" Stack Object items: (^) c (^0 1) b (^) a 2 3 list Object

a) Complete the big-oh notation for the “alternate” Stack methods: ("n" is the # items) Big-oh

push(item) pop( ) peek( ) size( ) isEmpty( ) init

  1. How could we use a stack to check if a word is a palindrome (e.g., radar, toot)?
  2. How could we check to see if we have a balanced string of nested symbols? (“((([]){()}[])”)

Data Structures (CS 1520) Lecture 4 Name:_________________

Lecture 4 - Page 2Docsity.com