1. 程式人生 > >佇列的陣列實現

佇列的陣列實現


typedef struct QNode *Queue;

struct QNode

{

    int *data;

    int front;//頭指標

    int rear;//尾指標;

    int size;//當前陣列大小

};

int Scu(int value,int Maxsize) 

{

    if (++value == Maxsize)

         value = 0;

    return value;

}

Queue
creatQueue(int Maxsize)

{

    Queue Q = (Queue)malloc
(sizeof(struct QNode)); Q->data= (int*)malloc(Maxsize * sizeof(int)); Q->size= 0; Q->front= 0; Q->rear= 0; return Q; } bool IsfullQ(Queue Q,int Maxsize) //判斷佇列是否滿 { if (Q->size == Maxsize) { return true; } return false; } bool IsEmptyQ
(Queue Q) //判斷佇列是否空 { if (Q->size == 0) { return true; } return false; } void AddQ(Queue Q,int item,int Maxsize) //向佇列加入一個元素 { if (IsfullQ(Q,Maxsize)) { printf("佇列已滿\n"); return; } Q->rear = Scu(Q->rear,Maxsize); Q->
data[Q->rear] = item; Q->size++; } int DelQ(Queue Q,int Maxsize) { if (IsEmptyQ(Q)) { printf("佇列為空\n"); return -1; } Q->size--; Q->front = Scu(Q->front,Maxsize); return Q->data[Q->front]; }