Question: I really need some assistance! I've created a program for a list based stack, but I continue to receive error message Stack has no attribute
I really need some assistance! I've created a program for a list based stack, but I continue to receive error message "Stack" has no attribute "list". If anyone could guide me in the right direction, that would be great!
Design and implement an experiment that will compare the performance of the Python listbased stack and queue with the linked list implementation. Provide a brief discussion of both stacks and queues for this activity.
#implementating a stack with linked list
import gc
gccollect
class Node:
def initialselfnewdata:
self.data newdata
self.next None
class Stack:
#set head as null by default
def initialself:
self.head None
# Checks if stack is empty
def isemptyself:
if self.head None:
return True
else:
return False
#adding data to the stack
def pushselfnewdata:
if self.head None:
self.head Nodenewdata
else:
newnode Nodenewdata
newnode.next self.head
self.head newnode
#removes current head from the stack
def popself:
if self.isempty:
return None
else:
popitem self.list.head.data
self.list.removeheadNone
return popitem
numstack Stack
numstack.push
numstack.push
numstack.push
numstack.push
printElement stack after push is: end
listedNode numstack.list.head
while listedNode None:
printlistedNodedata, end
listedNode listedNode.next
print
popitem numstack.pop
printPopped element:", popitem
printElements stack after pop is: end
listedNode numstack.list.head
while listedNode None:
printlistedNodedata, end
listedNode listedNode.next
print
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
