|
| 1 | +''' Name : Abhinav kumar |
| 2 | +Github username : Abhinavcode13 |
| 3 | +Repository name : data-structures-and-algorithms |
| 4 | +Problem : Implement Queue using Stacks in Python |
| 5 | +Issue Number : #256 |
| 6 | +Problem statement : |
| 7 | +
|
| 8 | +Explanation of the below python code : |
| 9 | +
|
| 10 | +In the above code, the Queue class represents a queue implemented using two stacks. The enqueue method simply adds the element to the first stack (stack1). The dequeue method pops an element from the second stack (stack2) if it's not empty. If stack2 is empty, it transfers all elements from stack1 to stack2 in reverse order and then pops an element from stack2. |
| 11 | +
|
| 12 | +The program provides the user with a menu to select the operation they want to perform - enqueue, dequeue, or quit. The user can input the value to enqueue, and the program prints the enqueued or dequeued value accordingly. |
| 13 | +
|
| 14 | +Summary about the code and time complexity: |
| 15 | +
|
| 16 | +This code implements a queue using two stacks. |
| 17 | +The enqueue operation is simply implemented by appending an element to one of the stacks, while the dequeue operation involves reversing the order of the elements by popping from one stack and pushing onto the other, and then popping the top element from the second stack. |
| 18 | +The time complexity of the enqueue operation is O(1), while the time complexity of the dequeue operation is O(n) in the worst case, where n is the number of elements in the queue. This is because in the worst case, all the elements will need to be moved from one stack to the other during the dequeue operation. |
| 19 | +
|
| 20 | +
|
| 21 | +
|
| 22 | +----------------------------------------------------------------------------------------------------------//Python code begins here----------------------------------------------------------------------------------------------------------------------- |
| 23 | +''' |
| 24 | + |
| 25 | +class Queue: |
| 26 | + def __init__(self): |
| 27 | + # Initialize two empty stacks |
| 28 | + self.stack1 = [] |
| 29 | + self.stack2 = [] |
| 30 | + |
| 31 | + def enqueue(self, val): |
| 32 | + # Add an element to the end of the queue by appending it to stack1 |
| 33 | + self.stack1.append(val) |
| 34 | + |
| 35 | + def dequeue(self): |
| 36 | + # If stack2 is empty, reverse the order of the elements by popping from stack1 and pushing onto stack2 |
| 37 | + if not self.stack2: |
| 38 | + while self.stack1: |
| 39 | + self.stack2.append(self.stack1.pop()) |
| 40 | + # If stack2 is still empty, the queue is empty, so return None |
| 41 | + if not self.stack2: |
| 42 | + return None |
| 43 | + # Otherwise, pop the top element from stack2 and return it |
| 44 | + return self.stack2.pop() |
| 45 | + |
| 46 | +# Create an instance of the Queue class |
| 47 | +queue = Queue() |
| 48 | + |
| 49 | +# Enter into an infinite loop to prompt the user for operations |
| 50 | +while True: |
| 51 | + print("Select operation -\n" |
| 52 | + "1. Enqueue\n" |
| 53 | + "2. Dequeue\n" |
| 54 | + "3. Quit") |
| 55 | + |
| 56 | + # Prompt the user for their choice of operation |
| 57 | + choice = int(input("Enter choice: ")) |
| 58 | + |
| 59 | + if choice == 1: |
| 60 | + # If the user selects option 1, prompt them for a value to enqueue and enqueue it |
| 61 | + val = int(input("Enter value to enqueue: ")) |
| 62 | + queue.enqueue(val) |
| 63 | + print("Enqueued value:", val) |
| 64 | + elif choice == 2: |
| 65 | + # If the user selects option 2, dequeue a value and print it, or indicate that the queue is empty |
| 66 | + val = queue.dequeue() |
| 67 | + if val: |
| 68 | + print("Dequeued value:", val) |
| 69 | + else: |
| 70 | + print("Queue is empty.") |
| 71 | + elif choice == 3: |
| 72 | + # If the user selects option 3, quit the loop |
| 73 | + break |
| 74 | + else: |
| 75 | + # If the user selects an invalid option, prompt them to try again |
| 76 | + print("Invalid choice. Please try again.") |
| 77 | + |
0 commit comments