一、栈和队列
栈和队列是非常常见的数据结构,都是限制存取点的线性结构,在计算机领域中被广泛应用,比如操作系统会给每个线程创建一个栈用来存储函数调用时各个函数的参数、返回地址及临时变量等。栈的特点是后进先出,即最后被压入栈的元素会第一个被弹出,栈与递归联系紧密。队列的特点是先入先出,只允许在表的一端进行插入,在另一端删除元素,队列主要应用于图的遍历。
4)如果queue1不空,queue2为空,插入元素到queue1,删除操作:queue1中元素压入到queue2,queue1中所剩最后一个元素弹出即可。
栈和队列是非常常见的数据结构,都是限制存取点的线性结构,在计算机领域中被广泛应用,比如操作系统会给每个线程创建一个栈用来存储函数调用时各个函数的参数、返回地址及临时变量等。栈的特点是后进先出,即最后被压入栈的元素会第一个被弹出,栈与递归联系紧密。队列的特点是先入先出,只允许在表的一端进行插入,在另一端删除元素,队列主要应用于图的遍历。
虽然栈和队列是特点针锋相对的两个数据结构,但它们之间却相互联系。
首先创建两个栈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;
}