队列的基本操作

来源:互联网 发布:淘宝店铺短连接 编辑:程序博客网 时间:2024/04/26 20:56

队列是链表的一种形式,队列具有先进先出(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;}//向链队中插入一个元素Xvoid 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,否则为0int 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 50typedef 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;}//向队列中添加元素Xvoid 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;}
0 0