链式队列实现(c语言)

队列的定义

      队列是一个线性的数据结构,这个数据结构只从一端进行插入,另一端进行删除,禁止直接访问除这两端以外的一切数据。

如图所示,队列就像一个管道一样,从一端输入,一端输出,先输入的就先输出。

请添加图片描述

队列节点的设计与初始化

        链式队列是一种同时带有队头指针和队尾指针的单链表,头指针指向队头结点,尾指针指向队尾结点。

typedef struct Node{
    ElemType data;
    struct Node *next;
}Node,*Q_node;
typedef struct queues{
    Q_node front;
    Q_node rear;
}queues,*Queues;

下面是关于队列和节点的初始化:

Q_node Init_Node(){
    Node* node=(Node*) malloc(sizeof(Node));
    if(node==NULL){
        printf("分配失败");
        exit(ERROR);
    }
    node->next=NULL;
    return node;
}
Queues Init_Queues(){
    Queues queues=(Queues)malloc(sizeof(queues));
    if(queues==NULL){
        printf("内存分配失败");
        exit(ERROR);
    }
    queues->front=NULL;
    queues->rear=NULL;
//    printf("初始化成功\n");
    return queues;
}

入队操作

当队列为空队列时,队头和队尾都是应该从空到指向新插入的节点,之后在每次插入都会对队尾节点进行连接,并在将指针移向刚插入的节点。如图所示:

第一次入队时,只有一个节点,也就成为了唯一的节点,所以队头和队尾都指向同一节点。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DaxopvqL-1669528720769)(C:\Users\LENOVO\AppData\Roaming\Typora\typora-user-images\image-20221126163933291.png)]

当队列不为空时,就是队尾在不断的向后移动,在通过节点的next指针指向下一个从而连接。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Gh2BnxlW-1669528655030)(C:\Users\LENOVO\AppData\Roaming\Typora\typora-user-images\image-20221126164320543.png)]

status push(Queues queues,ElemType e){
    if(queues==NULL){
        return ERROR;
    }
    Q_node node=Init_Node();
    node->data=e;
    if(queues->rear==NULL && queues->front==NULL){
        queues->front=queues->rear=node;
    } else {
    queues->rear->next=node;
    queues->rear=node;
    }
    return OK;
}

连续入队操作

连续入队的也同入队一样,都是通过不断next指针连接,在队尾指针向后移进行插入。

status Queues_I(Queues queues){
    int x;
    if(queues==NULL){
        return ERROR;
    }
    while (scanf("%d",&x)!=EOF){
        Q_node  node=Init_Node();
        node->data=x;
        if(queues->rear==NULL && queues->front==NULL){
          queues->front=queues->rear=node;
        } else{
            queues->rear->next=node;
            queues->rear=node;
        }
    }
    return OK;
}

出队操作

出队则是遵循着队列的先入先出,所以说在出的时候要从队头开始出,如果队内只有一个元素就直接置为空,否则就指向下一个节点,释放原有节点,即为出队。

status pop(Queues q){
    if(q==NULL){
        return ERROR;
    }
    Q_node qNode=q->front;
    if(q->front==q->rear){
        q->front=NULL;
        q->rear=NULL;
    }else{
        q->front=q->front->next;
        free(qNode); 
    }
    return OK;
}

打印队列

void Print_Queues(Queues q){
    if(q==NULL){
        printf("打印出错");
        exit(ERROR);
    }
    Q_node node= q->front;
    while (node!=NULL){
        printf("对列中的元素:%d\n",node->data);
        node=node->next;
    }
}

完整代码

#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int status;
typedef struct Node{
    ElemType data;
    struct Node *next;
}Node,*Q_node;
typedef struct queues{
    Q_node front;
    Q_node rear;
}queues,*Queues;
Q_node Init_Node(){
    Node* node=(Node*) malloc(sizeof(Node));
    if(node==NULL){
        printf("分配失败");
        exit(ERROR);
    }
    node->next=NULL;
    return node;
}
Queues Init_Queues(){
    Queues queues=(Queues)malloc(sizeof(queues));
    if(queues==NULL){
        printf("内存分配失败");
        exit(ERROR);
    }
    queues->front=NULL;
    queues->rear=NULL;
//    printf("初始化成功\n");
    return queues;
}
status push(Queues queues,ElemType e){
    if(queues==NULL){
        return ERROR;
    }
    Q_node node=Init_Node();
    node->data=e;
    if(queues->rear==NULL && queues->front==NULL){
        queues->front=queues->rear=node;
    } else {
    queues->rear->next=node;
    queues->rear=node;
    }
    return OK;
}
/**多条插入*/
status Queues_I(Queues queues){
    int x;
    if(queues==NULL){
        return ERROR;
    }
    while (scanf("%d",&x)!=EOF){
        Q_node  node=Init_Node();
        node->data=x;
        if(queues->rear==NULL && queues->front==NULL){
          queues->front=queues->rear=node;
        } else{
            queues->rear->next=node;
            queues->rear=node;
        }
    }
    return OK;
}
status pop(Queues q,ElemType *e){
    if(q==NULL){
        return ERROR;
    }
    Q_node qNode=q->front;
    *e=q->front->data;
    q->front=q->front->next;

    free(qNode);
    return OK;
}

void Print_Queues(Queues q){
    if(q==NULL){
        printf("打印出错");
        exit(ERROR);
    }
    Q_node node= q->front;
    while (node!=NULL){
        printf("对列中的元素:%d\n",node->data);
        node=node->next;
    }
}

int  main(){
    int x;
Queues  queues=Init_Queues();
    Queues_I(queues);
    Print_Queues(queues);
    push(queues,100);
    printf("入队之后\n");
    Print_Queues(queues);
    pop(queues,&x);
    printf("出队之后\n");
    Print_Queues(queues);
    printf("%d",x);
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值