wt_tong 2023-03-13 10:23 采纳率: 40%
浏览 81
已结题

如何实现循环队列和循环栈?(语言-c++)

  • 写回答

6条回答 默认 最新

  • 关注

    人工作答:队列是先进先出,栈是后进先出,他们可以用同样的结构体存储数据,只是在出队或者出栈时不一样。
    代码中有详细的注释,运行结果如下:

    img

    代码:

    #include <iostream>
    using namespace std;
    
    //定义数据类型,可根据需要调整
    typedef int datatype;
    
    #define MY_OVERFLOW (-1)
    #define MY_OK (1)
    #define MY_ERROR (0)
    
    //节点结构
    typedef struct _node
    {
        datatype data;
        struct _node* next;
    }Node;
    
    typedef struct _dataStruct
    {
        int maxLen;  //链表总长度
        Node* head;  //链表头
        Node* front; //当前数据头节点
        Node* last;  //当前数据尾节点
    }CQueue, CStack;
    
    /**   循环链表实现队列  **/
    //1.初始化,队列的初始采用尾插法创建链表
    void initQueue(CQueue& q,int maxSize = 100)
    {
        q.maxLen = maxSize;
        q.head = new Node; //附加头节点
        q.head->data = 0; //当前实际存储的数据个数
        q.head->next = 0;
    
        Node* p = 0, * t;
        t = q.head;
        //创建maxSize个节点
        for (int i = 0; i < maxSize;i++)
        {
            p = new Node;
            p->next = 0;
            t->next = p;
            t = p;
        }
        
        p->next = q.head;        //尾节点连接头节点
        q.front = q.head->next;  //初始化当前数据节点的头部
        q.last = q.front;
    }
    
    //2.入队
    int pushQueue(CQueue& q, datatype d)
    {
        if (q.head->data == q.maxLen)
            return MY_OVERFLOW;
        else
        {
            q.last->data = d;
            q.last = q.last->next;
            if (q.last == q.head) //跳过附加头节点
                q.last = q.head->next;
                
            q.head->data += 1; //个数+1
            return MY_OK;
        }
    }
    
    //3.出队
    int popQueue(CQueue& q, datatype& d)
    {
        if (q.head->data == 0)
            return MY_ERROR;
        else
        {
            d = q.front->data;
            q.front = q.front->next;
            if (q.front == q.head) //如果下一个节点是附加头节点,跳过
                q.front = q.front->next; 
            q.head->data -= 1; //个数-1
            return MY_OK;
        }
    }
    
    //4.获取队列元素个数
    int getQueueLength(CQueue& q)
    {
        return q.head->data;
    }
    /**   循环链表实现栈  **/
    //1.初始化
    void initStack(CStack& s, int maxSize = 100)
    {
        s.maxLen = maxSize;
        s.head = new Node; //附加头节点
        s.head->data = 0; //当前实际存储的数据个数
        s.head->next = 0;
    
        Node* p = 0, * t;
        t = s.head;
        //创建maxSize个节点
        for (int i = 0; i < maxSize; i++)
        {
            p = new Node;
            p->next = 0;
            t->next = p;
            t = p;
        }
    
        p->next = s.head;        //尾节点连接头节点
        s.front = s.head->next;  //初始化当前数据节点的头部
        s.last = s.front;
    }
    
    //2.入栈
    int pushStack(CStack& s, datatype d)
    {
        if (s.head->data == s.maxLen)
            return MY_OVERFLOW;
        else
        {
            s.last->data = d;
            s.last = s.last->next;
            if (s.last == s.head) //跳过附加头节点
                s.last = s.head->next;
                
            s.head->data += 1; //个数+1
            return MY_OK;
        }
    }
    //3.出栈
    int popStack(CStack& s, datatype& d)
    {
        if (s.head->data == 0)
            return MY_ERROR;
        else
        {
            Node* pre,*pp=0;
    
            pre = s.front; //当前节点
            while (pre->next != s.last)
            {
                if (pre->next == s.head)
                    pp = pre; //记录链表的最后一个节点
                pre = pre->next;
            }
    
            if (pre == s.head) //跳过附加节点
            {
                pre = pp;
            }
    
            d = pre->data; //后进先出
            s.last = pre; //尾节点前移
            
            s.head->data -= 1; //个数-1
            return MY_OK;
        }
    }
    
    //4.获取队列长度
    int getStackLength(CStack& s)
    {
        return s.head->data;
    }
    
    //测试
    int main()
    {
        CQueue que; //队列
        CStack sta; //栈
    
        initQueue(que); //初始化队列
        initStack(sta); //初始化栈
        int n, t;
        cout << "请输入入队元素个数:";
        cin >> n;
        cout << "请输入" << n << "个整数入队:" << endl;
        for (int i = 0; i < n; i++)
        {
            cin >> t;
            pushQueue(que, t); //入队
        }
        cout << "队列当前元素个数:" << getQueueLength(que) << endl;
        cout << "队列元素依次出队:" << endl;
        while (getQueueLength(que) > 0)
        {
            popQueue(que, t);
            cout << t << " ";
        }
        cout << endl;
    
    
        cout << "请输入栈元素个数:";
        cin >> n;
        cout << "请输入" << n << "个整数入栈:" << endl;
        for (int i = 0; i < n; i++)
        {
            cin >> t;
            pushStack(sta, t); //入栈
        }
        cout << "栈当前元素个数:" << getStackLength(sta) << endl;
        cout << "栈元素依次出栈:" << endl;
        while (getStackLength(sta) > 0)
        {
            popStack(sta, t);
            cout << t << " ";
        }
        cout << endl;
        return 0;
    }
    
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 3月13日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效