栈和队列的转换

一、栈和队列
   栈和队列是非常常见的数据结构,都是限制存取点的线性结构,在计算机领域中被广泛应用,比如操作系统会给每个线程创建一个栈用来存储函数调用时各个函数的参数、返回地址及临时变量等。栈的特点是后进先出,即最后被压入栈的元素会第一个被弹出,栈与递归联系紧密。队列的特点是先入先出,只允许在表的一端进行插入,在另一端删除元素,队列主要应用于图的遍历。  

    虽然栈和队列是特点针锋相对的两个数据结构,但它们之间却相互联系。


二、用两个栈实现队列

    首先创建两个栈stack1,stack2。我们通过下图来直观理解如何实现队列的插入和删除操作。

   

    我们可以总结出删除一个元素的步骤:当stack2中不为空时,在stack2中的栈顶元素是最先进入队列的元素,可以弹出。如果stack2中为空时,我们把stack1中的元素逐个弹出并压入stack2。由于先进入队列的元素被压到stack1的底端,经过弹出和压入之后就处于stack2的顶端了,又可以直接弹出。

    插入元素:直接压入到stack1。


三、用两个队列实现栈

    首先创建两个队列queue1,queue2。我们同样通过下图来直观理解如何实现栈的插入和删除操作。

   

    我们可以总结出插入和删除元素的步骤:

   1)如果queue1和queue2都为空,插入元素到queue1,此时栈为空,无法删除元素  

   2)如果queue1和queue2都不为空,插入元素到queue1,删除操作:queue1中元素压入到queue2,queue1中所剩最后一个元素弹出即可;

   3)如果queue1为空,queue2不空,插入元素到queue1,删除操作:queue2中元素压入到queue1,queue2中所剩最后一个元素弹出即可;   
   4)如果queue1不空,queue2为空,插入元素到queue1,删除操作:queue1中元素压入到queue2,queue1中所剩最后一个元素弹出即可。

四、代码实现


  两个栈实现队列

#pragma once
#include "SqStack.h"
class CQueue
{
public:
	CQueue(void);
	~CQueue(void);
	CQueue(int size);
	void AppendTail(int e);
	int DeleteHead();
private:
	SqStack stack1;
	SqStack stack2;
};

#include "StdAfx.h"
#include "Queue.h"
#include "SqStack.h"
#include<iostream>
using namespace std;

CQueue::CQueue(void)
{
}

CQueue::~CQueue(void)
{
}

CQueue::CQueue(int size)
{
	stack1.stacksize = stack2.stacksize = size;
	stack1.top = stack1.base = new int[size];
	stack2.top = stack2.base = new int[size];
}

void CQueue::AppendTail(int e)
{
	stack1.Push(e);
}

int CQueue::DeleteHead()
{
	int a, b;
	if(stack2.GetSize() == 0)
	{
		while(stack1.GetSize() != 0)
		{
			a = stack1.Pop();
			stack2.Push(a);
		}
	}
	if(stack2.GetSize() == 0)
	{
		cout<<"queue is empty"<<endl;
		return -1;//当队列元素全部删除时,返回-1
	}
	else 
	{
		b = stack2.Pop();
		return b;
	}
}

#pragma once

class SqStack
{
public:
	SqStack(void);
	~SqStack(void);
	SqStack(int size);
	void Push(int e);
	int Pop();
	int GetSize();
public:
	int stacksize;
	int* base;
	int* top;
};

#include "StdAfx.h"
#include "SqStack.h"
#include<iostream>
using namespace std;

SqStack::SqStack(void)
{
}

SqStack::~SqStack(void)
{
}

SqStack::SqStack(int size)
{
	stacksize = size;
	base = top = new int[size];
}

void SqStack::Push(int e)
{
	*top = e;
	top++;
}

int SqStack::Pop()
{
	int e;
	if(top == base)
	{
		cout<<"栈空"<<endl;
	}
	e = *--top;
	return e;
}

