Loading...
「ツール」は右上に移動しました。
利用したサーバー: wtserver1
13いいね 579 views回再生

Implement a Queue with Two Stacks in Python | FIFO Simulation #youtubeshorts #youtube #trending

This Python program demonstrates how to implement a queue using two stacks. A queue is a linear data structure that follows the "First In, First Out" (FIFO) principle, while a stack follows the "Last In, First Out" (LIFO) principle. By using two stacks, we can simulate the behavior of a queue.

In this program, the QueueUsingStacks class contains two lists, stack1 and stack2, which represent the two stacks. The class provides two main methods:

enqueue(item): This method adds an item to the queue by pushing it onto stack1.
dequeue(): This method removes the front item from the queue. If stack2 is empty, all the elements from stack1 are popped and pushed onto stack2. This reversal makes the elements appear in the correct order for the queue. Afterward, the top element of stack2 is popped and returned as the dequeued element.
This approach ensures that the queue maintains the FIFO order, even though stacks typically follow the LIFO order. If both stacks are empty, the dequeue() method will return "Queue is empty," indicating that there are no elements to remove.

Code Walkthrough:
Initialization: The QueueUsingStacks class initializes two stacks (stack1 and stack2) to store the elements.
Enqueue: Elements are added to stack1 by appending them.
Dequeue: When an element is removed from the queue, we first check if stack2 is empty. If it is, we pop all elements from stack1 and push them into stack2. After this, we pop the top element from stack2, which corresponds to the front of the queue.
The program simulates the behavior of a queue using two stacks and ensures that elements are dequeued in the correct order.

class QueueUsingStacks:
def __init__(self):
self.stack1 = [] # Main stack
self.stack2 = [] # Temporary stack

def enqueue(self, item):
self.stack1.append(item) # Push to stack1

def dequeue(self):
if not self.stack2: # If stack2 is empty, move elements from stack1
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2.pop() if self.stack2 else "Queue is empty"

queue = QueueUsingStacks()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(queue.dequeue()) # Output: 1
print(queue.dequeue()) # Output: 2

queue using two stacks, Python queue implementation, stack to queue, data structure Python, FIFO queue, stack, queue simulation, Python tutorial, interview questions, Python data structures, Python coding challenges, queue and stack, two stack solution, enqueue dequeue, queue simulation in Python.
queue, stacks, Python, enqueue, dequeue, Python tutorial, data structures, FIFO, stack implementation, queue simulation, coding challenges, Python interview questions, Python programming, algorithms, two stacks, Python queue.

#Queue #Stacks #Python #Enqueue #Dequeue #PythonTutorial #DataStructures #FIFO #PythonCoding #CodingChallenges #InterviewQuestions #PythonAlgorithms #QueueSimulation #TwoStacks #PythonProgramming #youtubeshorts #youtube #trending #trend #viralvideo #viralshort

コメント