【笔记】环形队列链式

来源:互联网 发布:excel中数据递增 编辑:程序博客网 时间:2024/05/05 03:49

废话不说直接帖代码和注释。另外观察到main函数中除了print_queue(直接用L这个结构体传参)以外 其他的函数全部用了&L去传参。记得自己在论坛里面也问过类似问题。可以翻去看看。

#include <stdio.h>#include <stdlib.h>#include <stdbool.h>typedef int Queue_Type;// 定义队列节点结构typedef struct CQUEUE_NODE{    Queue_Type value;    struct CQUEUE_NODE *next;}QNode, *PNode;// 定义队列结构typedef struct CQUEUE{    unsigned int size;    PNode front, retail;}LQueue;//创建环形队列void Create_Queue(LQueue *L, int n){    PNode front = (PNode)malloc(sizeof(QNode));//这个(PNode)其实是个PNode类型指针前文有定义。这里front这个指针就拿到了分配到QNode大小的那个地址了    PNode retail = (PNode)malloc(sizeof(QNode));//参考上面的注释    if(front == NULL || retail == NULL)    exit(1);    scanf("%d", &front->value);  //第一个节点那里存储的value    front->next = NULL;    // 只有一个节点的时候,头尾指向同一个点    retail = front;    // 多个点情况    int i;    for(i = 1; i < n; i++)    {        PNode p = (PNode)malloc(sizeof(QNode));        if(p == NULL)        exit(1);        scanf("%d", &p->value);    // 因为Retail的地址如今还等于front 所以retail->next其实等价front->next        retail->next = p;    //Retail不再和front一个地址 它开始等于p的地址        retail = p;    }    // 结束后,尾节点指向头节点    retail->next = front;    L->front = (PNode)malloc(sizeof(QNode));    L->retail = (PNode)malloc(sizeof(QNode));    if(L->front == NULL || L->retail == NULL)    exit(1);    L->front = front;    L->retail = retail;    L->size = n;}// 销毁队列void Destory_Queue(LQueue *L){    // 如果队列为空    if(L->front == NULL)    exit(1);    // 这里注意必须重新申请一个节点    // 直接使用L->retail->next会有类型不能识别的错误    PNode p = (PNode)malloc(sizeof(QNode));    if(p == NULL)    exit(1);    p = L->front;    int size = L->size;    // 队列是多个节点情况    while(size != 0)    {        L->front = p->next;        free(p);        p = L->front;        size--;    }    // 这里一定要记得处理    // 将front retail指向NULL 在上面的处理过程中,只是free并没有指向NULL    // 如果不指向NULL, front,retail将是野指针    // 还要记得将L->size重新赋值,上面size只是读取size,并不改变size的值    L->front = NULL;    L->retail = NULL;    L->size = size;}// 入列操作 在尾节点插入void Enqueue(LQueue *L, Queue_Type value){    PNode p = (PNode)malloc(sizeof(QNode));    if(p == NULL)    exit(1);    p->value = value;    int size = L->size;    //如果队列是个空队列    if(size == 0)    {        L->front = (PNode)malloc(sizeof(QNode));        L->retail = (PNode)malloc(sizeof(QNode));        L->front = p;        L->retail = p;        p->next = NULL;        size ++;    }    // 如果队列不是空队列    // 头节点倒是没有必要重新开辟空间    PNode p1 = (PNode)malloc(sizeof(QNode));    if(p1 == NULL)    exit(1);    //p1就是个用来转换的媒介 它承接了L->retail所指向的内容 从上文可知 L->retail本来已经等于front了    p1 = L->retail;    p1->next = p;    //p的next指针又去和L->front指到同一内容 也就是队列头部    p->next = L->front;    //而队列尾部L->retail这时背负值为和p指向同一内容 因此再增加了一个node后 头尾被拆线然后又缝合了    L->retail = p;     size++;    L->size = size;}// 出列操作Queue_Type Dequeue(LQueue *L){    int size = L->size;    Queue_Type n;    //如果队列为空    if(size == 0)    {        printf("The queue is empty.\n");        exit(1);    }    PNode p = (PNode)malloc(sizeof(QNode));    if(p == NULL)    exit(1);    p = L->front;    //如果队列只有一个节点    if(size == 1)    {        n = p->value;        free(p);        L->front = L->retail = NULL;        p = NULL;        size--;        L->size = size;        return n;    }    if(size == 2)    {        n = p->value;        L->front = L->retail;        free(p);        p = NULL;        size--;        L->size = size;        return n;    }    // 头节点倒是没有必要重新开辟空间    PNode p1 = (PNode)malloc(sizeof(QNode));    if(p1 == NULL)    exit(1);    p1 = L->retail;    n = p->value;    L->front = p->next;    p1->next = L->front;    free(p);    p = NULL;    size--;    L->size = size;    return n;}// 判断队列是否为空bool Is_Empty(LQueue L){    if(L.size == 0)    return false;    else    return true;}// 打印队列void Print_Queue(LQueue L){    // 如果队列为空    if(L.size == 0)    {        printf("The size of the queue is: %d\n", L.size);        exit(1);    }    printf("The size of the queue is: %d\n", L.size);    printf("The following are the elements of the queue:\n");    PNode p = (PNode)malloc(sizeof(QNode));    if(p == NULL)    exit(1);    //开个p 然后让他指向和L.front指向的地方相同    p = L.front;    while(p != L.retail)    {        printf("%d\n", p->value);//开始接力p的next        p = p->next;    }    if(p == L.retail)    {        printf("%d\n", p->value);    }}int main(){    LQueue L;    int n;    printf("Input the number of the nodes of the queue:\n");    scanf("%d", &n);    printf("-------------------------------------------\n");    Create_Queue(&L, n);    printf("-------------------------------------------\n");    Print_Queue(L);    printf("-------------------------------------------\n");    printf("Enqueue a node:\n");    int n1, n2,n3;    scanf("%d", &n1);    Enqueue(&L, n1);    printf("-------------------------------------------\n");    Print_Queue(L);    printf("Dequeue one node  -------------------------\n");    n2 = Dequeue(&L);    printf("Node1 is %d\n",n2);    Print_Queue(L);    printf("Dequeue another node  -------------------------\n");    n3 =Dequeue(&L);    printf("Node2 is %d\n",n3);    Print_Queue(L);    printf("-------------------------------------------\n");    printf("Destory the queue---------------------------\n");    Destory_Queue(&L);    Print_Queue(L);    return 0;}


0 0
原创粉丝点击