|
4驿站币
#include <iostream>
using namespace std;
template<class T>
class LinearStack
{
public:
LinearStack(int LSMaxSize); //构造函数,创建空栈
~LinearStack(); //析构函数,删除栈
bool IsEmpty();
//判断栈是否为空,空返回true,非空返回false
bool IsFull();
//判断栈是否为满,满返回true,不满返回false
int GetElementNumber(); //求栈中元素的个数
bool Push(const T& x);
//在栈顶插入元素x,插入成功返回true,不成功返回false
bool Top(T& x);
//求栈顶元素的值放入x中,成功返回true,失败返回false
bool Pop(T& x);
//从栈顶删除一个元素,并将该元素的值放入x中
void OutPut(ostream& out);
// 将顺序栈放到输出流out中输出
private:
int top; //用来表示栈顶
int MaxSize; //栈中最多元素个数
T *element; //一维动态数组
};
//实现构造函数
template<class T>
LinearStack<T>::LinearStack(int LSMaxSize)
{
MaxSize=LSMaxSize;
element=new T[LSMaxSize];
top=-1;
}
//实现析构函数
template<class T>
LinearStack<T>::~LinearStack()
{
delete []element;
}
//实现判断栈是否为空
template<class T>
bool LinearStack<T>::IsEmpty()
{
return top==-1;
}
//实现判断栈是否为满
template<class T>
bool LinearStack<T>::IsFull()
{
return top==MaxSize;
}
//实现求栈中元素的个数
template<class T>
int LinearStack<T>::GetElementNumber()
{
return top+1;
}
//实现进栈
template<class T>
bool LinearStack<T>::Push(const T& x)
{
if (IsFull())
return false;
else
{
top++;
element[top]=x;
return true;
}
}
//实现求栈顶元素
template<class T>
bool LinearStack<T>::Top(T& x)
{
if (IsEmpty())
return false;
else
{
x=element[top];
return true;
}
}
//实现出栈
template<class T>
bool LinearStack<T>::Pop(T& x)
{
if (IsEmpty())
return false;
else
{
x=element[top];
top--;
return true;
}
}
//实现顺序栈的输出
template<class T>
void LinearStack<T>:: OutPut(ostream& out)
{
T x;
while(!IsEmpty())
{
Pop(x);
out<<x<<endl;
}
}
//重载插入运算符<<
template<class T>
ostream& operator<<(ostream& out, LinearStack<T>& x)
{
x.OutPut(out);
return out;
}
#ifndef LINEARQUEUE
#define LINEARQUEUE
template<class T>
class LinearQueue
{
public:
LinearQueue(int LQMaxSize); //创建空队列
~LinearQueue(); //删除队列
bool IsEmpty();
//判断队列是否为空,空返回true,非空返回false
bool IsFull();
//判断队列是否为满,满返回true,不满返回false
bool Insert(const T& x); //入队,在队列尾部插入元素x
bool GetElement(T& x); //求队头元素的值放入x中
bool Delete(T& x);
//出队,从队头删除一个元素,并将该元素的值放入x中
void OutPut(ostream& out) const;
int GetLength(); //获取队列长度
//输出队列
private:
int size; //队列实际元素个数
int MaxSize; //队列中最大元素个数
int front,rear; //队列的队头和队尾指针
T *element; //一维动态数组
};
//实现构造函数
template<class T>
LinearQueue<T>::LinearQueue(int LQMaxSize)
{
MaxSize=LQMaxSize;
element=new T[MaxSize];
size=0;
front=0;
rear=0;
}
//实现析构函数
template<class T>
LinearQueue<T>::~LinearQueue()
{
delete []element;
}
//实现判断栈是否为空
template<class T>
bool LinearQueue<T>::IsEmpty()
{
return size==0;
}
//实现判断栈是否为满
template<class T>
bool LinearQueue<T>::IsFull()
{
return size==MaxSize;
}
//实现入队
template<class T>
bool LinearQueue<T>::Insert(const T& x)
{
if (IsFull())
return false;
else
{
element[rear]=x;
rear=(rear+1)%(MaxSize);
size++;
return true;
}
}
//实行求队头元素
template<class T>
bool LinearQueue<T>::GetElement(T& x)
{
if (IsEmpty())
return false;
else
{
x=element[front];
return true;
}
}
//实现出队
template<class T>
bool LinearQueue<T>::Delete(T& x)
{
if (IsEmpty())
return false;
else
{
x=element[front];
front=(front+1)%(MaxSize);
size--;
return true;
}
}
//实现获取队列长度
template<class T>
int LinearQueue<T>::GetLength()
{
return size;
}
//实现顺序队列的输出
template<class T>
void LinearQueue<T>::OutPut(ostream& out) const
{ int index;
index=front;
for(int i=0;i<size;i++)
{
out<<element[index]<<endl;
index=(index+1)%MaxSize;
}
}
//重载插入运算符<<
template<class T>
ostream& operator<<(ostream& out,const LinearQueue<T>& x)
{
x.OutPut(out);
return out;
}
#endif
如何利用这两个头文件去实现利用一个队列中元素创建一个栈的算法,同时要求:队头作为栈顶,队尾作为栈底,创建栈后队列保持不变 ?
要求写好主函数。
#include<iostream>
using namespace std;
#include "LinearQueue.h"
#include "LinearStack.h"
int main()
{
return 0;
}
我百度的结果都是两个队列去实现一个栈,请问这道题是不是有问题?到底怎么去实现这个主函数呢?原则上几个队列可以去满足题干给定的条件呢?
用一个中间栈就可以实现该功能。然后吐槽一下栈的打印竟然调用了Pop函数,打印会把数据清空
- #include<iostream>
- #include "LinearQueue.h"
- #include "LinearStack.h"
- using namespace std;
- int main()
- {
- int nMaxLength = 10;
- LinearQueue<int> linearQueue(nMaxLength);
- for (int i = 0; i < nMaxLength; i++)
- {
- linearQueue.Insert(i);
- }
- cout << "linearQueue" << endl <<linearQueue << endl;
- LinearStack<int> linearStackCache(nMaxLength);
- for (int i = 0; i < nMaxLength; i++)
- {
- int nValue = 0;
- bool bRet = linearQueue.Delete(nValue);
- if (bRet)
- {
- linearStackCache.Push(nValue);
- }
- }
- LinearStack<int> linearStack(nMaxLength);
- for (int i = 0; i < nMaxLength; i++)
- {
- int nValue = 0;
- bool bRet = linearStackCache.Pop(nValue);
- if (bRet)
- {
- linearStack.Push(nValue);
- }
- }
- cout << "linearStack" << endl << linearStack << endl;
- return 0;
- }
复制代码
|
最佳答案
查看完整内容
用一个中间栈就可以实现该功能。然后吐槽一下栈的打印竟然调用了Pop函数,打印会把数据清空
上一篇: 实用c++ 第22课 地址与指针 小作业下一篇: 做win逆向还有前景吗
|