使用单队列实现堆栈
我们得到了队列数据结构,任务是仅使用给定的队列数据结构来实现堆栈。
我们已经讨论了使用两个队列的解决方案。在本文中,讨论了一种仅使用一个队列的新解决方案。这个解决方案假设我们可以在任何时候找到队列的大小。这个想法是将新插入的元素始终保持在队列的前面,保持先前元素的顺序相同。下面是完整的步骤。
// x is the element to be pushed and s is stack
push(s, x)
1) Let size of q be s.
1) Enqueue x to q
2) One by one Dequeue s items from queue and enqueue them.
// Removes an item from stack
pop(s)
1) Dequeue an item from q
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。
下面是这个想法的实现。
C++
// C++ program to implement a stack using
// single queue
#include
using namespace std;
// User defined stack that uses a queue
class Stack
{
queueq;
public:
void push(int val);
void pop();
int top();
bool empty();
};
// Push operation
void Stack::push(int val)
{
// Get previous size of queue
int s = q.size();
// Push current element
q.push(val);
// Pop (or Dequeue) all previous
// elements and put them after current
// element
for (int i=0; i
Java
// Java program to implement stack using a
// single queue
import java.util.LinkedList;
import java.util.Queue;
public class stack
{
Queue q = new LinkedList();
// Push operation
void push(int val)
{
// get previous size of queue
int size = q.size();
// Add current element
q.add(val);
// Pop (or Dequeue) all previous
// elements and put them after current
// element
for (int i = 0; i < size; i++)
{
// this will add front element into
// rear of queue
int x = q.remove();
q.add(x);
}
}
// Removes the top element
int pop()
{
if (q.isEmpty())
{
System.out.println("No elements");
return -1;
}
int x = q.remove();
return x;
}
// Returns top of stack
int top()
{
if (q.isEmpty())
return -1;
return q.peek();
}
// Returns true if Stack is empty else false
boolean isEmpty()
{
return q.isEmpty();
}
// Driver program to test above methods
public static void main(String[] args)
{
stack s = new stack();
s.push(10);
s.push(20);
System.out.println("Top element :" + s.top());
s.pop();
s.push(30);
s.pop();
System.out.println("Top element :" + s.top());
}
}
// This code is contributed by Rishabh Mahrsee
Python3
# Python3 program to implement stack using a
# single queue
q = []
# append operation
def append(val):
# get previous size of queue
size = len(q)
# Add current element
q.append(val);
# Pop (or Dequeue) all previous
# elements and put them after current
# element
for i in range(size):
# this will add front element into
# rear of queue
x = q.pop(0);
q.append(x);
# Removes the top element
def pop():
if (len(q) == 0):
print("No elements");
return -1;
x = q.pop(0);
return x;
# Returns top of stack
def top():
if(len(q) == 0):
return -1;
return q[-1]
# Returns true if Stack is empty else false
def isEmpty():
return len(q)==0;
# Driver program to test above methods
if __name__=='__main__':
s = []
s.append(10);
s.append(20);
print("Top element :" + str(s[-1]));
s.pop();
s.append(30);
s.pop();
print("Top element :" + str(s[-1]));
# This code is contributed by rutvik_56.
C#
// C# program to implement stack using a
// single queue
using System;
using System.Collections.Generic;
public class stack
{
Queue q = new Queue();
// Push operation
void push(int val)
{
// get previous size of queue
int size = q.Count;
// Add current element
q.Enqueue(val);
// Pop (or Dequeue) all previous
// elements and put them after current
// element
for (int i = 0; i < size; i++)
{
// this will add front element into
// rear of queue
int x = q.Dequeue();
q.Enqueue(x);
}
}
// Removes the top element
int pop()
{
if (q.Count == 0)
{
Console.WriteLine("No elements");
return -1;
}
int x = q.Dequeue();
return x;
}
// Returns top of stack
int top()
{
if (q.Count == 0)
return -1;
return q.Peek();
}
// Returns true if Stack is empty else false
bool isEmpty()
{
if(q.Count == 0)
return true;
return false;
}
// Driver program to test above methods
public static void Main(String[] args)
{
stack s = new stack();
s.push(10);
s.push(20);
Console.WriteLine("Top element :" + s.top());
s.pop();
s.push(30);
s.pop();
Console.WriteLine("Top element :" + s.top());
}
}
// This code has been contributed by Rajput-Ji
Javascript
输出 :
20
10