棧和隊列(二)

隊列

代碼實現(xiàn)

#include<malloc.h> 
#include<stdio.h> 
#define OK 1
#define ERROR 0
typedef int Status;     // Status是函數(shù)的類型,其值是函數(shù)結(jié)果狀態(tài)代碼,如OK等
typedef int QElemType;
#define MAXQSIZE 100    // 最大隊列長度(對于循環(huán)隊列,最大隊列長度要減1)

typedef struct
{
    QElemType *base;    // 初始化的動態(tài)分配存儲空間
    int front;          // 頭指針,若隊列不空,指向隊列頭元素
    int rear;           // 尾指針,若隊列不空,指向隊列尾元素的下一個位置
}SqQueue;


// 構(gòu)造一個空隊列Q,該隊列預(yù)定義大小為MAXQSIZE
Status InitQueue(SqQueue &Q)   
{
    Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
    if(!Q.base) return ERROR;
    Q.rear = Q.front = 0;
    return OK;
}


// 插入元素e為Q的新的隊尾元素
Status EnQueue(SqQueue &Q,QElemType e)  
{ 
    if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXQSIZE;
    return OK;
}


// 若隊列不空, 則刪除Q的隊頭元素, 用e返回其值, 并返回OK; 否則返回ERROR
Status DeQueue(SqQueue &Q, QElemType &e) 
{  
    if(Q.front==Q.rear) return ERROR;
    e=Q.base[Q.front];
    Q.front=(Q.front+1)%MAXQSIZE;
    return OK;
}


// 若隊列不空,則用e返回隊頭元素,并返回OK,否則返回ERROR
Status GetHead(SqQueue Q, QElemType &e)
{   
    if(Q.front == Q.rear) return ERROR;
    e = Q.base[Q.front];
    return OK;
}


// 返回Q的元素個數(shù)
int QueueLength(SqQueue Q)  
{
    //return Q.rear - Q.front;
    //return Q.rear%MAXQSIZE-Q.front%MAXQSIZE
    return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}


// 若隊列不空,則從隊頭到隊尾依次輸出各個隊列元素,并返回OK;否則返回ERROR.
Status QueueTraverse(SqQueue Q)  
{
    int i;
    i=Q.front;
    if(Q.rear == Q.front)printf("The Queue is Empty!");  
    else
    {
        printf("The Queue is: ");
        while(i != Q.rear)     //請?zhí)羁?        {
            printf("%d ",Q.base[i] );  
            i = (i+1) % MAXQSIZE;  
        }
    }
    printf("\n");
    return OK;
}

int main()
{
    int a;
    SqQueue S;
    QElemType x, e;
    if(InitQueue(S))    // 判斷順序表是否創(chuàng)建成功
    {
        printf("A Queue Has Created.\n");
    }
    while(1)
    {
        printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1: 
                scanf("%d", &x);
                if(!EnQueue(S, x)) printf("Enter Error!\n"); // 判斷入隊是否合法
                else printf("The Element %d is Successfully Entered!\n", x); 
                break;
            case 2: 
                if(!DeQueue(S, e)) printf("Delete Error!\n"); // 判斷出隊是否合法
                else printf("The Element %d is Successfully Deleted!\n", e);
                break;
            case 3: 
                if(!GetHead(S, e))printf("Get Head Error!\n"); // 判斷Get Head是否合法
                else printf("The Head of the Queue is %d!\n", e);
                break;
            case 4: 
                printf("The Length of the Queue is %d!\n", QueueLength(S));
                break;
            case 5: 
                QueueTraverse(S);
                break;
            case 0: 
                return 1;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容