Question

In: Computer Science

Python - Please include running time of push(), pop(), and top() methods and tester code for...

Python - Please include running time of push(), pop(), and top() methods and tester code for all methods.

Design a stack ADT using a single queue as an instance variable, and only constant additional local memory within the method bodies. What is the running time of the push(), pop(), and top() methods for your design? Implement your modified stack ADT in Python, including tester code to test all its methods.

Solutions

Expert Solution

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

# Implement Stack using single queue and recursion

class Stack:

    # Constructor

    def __init__(self):

        self.q = []

    # Insert an item into the stack

    def push(self, data):

        self.q.append(data)

    # Utility function to reverse contents of a queue

    def reversedeque(self):

        # base case

        if not self.q:

            return

        # hold front element in recursion call stack and insert

        # it back into the queue after recursive call is over

        front = self.q.pop()

        self.reversedeque()

        self.q.append(front)

    # Remove the top item from the stack

    def pop(self):

        # if the queue is isEmpty

        if not self.q:

            print("Underflow!!")

            exit(0)

        # reverse the queue

        self.reversedeque()

        # pop front element of reversed queue

        front = self.q.pop()

        # revert the queue to original state

        self.reversedeque()

        return front

if __name__ == '__main__':

    keys = 1, 2, 3, 4, 5

    # insert above keys into the stack

    s = Stack()

    for key in keys:

        s.push(key)

    for i in range(len(keys) + 1):

        print(s.pop())


Related Solutions

All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
IN JAVA. I have the following code (please also implement the Tester to test the methods...
IN JAVA. I have the following code (please also implement the Tester to test the methods ) And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements. After it should decrease the size of the array, and after return and save the integer. package ourVector; import java.util.Scanner; public class ourVector { private int[]...
Design and write an efficient Python function (with tester code) for finding the 10 largest elements...
Design and write an efficient Python function (with tester code) for finding the 10 largest elements in a sequence of size n, where n >= 50. In your tester function, write code that generates at least 50 random numbers and stores them in an array, then calls your function on that array (and prints the resulting 10 largest elements). This function must be as efficient as possible. Marks will be deducted for a slow function, even if it actually answers...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would be appreciated 8.19 LAB*: Program: Soccer team roster (Dictionaries) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers and the ratings in a...
This is my current code for a question that asks: "Implement the stack methods push(x) and...
This is my current code for a question that asks: "Implement the stack methods push(x) and pop() using two queues". It works well, other than I want it to work for data of type T, not just Int's. (EXCUSE THE INDENTING... CHEGG POSTING DOESNT ALLOW PROPER CODE TO BE UPLOADED... JUST COPY PASTE IT INTO ECLIPSE IDE, HIGHLIGHT ALL CODE, PRESS COMMAND+SHIFT+F AND SHOULD AUTO-FORMAT) MY QUESTION: How could one modify this code in order to take data of type...
Please fill in the code where it says to implement Methods needed to implement include: insert,...
Please fill in the code where it says to implement Methods needed to implement include: insert, find, remove, and toIndex // // STRINGTABLE.JAVA // A hash table mapping Strings to their positions in the the pattern sequence // You get to fill in the methods for this part. // public class StringTable {    private LinkedList<Record>[] buckets; private int nBuckets; // // number of records currently stored in table -- // must be maintained by all operations // public int...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT