队列--链式存储结构

目录

1.头文件

2.初始化

3.销毁队列

4.判空

5.入队

6.出队


1.头文件

include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 50

2.初始化

typedef char ElemType;

typedef struct qnode{
    ElemType data;
    struct qnode *next;
}DataNode;

typedef struct {
    DataNode *front;
    DataNode *rear;
}LinkQuNode;

 LinkQuNode *q;
    q = (LinkQuNode * ) malloc(sizeof(LinkQuNode));
    q -> front = q -> rear = NULL;

3.销毁队列

void DestroyQueue(LinkQuNode * s) {
    DataNode *pre = s -> front,*p;
    if(pre != NULL){
        p = pre -> next;
        while (p != NULL){
            free(pre);
            pre = p;
            p = pre -> next;
        }
        free(pre);
    }
    free(s);
    printf("销毁成功\n");
}

4.判空

bool EmptySQueue(LinkQuNode * s){
    if(s -> rear == NULL){
        printf("队列为空\n");
        return false;
    } else{
        printf("队列不为空\n");
        return true;
    }
}

5.入队

bool enQueue(LinkQuNode *s,ElemType e){
    DataNode *p;
    p = (DataNode*) malloc(sizeof(DataNode));
    p -> data = e;
    if(s -> rear == NULL){
        s -> front = s -> rear = p;
    } else{
        s -> rear -> next = p;
        s -> rear = p;
    }
    printf("入队成功\n");
    return true;
}

6.出队

bool dlQueue(LinkQuNode *s,ElemType e){

    DataNode *t;
    if(s -> rear == NULL){
        printf("队列为空\n");
        return false;
    }
    t = s -> front;
    if(s -> front == s -> rear)
        s -> front = s -> rear = NULL;
    else
        s -> front = s -> front -> next;
    e = t -> data;
    free(t);
    printf("%c出队成功\n",e);
    return true;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值