数据结构之队列12阵列队列

来源:互联网 发布:hadoop大数据平台架构 编辑:程序博客网 时间:2024/06/06 14:20

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR -1;
#define INFEASBIOLE -2;
#define OVERFLOW 0;
typedef int Status;
//阵列队列的存储结构
struct Queue
{
    int Array[10];
    int head;
    int tail;
    int length;
};
//阵列队列的基本操作
Status InitQueue(struct Queue *Q)
{
    //建立空的阵列队列
    Q->head=Q->tail=0;
    Q->length=0;
    return OK;
}
Status ClearQueue(struct Queue *Q)
{
    //清空阵列队列
    Q->head=Q->tail=0;
    Q->length=0;
    return OK;
}
Status EnQueue(struct Queue *Q,int e)
{
    //添加元素
    Q->Array[Q->tail]=e;
    if(Q->tail+1==10)
        Q->tail=0;
    Q->tail+=1;
    Q->length+=1;
    return OK;
}
Status DeQueue(struct Queue *Q)
{
    //删除元素
    int e=Q->Array[Q->head];
    if(Q->head+1==10)
        Q->head=0;
    Q->head+=1;
    Q->length-=1;
    return e;
}
int main()
{
    struct Queue *Queue1=malloc(sizeof(struct Queue));//建立資料結構
    Queue1->length=0;//新增長度//10改為0,初始狀態
    Queue1->head=0;//必須要先初始化
    Queue1->tail=0;//必須要先初始化
    EnQueue(Queue1,5);//將5放入佇列
    EnQueue(Queue1,8);//將8放入佇列
    EnQueue(Queue1,3);//將3放入佇列
    EnQueue(Queue1,2);//將2放入佇列
    printf("%d ",DeQueue(Queue1));//輸出佇列(5)
    printf("%d ",DeQueue(Queue1));//輸出佇列(8)
    printf("%d ",DeQueue(Queue1));//輸出佇列(3)
    system("pause");
    return 0;
}
0 0
原创粉丝点击