Question

ii. (30) A Stack is a container commonly used to provide a way to keep organized so that the last one pushed onto the top of the stack is thth one popped (removed)- so stacks are called last-in-first-out. on top of the stack is visible, but after removing the top item, t pushed item is visible, since it is back on top. The following Python co is a recursive definition of the class Stack. Read the code, and answer te Only the the previously questions which follow: class Stack (object) def init. (self): self.top = None self.restNone def push(self, item): if self.topNone: self.rest Stack) self.rest.push(self.top) self.top item else: self .top item def pop(self): if self.top None: return None else: item self.top self.top = self.rest.pop ( ) return item a. Identify, by circling and labeling, which parts of the method defi- nitions for push and pop are base case and recursive case b. Draw a class symbol diagram for Stack, giving all attributes and methods. For each attribute, specify its type. (Assume all items pushed and popped are integers.) c. Explain how this class definition is both structurally and function- ally recursive. d. A stack is empty if either nothing has yet been pushed onto it or everything that was pushed has been popped off the stack. Write a method enpty that returns True exactly when the stack is empty.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

a)

for push:

Base case:

if self.top==None

Recursive case:

the else part where

self.rest.push(self.push)

b)

stack cl noa Pope, to res ddo em teu

c)

Because push and pop are structurally defined in the class and within these functions, we can see that they are calling themselves recursively.

d)

def emptyStack:

if self.top==None

return true

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Add a comment
Know the answer?
Add Answer to:
ii. (30) A Stack is a container commonly used to provide a way to keep organized...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • ae two stacks, a number stack and an operator stack, to evaluate the tollowing algebraic expression...

    ae two stacks, a number stack and an operator stack, to evaluate the tollowing algebraic expression (6+2*8/(16-4*3))/2 Read the items in the expression one at a time from left to right. Show the number stack and the operator stack after each item in the expression has been read and processed. If processing an item involves evaluating the top, show the two stacks af and then show the two stacks again after a "(” , if any, is popped off or...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • Stacks There are two main operations associated with stacks; 1) putting things on the stack which...

    Stacks There are two main operations associated with stacks; 1) putting things on the stack which is referred to as push, 2) taking things from the stack which is referred to as pop.   We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of...

  • 11.2 Problem Set 2: PostScript Stack Commands (in python please) For this assignment you are to...

    11.2 Problem Set 2: PostScript Stack Commands (in python please) For this assignment you are to implement a PostScript command interpreter using a Stack. You must implement the stack using a singly-linked list. Solutions that use a data structure other than a singly-linked list will received no credit. The interpreter must read and execute a string of PostScript commands based on the following definitions: 1. = objn objn-1 … obj1 = : objn-1 … obj1 This command removes the topmost...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Po...

    in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Pop(self): return self.items.pop() def Display(self): for i in reversed(self.items): print(i,end="") print() s = My_Stack() #taking input from user str = input('Enter your string for palindrome checking: ') n= len(str) #Pushing half of the string into stack for i in range(int(n/2)): s.Push(str[i]) print("S",end="") s.Display() s.Display() #for the next half checking the upcoming string...

  • Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition...

    Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition of creating stack-based calculators. Rather than using standard algebraic notation ( 1 + 1 = ), the user would enter the values, then the operator. The calculator had an “enter” key to push each value onto a stack, then pop the stack when an operation key (e.g. “+” or “-”) was pressed. Assignment: Create a Calculator class that implements the provided StackCalculator interface Requirement Constructor: + Calculator()   ...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT