怎么用两个堆栈实现一个队列?

本文转自:http://zouduowei.blog.163.com/blog/static/256191520083257113379/

 具体思路:

        两个栈重叠,并且栈的方向相反,栈底为另一个栈的栈顶,栈顶为另一个栈的栈底,一个栈只允许压栈,另一个栈只允许出栈,进队列的操作就是通过只允许压栈的栈进,出队列的操作从只允许出栈的栈出

//用两个堆栈表示一个队列。
#include "iostream.h"
#include "malloc.h"
typedef struct Stack //定义堆栈
{
 int *base;
 int *top;
 int stacksize;
}Stack;
int InitStack(Stack &s)  //初始化堆栈
{
 s.base=(int *)malloc(100*sizeof(int));
 if(!s.base)
  return 0;
 s.top=s.base;
 s.stacksize=100;
 return 1;
}
int DestroyStack(Stack &s)  //销毁堆栈
{
 free (s.base);
 free (s.top);
 return 1;
}
void ClearStack(Stack &s)  //清除堆栈中的内容
{
 s.top=s.base;
}
int StackEmpty(Stack s)  //判断堆栈是否为空
{
 if(s.base==s.top)
  return 1;
 else return 0;
}
int StackLength(Stack s)  //堆栈的长度
{
 return (s.top-s.base);
}

int GetTop(Stack s,int &e)  //获取堆栈的数据
{
 if(s.top==s.base)
  return 0;
 e=*(s.top-1);
 return e;
}

int Push(Stack &s,int &e)  //进栈
{
 if(s.top-s.base>=s.stacksize)
 {
  s.base=(int *)realloc(s.base,(s.stacksize+10)*sizeof(int));
   if(!s.base)
    return 0;
  s.top=s.base+s.stacksize;
  s.stacksize+=10;
 }
 *s.top++=e;
 s.stacksize++;
 return 1;

}

int Pop(Stack &s,int &e)  //出栈
{
 if(s.top==s.base)
  return 0;
 e=*--s.top;
 --s.stacksize;
 return e;

}

typedef struct queue //定义队列
{
 Stack a;
 Stack b;

}Queue;

int InitQueue(Queue &q)  //初始化队列
{
 InitStack(q.a);
 InitStack(q.b);
 return 1;

}

int DestroyQueue(Queue &q)  //销毁队列
{
 DestroyStack(q.a);
 DestroyStack(q.b);
 if(DestroyStack(q.a)&&DestroyStack(q.b))
  return 1;

}

void ClearQueue(Queue &q)  //清除队列
{
 ClearStack(q.a);
 ClearStack(q.b);
}

int QueueEmpty(Queue q)
{
 if(StackEmpty(q.a)==1&&StackEmpty(q.b)==1)
  return 1;//返回1表示队列为空
 else return 0;

}

int QueueLength(Queue q)
{
 return ((q.a.top-q.a.base)+(q.b.top-q.b.base));

}

int GetQueueTop(Queue q,int &e)  //获取队列的数据
{
 if(QueueEmpty(q)==0)
  return GetTop(q.b,e);
 else return 0;

}

int PushQueue(Queue &q,int &e)  //进队
{
 Push(q.a,e);
 q.a.stacksize++;
 if(Push(q.a,e))
  return 1;
  else return 0;
}
int PopQueue(Queue &q,int &temp)  //出队
{
 int e;
 if(StackEmpty(q.b)==1)
 {
  if(StackEmpty(q.a)==1)
   return 0;
  else
   while(StackEmpty(q.a)==0)
   {
    q.a.top--;
    *q.b.top=*q.a.top;
    q.b.top++;
   }
 }
 temp=Pop(q.b,e);
 cout<<temp;
 q.b.stacksize--;
 if(Pop(q.b,e))
  return 1;
 else return 0;
}
//主函数部分
int main(int argc, char* argv[])
{           //…………………………队列的实现部分………………………………
 Queue q_a;
 int e[7]={1,2,3,6,4,77,1};
 int b[7]={0};
 InitQueue(q_a);
 cout<<"进队顺序为:";
 if(QueueEmpty(q_a)==1)
 {
  for(int i=1;i<=7;i++)
  {
   PushQueue(q_a,e[i-1]);
   cout<<e[i-1]<<" ";
  }
 }
 cout<<endl;
 cout<<"出队顺序为:";
 for(int i=1;i<=7;i++)
 {
  PopQueue(q_a,b[i]);
  cout<<" ";

 }cout<<endl;
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用两个堆栈实现队列的算法如下: 1. 创建两个堆栈stack1和stack2,分别用于存储队列中的元素。 2. 元素入队操作:将元素压入stack1中。 3. 元素出队操作:先检查stack2是否为空,如果不为空,则直接弹出stack2的栈顶元素作为队列的出队元素;如果stack2为空,则将stack1中的所有元素依次弹出并压入stack2中,然后再从stack2中弹出栈顶元素作为队列的出队元素。 4. 元素获取操作:同出队操作,只是不弹出栈顶元素。 下面是使用Java语言实现的代码: ```java import java.util.Stack; public class QueueWithTwoStacks<T> { private Stack<T> stack1 = new Stack<>(); private Stack<T> stack2 = new Stack<>(); public void enqueue(T item) { stack1.push(item); } public T dequeue() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.pop(); } public T peek() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.peek(); } public boolean isEmpty() { return stack1.isEmpty() && stack2.isEmpty(); } public int size() { return stack1.size() + stack2.size(); } } ``` 在上面的代码中,enqueue()方法将元素压入stack1中,dequeue()方法先检查stack2是否为空,如果不为空,则直接弹出stack2的栈顶元素作为队列的出队元素;如果stack2为空,则将stack1中的所有元素依次弹出并压入stack2中,然后再从stack2中弹出栈顶元素作为队列的出队元素。peek()方法也是同理。isEmpty()方法检查队列是否为空,size()方法返回队列中元素的个数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值