int SqStack::GetSize()
{
	if(top == base)
		return 0;
	else 
		return 1;
}

#include "stdafx.h"
#include "Queue.h"
#include "SqStack.h"
#include<iostream>
using namespace std;

int main()
{
	int a, b, n;
	CQueue c(10);
	cout<<"请输入队列元素个数"<<endl;
	cin>>n;
	cout<<"请输入队列元素"<<endl;
	for(int i = 0; i < n; ++i)
	{
		cin>>a;
		c.AppendTail(a);
	}
	b = c.DeleteHead();
	cin>>a;
	c.AppendTail(a);
	b = c.DeleteHead();
	b = c.DeleteHead();
	b = c.DeleteHead();
	cout<<b<<endl;
	system("pause");
	return 0;
}
   两个队列实现栈

#pragma once
class CQueue
{
public:
	CQueue(void);
	~CQueue(void);
	void InitCQueue();
	int QueueLength();
	void EnQueue(int e);
	int DeQueue();
public:
	int* base;
	int front;
	int rear;
};

#include "StdAfx.h"
#include "Queue.h"
#include<iostream>
using namespace std;
#define MAXQSIZE 10

CQueue::CQueue(void)
{
}

CQueue::~CQueue(void)
{
}

void CQueue::InitCQueue()
{
	base  = new int[MAXQSIZE];
	front = rear = 0;
}

int CQueue::QueueLength()
{
	return (rear - front + MAXQSIZE) % MAXQSIZE;
}

void CQueue::EnQueue(int e)
{
	if((rear + 1) % MAXQSIZE == front)
	{
		cout<<"队列满"<<endl;
		return;
	}
	base[rear] = e;
	rear = (rear + 1) % MAXQSIZE;
	return;
}

int CQueue::DeQueue()
{
	int e;
	if(front == rear)
	{
		cout<<"队列空"<<endl;
		//return;
	}
	e = base[front];
	front = (front + 1) % MAXQSIZE;
	return e;
}

#pragma once
#include "Queue.h"
class SqStack
{
public:
	SqStack(void);
	~SqStack(void);
	SqStack(int size);
	void Push(int e);
	int Pop();
private:
	CQueue cqueue1;
	CQueue cqueue2;
};

#include "StdAfx.h"
#include "SqStack.h"
#include "Queue.h"
#include<iostream>
using namespace std;

SqStack::SqStack(void)
{
}

SqStack::~SqStack(void)
{
}

SqStack::SqStack(int size)
{
	cqueue1.InitCQueue();
	cqueue2.InitCQueue();
}

void SqStack::Push(int e)
{
	cqueue1.EnQueue(e);
}

int SqStack::Pop()
{
	int a;
	if(cqueue1.QueueLength() == 0 && cqueue2.QueueLength() == 0)
	{
		cout<<"栈空"<<endl;
		//return ;
	}
	else if(cqueue1.QueueLength() != 0)
	{
		while(cqueue1.QueueLength() != 1)
		{
			a = cqueue1.DeQueue();
			cqueue2.EnQueue(a);
		}
		a = cqueue1.DeQueue();
		return a;
	}
	else if(cqueue1.QueueLength() == 0 && cqueue2.QueueLength() != 0)
	{
		while(cqueue2.QueueLength() != 1)
		{
			a = cqueue2.DeQueue();
			cqueue1.EnQueue(a);
		}
		a = cqueue2.DeQueue();
		return a;
	}
}

#include "stdafx.h"
#include "SqStack.h"
#include "Queue.h"
#include<iostream>
using namespace std;

int main()
{
	int r;
	SqStack s(10);
	s.Push(1);
	s.Push(2);
	r = s.Pop();
	cout<<r<<endl;
	s.Push(3);
	r = s.Pop();
	cout<<r<<endl;
	r = s.Pop();
	cout<<r<<endl;
	system("pause");
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值