当前位置:网站首页>线性结构:栈和队列
线性结构:栈和队列
2022-07-30 19:57:00 【@布响丸辣】
两个栈实现一个队列和两个队列实现一个栈
目录
1.mian.cpp
#include "Stack.h"
#include "Queue.h"
//两个队列实现栈
void StackPush(Queue& q1, Queue& q2, int n)
{
//找到一个非空队列 将元素入队
if (q1.size != 0)
{
q1.Push(n);
}
else
{
q2.Push(n);
}
}
int StackPop(Queue& q1, Queue& q2)
{
//找到一个非空队列 将该队列中除队尾元素外的所有元素入队到另一个队中 将该队尾元素返回
if (q1.size == 0 && q2.size == 0)
{
cout << "栈中无元素" << endl;
return -1;
}
int value;
if (q1.size != 0)
{
while (q1.size > 1)
{
q2.Push(q1.Pop());
}
value = q1.Pop();
}
else
{
while (q2.size > 1)
{
q1.Push(q2.Pop());
}
value = q2.Pop();
}
return value;
}
//------------------------------------------------------------------------------------
//两个栈实现一个队列
void QueuePush(Stack& SPush, Stack& SPop, int n)
{
//将出队栈的元素全部倒入入队栈中 将新元素入到入队栈中
/*while (SPop.size > 0)
{
SPush.Push(SPop.Pop());
}*/
//SPush.Push(n);
}
int QueuePop(Stack& SPush, Stack& SPop)
{
if (SPush.size == 0 && SPop.size == 0)
{
cout << "队列中无元素" << endl;
return -1;
}
//将入队中元素全部倒入到出队栈中 将出队栈栈顶弹出
if (SPop.size == 0)
{
while (SPush.size > 0)
{
SPop.Push(SPush.Pop());
}
}
return SPop.Pop();
}
int main()
{
Stack SPush;
Stack SPop;
QueuePush(SPush , SPop,1);
QueuePush(SPush , SPop,2);
QueuePush(SPush , SPop,3);
QueuePush(SPush , SPop,4);
cout << QueuePop(SPush, SPop) << endl;
cout << QueuePop(SPush, SPop) << endl;
cout << QueuePop(SPush, SPop) << endl;
cout << QueuePop(SPush, SPop) << endl;
//----------------------------------------------------------------------------------
/*Queue q1;
Queue q2;
StackPush(q1, q2, 1);
StackPush(q1, q2, 2);
StackPush(q1, q2, 3);
cout << StackPop(q1, q2) << endl;
cout << StackPop(q1, q2) << endl;
StackPush(q1, q2, 5);
cout << StackPop(q1, q2) << endl;
cout << StackPop(q1, q2) << endl;*/
--------------------------------------------------------------------
/*Stack s;
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
cout << s.Pop() << endl;
cout << s.Pop() << endl;
cout << s.Pop() << endl;
cout << s.Pop() << endl;
cout << s.Pop() << endl;
Queue q;
q.Push(1);
q.Push(2);
q.Push(3);
q.Push(4);
cout << q.Pop() << endl;
cout << q.Pop() << endl;
cout << q.Pop() << endl;
cout << q.Pop() << endl;
cout << q.Pop() << endl;
return 0;*/
//}
2.Stack.h
#pragma once
#include <iostream>
using namespace std;
class Stack
{
struct Node
{
int value;
Node* pNext;
};
public:
Node* pTop;
int size;
public:
Stack();
~Stack();
public:
void Push(int n);
int Pop();
};
3.Stack.cpp
#include "Stack.h"
Stack::Stack()
{
pTop = NULL;
size = 0;
}
Stack::~Stack()
{
while (pTop != NULL)
{
Node* pDel = pTop;
pTop = pTop->pNext;
delete pDel;
pDel = NULL;
}
}
void Stack::Push(int n)
{
Node* pTemp = new Node;
pTemp->value = n;
pTemp->pNext = NULL;
pTemp->pNext = pTop;
pTop = pTemp;
size++;
}
int Stack::Pop()
{
if (size == 0)
{
cout << "栈为空" << endl;
return -1;
}
Node* pDel = pTop;
int nMark = pTop->value;
pTop = pTop->pNext;
delete pDel;
pDel = NULL;
size--;
return nMark;
}
4.Queue.h
#pragma once
#include <iostream>
using namespace std;
class Queue
{
struct Node
{
int value;
Node* pNext;
};
public:
Node* pHead;
Node* pEnd;
int size;
public:
Queue();
~Queue();
public:
void Push(int n);
int Pop();
};
5.queue.cpp
#include "Queue.h"
Queue::Queue()
{
pHead = NULL;
pEnd = NULL;
size = 0;
}
Queue::~Queue()
{
while (pHead != NULL)
{
Node* pDel = pHead;
delete pDel;
pDel = NULL;
pHead = pHead->pNext;
}
}
void Queue::Push(int n)
{
Node* pTemp = new Node;
pTemp->value = n;
pTemp->pNext = NULL;
if (!pHead)
{
pHead = pTemp;
}
else
{
pEnd->pNext = pTemp;
}
pEnd = pTemp;
size++;
}
int Queue::Pop()
{
if (size == 0)
{
cout << "队列为空" << endl;
return -1;
}
Node* pDel = pHead;
int nMark = pHead->value;
pHead = pHead->pNext;
delete pDel;
pDel = NULL;
size--;
return nMark;
}
边栏推荐
- coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products
- Scala类中的属性
- 【请教】SQL语句按列1去重来计算列2之和?
- MySQL database - views and indexes
- These services can't ali interview?Then don't go to, the basic notification, etc
- 推荐系统:开源项目/工具【谷歌:TensorFlow Recommenders】【Facebook:TorchRec】【百度:Graph4Rec】【阿里:DeepRec和EasyRec】
- win2003下FTP服务器如何搭建
- 数据库索引:索引并不是万能药
- el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
- [Private Series] All kinds of strange problems encountered in daily PHP
猜你喜欢

Is the iPhone really thirteen incense?The two generations of products are completely compared, perhaps the previous generation is more worth buying

KEIL问题:【keil Error: failed to execute ‘C:\Keil\ARM\ARMCC‘】

MySQL database - DQL data query language

How to build FTP server under win2003

DCM 中间件家族迎来新成员

MySQL slow query optimization

MySQL数据库————视图和索引

Install MySQL tutorial under Linux

These services can't ali interview?Then don't go to, the basic notification, etc
![[hbuilder] cannot run some projects, open the terminal and cannot enter commands](/img/fa/63f36683d090558f3fe5f582d86ca0.png)
[hbuilder] cannot run some projects, open the terminal and cannot enter commands
随机推荐
ELK日志分析系统
iPhone真是十三香?两代产品完全对比,或许上一代更值得买
移动web开发01
18.客户端会话技术Cookie
MySQL database master-slave configuration
Face-based Common Expression Recognition (2) - Data Acquisition and Arrangement
MySQL八股文背诵版
The JDBC programming of the MySQL database
使用MULTISET来比较数据集的实例介绍
看完《二舅》,我更内耗了
银行数据资产转换能力弱?思迈特软件助力解决银行困境
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
Cesium加载离线地图和离线地形
一文2500字手把手教你配置Jenkins自动化邮件通知
Recommended system: cold start problem [user cold start, item cold start, system cold start]
倾斜文档扫描与字符识别(opencv,坐标变换分析)
Weak Banks to data conversion ability?Matt software help solve bank dilemma
OSS简单上传图片
MySQL eight-part text recitation version
M3SDA: Moment matching for multi-source domain adaptation