zoukankan      html  css  js  c++  java
  • 队列的基本操作周期

    基本功能:1)初始化循环队列

                            2)求循环队列的长度

                            3)循环队列的插入删除操作

                            4) 推断循环队列是否为空。是否已经满了

                            5)遍历循环队列

    基本的原理:

    1)初始化 front = rear = 0;

    2)为空  front = rear   

    3)已满 front=(rear+1)%size 

    4)  插入 rear = (rear+1)%size 

    5)删除 front=(front+1)%size

    代码例如以下:

    /****************************************************************************
                              author---bigship
                             date----2014.10.24
    *****************************************************************************/
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;
    
    const int Size = 100;//循环队列的大小
    
    template <class T>
    
    struct Queue
    {
        T *base;
        int front;
        int rear;
        void init(){//初始化队列
            base = (T *)malloc(sizeof(T)*Size);
            front=rear=0;
        }
        bool empty(){//推断队列是否为空
            if(front==rear)
                return true;
            return false;
        }
        bool full(){//推断是否已经满了
            if((rear+1)%Size==rear)
                return true;
            return false;
        }
        void push(T x){//插入元素
            if(full()){
                printf("The queue is full
    ");
                return ;
            }
            base[rear] = x;
            rear = (rear + 1)%Size;
        }
        void pop(){//删除队首元素
            if(empty()){
                printf("The queue is empty
    ");
                return ;
            }
            front=(front+1)%Size;
        }
        T top(){//返回队首元素
            return base[front];
        }
        int length(){//队列长度
            return (rear-front+Size)%Size;
        }
        void destroy(){//销毁队列
            if(base) free(base);
            base = NULL;
            rear=front;
        }
        void visit(){//遍历队列
            if(empty()){
                printf("the queue is empty");
                return ;
            }
            int tmp = front;
            while(tmp!=rear){
                printf("%d ",base[tmp]);
                tmp=(tmp+1)%Size;
            }
            puts("");
        }
    };
    int main()
    {
        Queue<int > q;
        q.init();
        for(int i=0;i<20;i++)
            q.push(i);
        printf("top %d
    ",q.top());
        printf("length %d
    ",q.length());
        q.push(21);
        printf("length %d
    ",q.length());
        q.visit();
        q.pop();
        printf("top %d
    ",q.top());
        q.visit();
        q.destroy();
        q.visit();
        return 0;
    }
    


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    2020.06.09——习题训练五
    2020.06.01——习题训练4
    2020-05-26 — 习题训练三
    2020-05-22 — 习题训练二-F
    Java笔记(22):IO流(04)
    Java笔记(21):IO流(03)
    Java笔记(20):IO流(02)
    Java笔记(19):IO流(01)
    Java笔记(18):集合框架(04)
    Java笔记(17):集合框架(03)
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4908880.html
Copyright © 2011-2022 走看看