Question: Stack using node-PYTHON Here's the code: class Node: def __init__(self, value): self.value = value self.next = None def getValue(self): return self.value def getNext(self): return self.next

Stack using node-PYTHON

Stack using node-PYTHON Here's the code: class Node: def __init__(self, value): self.value

Here's the code:

class Node: def __init__(self, value): self.value = value self.next = None def getValue(self): return self.value

def getNext(self): return self.next

def setValue(self,new_value): self.value = new_value

def setNext(self,new_next): self.next = new_next

def __str__(self): return ("{}".format(self.value))

__repr__ = __str__ class Stack: def __init__(self): self.top = None #Do NOT modify this line

def isEmpty(self): #write your code here

def size(self): #write your code here

def push(self,item): #write your code here

def pop(self): #write your code here

def peek(self): #write your code here

def printStack(self): temp=self.top while temp: print(temp.getValue()) temp=temp.getNext()

Testcode:

>>>stack=Stack()

>>>stack.push(2)

>>> stack.push(4)

>>> stack.push(6)

>>> stack.printStack()

6

4

2

>>> stack.pop()

6

>>> stack.printStack()

4

2

>>> stack.size()

2

>>> stack.isEmpty()

False

>>> stack.push(15)

>>> stack.printStack()

15

4

2

In class, we discussed the abstract data type Stack. A stack is a collection of items where items are added to and removed from the top (LIFO). Implement the stack data structure with the following operations Stack0 creates a new stack that is empty. It needs no parameters and returns nothing push item) adds a new Node with value-item to the top of the stack. It needs the item and returns nothing. [2 pts] popO removes the top Node from the stack. It needs no parameters and returns the value of the Node removed from the stack. Modifies the stack. [2 pts] peek) returns the value of the top Node from the stack but does not remove it. It needs no parameters. The stack is not modified. [2 pts] isEmptyO tests to see whether the stack is empty. It needs no parameters and returns a boolean value. [2 pts size0 returns the number of items on the stack. It needs no parameters and returns an integer 12 pts EXAMPLE >>>stack Stack () >>>stack.push (2) >>stack.push (4) stack.push (6)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!