Instructions:
SLLStack (12 pts)
● Using the two properties below, implement the stack
interface using the SLLNode class from Lab 2:
○ top_node → SLLNode object or None
○ size → int, keep track of stack size
● Implement the push( ), pop( ), top( ) methods
● Use SLLNode methods:
get_item( ), set_item( ), get_next( ), set_next( )
● (5 pts) In push(item):
○ Create new SLLNode with item
○ Add new node before top node
○ Update top_node and size properties
● (5 pts) In pop( ):
○ If empty, raise Exception('Empty stack: cannot pop')
○ Remove top node and return its item
○ Remove any links from old top node
○ Update top_node and size properties
● (2 pts) In top( ):
○ If empty, raise Exception('Empty stack: no top')
○ Return top node’s item
Code:
class SLLStack:
def __init__(self):
# NOTE: DO NOT EDIT THIS CODE
# constructor: set properties
top_node, size
self.top_node = None
self.size = 0
def __repr__(self):
# NOTE: DO NOT EDIT THIS CODE
# string representation of SLLStack
object
display = []
node = self.top_node
while node != None:
display.append(str(node.get_item()))
node =
node.get_next()
display = ', '.join(display)
return 'top [' + display + ']'
def is_empty(self):
# NOTE: DO NOT EDIT THIS CODE
# check if stack is empty
return self.size == 0
def push(self,item):
# TODO: Write your code
here...
# TODO: add new node (with item)
before top_node
# TODO: update top_node and size
properties
pass # TODO: remove this line
def pop(self):
# TODO: Write your code
here...
# TODO: if empty, raise
Exception('Empty stack: cannot pop')
# TODO: remove top node and return
its item
# TODO: remove any links from old
top_node (Hint: set to None)
# TODO: update top_node and size
properties
pass # TODO: remove this line
def top(self):
# TODO: Write your code
here...
# TODO: if empty, raise
Exception('Empty stack: no top')
# TODO: return top_node's item
pass # TODO: remove this
line
ANSWER:
CODE TEXT
# sample class SLLNode, as not provided in question
class SLLNode:
def __init__(self,item):
# constructor: set properties item, next_node
self.item = item
self.next = None
# method get_item()
def get_item(self):
# return item value
return self.item
# method set_item()
def set_item(self,item):
# set item value
self.item = item
# method get_next()
def get_next(self):
# return next node
return self.next
# method set_next():
def set_next(self,node):
# set next value
self.next = node
# class SLLStack
class SLLStack:
def __init__(self):
# NOTE: DO NOT EDIT THIS CODE
# constructor: set properties top_node, size
self.top_node = None
self.size = 0
def __repr__(self):
# NOTE: DO NOT EDIT THIS CODE
# string representation of SLLStack object
display = []
node = self.top_node
while node != None:
display.append(str(node.get_item()))
node = node.get_next()
display = ', '.join(display)
return 'top [' + display + ']'
def is_empty(self):
# NOTE: DO NOT EDIT THIS CODE
# check if stack is empty
return self.size == 0
def push(self,item):
# TODO: Write your code here...
# TODO: add new node (with item) before top_node
# new node, SLLNode object with value item
node = SLLNode(item)
# seting next value of node to top_node, using set_next
method
node.set_next(self.top_node)
# TODO: update top_node and size properties
# updating top_node to node
self.top_node = node
# updating size, by incrementing by 1
self.size += 1
def pop(self):
# TODO: Write your code here...
# TODO: if empty, raise Exception('Empty stack: cannot pop')
if self.size == 0:
raise Exception('Empty stack: cannot pop')
# TODO: remove top node and return its item
# defining next_node and assigning it next node to top node
# using get_next() method
next_node = self.top_node.get_next()
# fetching value from top_node using get_item method
top_value = self.top_node.get_item()
# TODO: remove any links from old top_node (Hint: set to
None)
# assigning next of top_node to none, using set_next
self.top_node.set_next(None)
# TODO: update top_node and size properties
# updating top_node to next_node
self.top_node = next_node
# decrementing size by 1
self.size -= 1
# returning top_value
return top_value
def top(self):
# TODO: Write your code here...
# TODO: if empty, raise Exception('Empty stack: no top')
if self.size == 0:
raise Exception('Empty stack: no top')
# TODO: return top_node's item
# using get_item method
return self.top_node.get_item()
## example
# defining stack
stack = SLLStack()
# pushing values 0 to 9 in stack
for i in range(10):
stack.push(i)
# displaying stack
print("Initial stack:",stack)
# displaying top value
print("Top value:",stack.top())
# displaying pop value and left stack
pop = stack.pop()
print("Pop value:",pop,",Left stack:",stack)
CODE IMAGE
OUTPUT IMAGE
Get Answers For Free
Most questions answered within 1 hours.