隊列的基本操作

隊列是鏈表的一種形式,隊列具有先進先出(FIFO)的特性,即使用隊列時,插入在一端進行,而刪除在另一端進行。

隊列的基本操作是Enqueue(入隊),他是在表的末尾(rear)插入一個元素,還有Dequequ(出隊),即刪除(或返回)在表的開頭(隊頭,front)的元素。

隊列可以使用鏈表或者數組來實現。基本的操作示例如下:

鏈表實現:

#include <stdlib.h>
#include <stdio.h>

typedef int elemType;

struct Node
{
    elemType data; //值域
    Node * next;//鏈接指針
};

struct queueLK
{
    Node *front;  //隊首指針
    Node *rear; //隊尾指針
};

//初始化鏈隊
void initQueueLK(queueLK *hq)
{
    hq->front = hq->rear = NULL; //把隊首、隊尾指針置空
    return;

}

//向鏈隊中插入一個元素X
void enQueue(queueLK *hq, elemType x)
{
    //得到一個由newP指針所指向的新節點
    Node *newP;
    newP = (Node*)malloc(sizeof(Node));
    if (newP == NULL)
    {
        printf("內存空間分配失敗\n");
        exit(1);
    }
    //把X的值賦給新節點,吧新節點的指針域置空
    newP->data = x;
    newP->next = NULL;

    //若鏈隊爲空,則新節點即是隊首又是隊尾
    if(hq->rear == NULL)
        hq->front = hq->rear = newP;
    else
        hq->rear = hq->rear->next = newP; //注意賦值順序
    return;
}

//從隊列中刪除一個元素
elemType outQueue(queueLK *hq)
{
    Node *p;
    elemType temp;

    //若鏈表爲空,停止運行
    if (hq->front == NULL)
    {
        printf("隊列爲空,無法刪除\n");
        exit(1);
    }

    temp = hq->front->data; //暫存隊尾元素以便返回
    p = hq->front;  //暫存隊首指針以便回收隊尾節點
    hq->front = p->next; //隊首指針指向下一個節點

    //若刪除後鏈隊爲空,則需同時將隊尾指針置空
    if(hq->front == NULL)
        hq->rear = NULL;
    free(p); //回收原隊首節點
    return temp; //返回唄刪除的隊首元素值
}

//讀取隊首元素值
elemType peekQueue(queueLK *hq)
{
    //若鏈隊爲空,停止運行
    if (hq->front == NULL)
    {
        printf("鏈隊爲空,退出程序\n");
        exit(1);
    }
    return hq->front->data; //返回隊首元素
}

//檢測鏈隊是否爲空,空返回1,否則爲0
int emptyQueue(queueLK *hq)
{
    //判斷隊首或隊尾任一個指針是否爲空即可
    if (hq->front == NULL)
        return 1;
    else
        return 0;
}


//清除鏈隊所有元素
void clearQueue(queueLK *hq)
{
    if (hq->front == NULL)
    {
        printf("鏈表爲空,退出運行\n");
        exit(1);
    }
    Node *p = hq->front; //隊首指針賦給p
    //依次刪除隊列中的每一個節點,最後使隊首指針爲空
    while (p != NULL)
    {
        hq->front = hq->front->next;
        free(p);
        p = hq->front;
    }
    hq->rear = NULL; //置隊尾指針爲空
    printf("清除鏈表元素成功\n");
    return;
}

int main()
{
    queueLK q;
    int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
    int i;
    initQueueLK(&q);
    for ( i = 0; i < 8; i++)
    {
        enQueue(&q, a[i]);
    }
    printf("%d\n", outQueue(&q));
    enQueue(&q,68);
    printf("%d\n", outQueue(&q));
    while(!emptyQueue(&q))
    {
        printf("%d\t", outQueue(&q));
    }
    printf("\n");
    clearQueue(&q);

    system("pause");
    return 0;
}

數組實現:

#include <stdio.h>
#include <stdlib.h>

#define QUEUE_SIZE 50

typedef struct SeqQueue
{
    int data[QUEUE_SIZE];
    int front;
    int rear;

}Queue;

//初始化隊列
void initQueue( Queue *q)
{
    if (q == NULL)
    {
        printf("隊列爲空,退出程序\n");
        exit(1);
    }
    q->front = NULL;
    q->rear = NULL;
}

//判斷隊列是否爲滿
int isFull(Queue * q)
{
    return ((q->rear + 1) % QUEUE_SIZE == q->front);
}

//判斷隊列是否爲空
int isEmpty(Queue *q)
{
    return (q->front == q->rear);
}

//刪除隊列元素
int deQueue(Queue *q)
{
    if(isEmpty(q)) 
    {
        printf("隊列爲空,無法刪除,退出程序\n");
        exit(1);
    }
    int temp = q->data[q->front];
    q->front = (q->front +1) %QUEUE_SIZE;
    return temp;
}

//向隊列中添加元素X
void enQueue(Queue *q, int x)
{
    if (isFull(q))
    {
        printf("隊列已經滿,無法添加,退出程序\n");
        exit(1);
    }
    q->data[q->rear] = x;
    q->rear = (q->rear + 1) %QUEUE_SIZE;

}

void clearQueue(Queue *q)
{
    if (isEmpty(q))
    {
        printf("隊列已經爲空,退出程序\n");
        exit(1);
    }
    q->front = q->rear = 0;
    printf("清空隊列完畢\n");
}

int main()
{
    Queue *q = (Queue *) malloc(sizeof (Queue));
    initQueue(q);
    int i;
    for (i = 0; i < 10; i++)
    {
        enQueue(q, i);
        printf("%d\t",i);
    }
    printf("\n");
    /*while (!isEmpty(q))
    {
        int data = deQueue(q);
        printf("%d\t", data);
    }*/

    clearQueue(q);
    deQueue(q);//驗證是否清空隊列

    system("pause");
    return 0;
}
發佈了46 篇原創文章 · 獲贊 25 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章