C語言實現常用數據結構——隊列


#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 10
/* 用一個動態數組來實現隊列 */

typedef struct Queue {
    int Capacity;
    int Front;
    int Rear;
    int Size;
    int data[MAX_SIZE];
} Queue;

void Error(char *error) {
    printf("%s",error);
}

void FatalError(char *fatalerror) {
    printf("%s",fatalerror);
}

int IsEmpty(Queue *Q) {
    return Q->Size == 0;
}

int IsFull(Queue *Q) {
    return Q->Size == Q->Capacity;
}

void Init( Queue *Q ) {
    Q->Size = 0;
    Q->Front = 1;
    Q->Rear = 0;
    Q->Capacity = MAX_SIZE;
}


static int Succ(int value,Queue *Q) {
    if(++value == Q->Capacity) {
        value = 0;
    }
    return value;
}


void Enqueue(int X,Queue *Q) {
    if( IsFull( Q ) )
        FatalError("Full queue");
    else {
        Q->Size++;
        Q->Rear = Succ(Q->Rear,Q);
        Q->data[ Q->Rear ] = X;
    }
}

 
void Dequeue(Queue *Q) {
    if(IsEmpty(Q))
        FatalError("Empty queue");
    else {
        Q->Size--;
        Q->Front = Succ(Q->Front,Q);
    }
}


int FrontAndDequeue(Queue *Q) {
    int Tmp;
    if(IsEmpty(Q))
        Error("Empty queue");
    else {
        Q->Size--;
        Tmp = Q->data[Q->Front];
        Q->Front = Succ(Q->Front,Q);
        return Tmp;
    }
}


void DisposeQueue( Queue *Q ) {
    free(Q->data);
    free(Q);
}



main() {
    Queue *q ;
    Init(q);
    int i;
    for(i = 0; i <MAX_SIZE; i++) {
        Enqueue(i,q);
    }
    for(i = 0; i <MAX_SIZE; i++) {
        printf("%d\n",FrontAndDequeue(q));
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM