VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 564|回复: 1

[已解决]关于队列问题的请教

[复制链接]
29_avatar_middle
最佳答案
0 
在线会员 发表于 2020-4-18 09:03:38 | 显示全部楼层 |阅读模式
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;
       
}
我百度的结果都是两个队列去实现一个栈,请问这道题是不是有问题?到底怎么去实现这个主函数呢?原则上几个队列可以去满足题干给定的条件呢?
最佳答案
57_avatar_small
2020-4-18 09:03:39
用一个中间栈就可以实现该功能。然后吐槽一下栈的打印竟然调用了Pop函数,打印会把数据清空
  1. #include<iostream>
  2. #include "LinearQueue.h"
  3. #include "LinearStack.h"
  4. using namespace std;

  5. int main()
  6. {
  7.         int nMaxLength = 10;
  8.         LinearQueue<int> linearQueue(nMaxLength);
  9.         for (int i = 0; i < nMaxLength; i++)
  10.         {
  11.                 linearQueue.Insert(i);
  12.         }
  13.         cout <<        "linearQueue" << endl <<linearQueue << endl;

  14.         LinearStack<int> linearStackCache(nMaxLength);
  15.         for (int  i = 0; i < nMaxLength; i++)
  16.         {
  17.                 int nValue = 0;
  18.                 bool bRet = linearQueue.Delete(nValue);
  19.                 if (bRet)
  20.                 {
  21.                         linearStackCache.Push(nValue);
  22.                 }
  23.         }

  24.         LinearStack<int> linearStack(nMaxLength);
  25.         for (int i = 0; i < nMaxLength; i++)
  26.         {
  27.                 int nValue = 0;
  28.                 bool bRet = linearStackCache.Pop(nValue);
  29.                 if (bRet)
  30.                 {
  31.                         linearStack.Push(nValue);
  32.                 }
  33.         }
  34.         cout << "linearStack" << endl << linearStack << endl;

  35.         return 0;
  36. }
复制代码

最佳答案

查看完整内容

用一个中间栈就可以实现该功能。然后吐槽一下栈的打印竟然调用了Pop函数,打印会把数据清空




上一篇:实用c++ 第22课 地址与指针 小作业
下一篇:做win逆向还有前景吗
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-18 09:03:39 | 显示全部楼层    本楼为最佳答案   
bestAnswer
用一个中间栈就可以实现该功能。然后吐槽一下栈的打印竟然调用了Pop函数,打印会把数据清空
  1. #include<iostream>
  2. #include "LinearQueue.h"
  3. #include "LinearStack.h"
  4. using namespace std;

  5. int main()
  6. {
  7.         int nMaxLength = 10;
  8.         LinearQueue<int> linearQueue(nMaxLength);
  9.         for (int i = 0; i < nMaxLength; i++)
  10.         {
  11.                 linearQueue.Insert(i);
  12.         }
  13.         cout <<        "linearQueue" << endl <<linearQueue << endl;

  14.         LinearStack<int> linearStackCache(nMaxLength);
  15.         for (int  i = 0; i < nMaxLength; i++)
  16.         {
  17.                 int nValue = 0;
  18.                 bool bRet = linearQueue.Delete(nValue);
  19.                 if (bRet)
  20.                 {
  21.                         linearStackCache.Push(nValue);
  22.                 }
  23.         }

  24.         LinearStack<int> linearStack(nMaxLength);
  25.         for (int i = 0; i < nMaxLength; i++)
  26.         {
  27.                 int nValue = 0;
  28.                 bool bRet = linearStackCache.Pop(nValue);
  29.                 if (bRet)
  30.                 {
  31.                         linearStack.Push(nValue);
  32.                 }
  33.         }
  34.         cout << "linearStack" << endl << linearStack << endl;

  35.         return 0;
  36. }
复制代码
test.png
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-10 08:44

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表