Question

Python coding conceptual questions -

Conceptual Questions Suppose you are given a (strange) computer that can only perform the following instructions (in addition

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

Following is the queue implementation using stacks, and in python, we can use lists as the stack using the append and pop operation for push and pop respectively.

Following is the code for the same in python.

# Python3 program to implement Queue using
# Two Stack implementation for making a queue
# Here we can use list as the stack in python
# using the append function as the push operation
# using the pop operation from poping out from the last
class Queue:
    def __init__(self):
        self.s1 = []
        self.s2 = []

    def enQueue(self, x):
        # if first stack is empty
        # Add to top of self.s1
        self.s1.append(x)

    # Dequeue an item from the queue
    def deQueue(self):
        if len(self.s1) == 0:
            print('Q is Empty')
        while len(self.s1) != 0:
            self.s2.append(self.s1[-1])
            self.s1.pop()

        # Push item into self.s1
        x = self.s2[-1]
        self.s2.pop()

        # Push everything back to s1
        while len(self.s2) != 0:
            self.s1.append(self.s2[-1])
            self.s2.pop()
        return x

# Driver code
if __name__ == '__main__':
    q = Queue()
    q.enQueue(55)
    q.enQueue(44)
    q.enQueue(33)

    print(q.deQueue())
    print(q.deQueue())
    print(q.deQueue())

2. Worst case for the dequeue operation in this implementation can be O(n)
As we need to remove all the elements from the one stack and then push them back again to other stack, which could take n operation hence the complexity would be O(n).

3. dequeue for k items would be 2*k

Thus for n enqueue and n dequeue operation.

Enqueue operation: does not require any pop operation.

Dequeue operation: at any time with k entries, we require 2*k pop for one dequeue.

So for n operation: 2*n + 2*(n-1) + 2*(n-2) + 2 *(n-3) + . . . . . + 2*(1)
= 2 *(n + n-1 + n-2 + n-3 + . . . . 1)
= 2* (n * (n-1)) /2
= n*(n-1)

Thanks,

Add a comment
Know the answer?
Add Answer to:
Conceptual Questions Suppose you are given a (strange) computer that can only perform the followi...
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
  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • 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...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • My CSC 220 teacher has given this as a homework assignment and starting it has not...

    My CSC 220 teacher has given this as a homework assignment and starting it has not made much sense and am generally lost on this assignment help would be much appreciated. I put everything from the assignment power point slides she gave us in here so the expert has everything im working with as well. Dequeue Please implement your own Dequeue class which has following methods boolean add(E e)= void addLast(E e) void addFirst(E e) E getFirst( ) = E...

  • Goals This assignment is an individual assignment and you will work on it on your own....

    Goals This assignment is an individual assignment and you will work on it on your own. The goal of this assignment is to be able to use stacks and queues, and to master and have an in-depth understanding of such primitive ADTs. In this assignment you will build a more complex ADT using the primitive stack and queue ADTs. Moreover, an important goal of this assignment is to be able to analyze an ADT that you will build yourself using...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

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