Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write code in Python class Node: def __init__(self, value): self.value = value self.next = None def __str__(self): return Node({}).format(self.value) __repr__ = __str__ class Stack: '''

Write code in Python

image text in transcribed

image text in transcribed

class Node:

def __init__(self, value):

self.value = value

self.next = None

def __str__(self):

return "Node({})".format(self.value)

__repr__ = __str__

class Stack:

'''

Creates an empty Stack with support for push and pop operations

>>> x=Stack()

>>> x.pop()

'Stack is empty'

>>> x.push(2)

>>> x.push(4)

>>> x.push(6)

>>> x

Top:Node(6)

Stack:

6

4

2

>>> x.pop()

6

>>> x

Top:Node(4)

Stack:

4

2

>>> len(x)

2

>>> x.peek()

4

'''

def __init__(self):

self.top = None

def __str__(self):

temp=self.top

out=[]

while temp:

out.append(str(temp.value))

temp=temp.next

out=' '.join(out)

return ('Top:{} Stack: {}'.format(self.top,out))

__repr__=__str__

def isEmpty(self):

#write your code here

def __len__(self):

#write your code here

def peek(self):

#write your code here

def push(self,value):

#write your code here

def pop(self):

#write your code here

Goal: [10 pts] In class, we discussed the abstract data structure Stack. A stack is a collection of items where items are added to and removed from the top (LIFO). Use the Node class (an object with a data field and a pointer to the next element) to 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. pop) 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. * 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. * * isEmpty) tests to see whether the stack is empty. It needs no parameters and returns a boolean value. len() returns the number of items on the stack. It needs no parameters and returns an integer. (You can add count in the Stack's constructor) * EXAMPLE >>>x-Stack() >>> x.pop () Stack is empty' >>x.push (2) >>>x.push (4) >>>x.push (6) Top: Node (6) Stack: 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions