数据结构与算法设计——循环队列

数据结构与算法设计——循环队列

#include <iostream>
#define SIZE 6//固定pbase长度
using namespace std;
typedef struct Queue{
    
    
    int *pbase;
    int front;
    int rear;
}Queue;
bool init_Queue(Queue*);//初始化队列
bool into_Queue(Queue*,int);//入队
bool empty_Queue(Queue*);//是否为空
bool full_Queue(Queue*);//是否已满
bool out_QUeue(Queue*);//出队
void traverse(Queue*);//遍历

bool init_Queue(Queue* Queue)
{
    
    
    Queue->pbase = (int *)malloc(sizeof(int)*SIZE);
    Queue->front = 0;
    Queue->rear = 0;
    return true;
}
bool into_Queue(Queue* Queue,int value)
{
    
    
    if(full_Queue(Queue))
    {
    
    
        return false;
    }
    else
    {
    
    
        Queue->pbase[Queue->rear]=value;
        Queue->rear = (Queue->rear+1)%SIZE;
    }
    return true;
}
bool empty_Queue(Queue* Queue)
{
    
    
    if(Queue->front==Queue->rear)
    {
    
    
        return true;
    }
    return false;
}
bool full_Queue(Queue* Queue)
{
    
    
    if((Queue->rear+1)%SIZE==Queue->front)
    {
    
    
        return true;
    }
    return false;
}
void traverse(Queue* Queue)
{
    
    
    for(int i=Queue->front;i!=Queue->rear;i=(i+1)%SIZE)
    {
    
    
        cout<<Queue->pbase[i]<<endl;;
    }
}
bool out_Queue(Queue* Queue)
{
    
    
    if(empty_Queue(Queue))
    {
    
    
        return false;
    }
    else
    {
    
    
        Queue->front++;
    }
    return true;
}
int main(int argc, char const *argv[])
{
    
    
    Queue Queue;
    init_Queue(&Queue);
    into_Queue(&Queue,10);
    into_Queue(&Queue,20);
    into_Queue(&Queue,30);
    into_Queue(&Queue,40);
    into_Queue(&Queue,50);
    traverse(&Queue);
    out_Queue(&Queue);
    out_Queue(&Queue);
    traverse(&Queue);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46061085/article/details/120554445
今日推荐