【数据结构】链式队列的实现(函数功能实现:创建链式队列、进队、出队、清除队列、释放内存等)

问题描述:链式队列的实现。(功能实现:创建链式队列、进队、出队、清除队列、释放内存等)。

代码实现:

实现一个功能函数需要三个文件(三个文件在同一目录)。
sequeue.h(定义顺序表)
sequeue.c(实现接口函数)
test.c(主函数实现)

linkqueue.h

定义各种函数。包括
*创建链式队列linkqueue queue_create();
*入队int enqueue(linkqueue lq, datatype x);
*出队datatype dequeue(linkqueue lq);
*清除队列int queue_clear(linkqueue lq);
**释放内存空间linkqueue queue_free(linkqueue lq);

typedef int datatype;

typedef struct node {
    datatype data;
    struct node *next;
} linknode, *linklist;

typedef struct {
    linklist front;
    linklist rear;
} linkqueue;

linkqueue *queue_create();

int enqueue(linkqueue *lq, datatype x);

datatype dequeue(linkqueue *lq);

int queue_empty(linkqueue *lq);

int queue_clear(linkqueue *lq);

linkqueue *queue_free(linkqueue *lq);

linkqueue.c

上述定义函数的实现。

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

linkqueue *queue_create() {
    linkqueue *lq;
    if ((lq = (linkqueue *) malloc(sizeof(linkqueue))) == NULL) {
        printf("malloc linkqueue failed\n");
        return NULL;
    }
    lq->front = lq->rear = (linklist) malloc(sizeof(linknode));

    if (lq->front == NULL) {
        printf("malloc node failed\n");
        return NULL;
    }
    lq->front->data = 0;
    lq->front->next = NULL;

    return lq;
}

int enqueue(linkqueue *lq, datatype x) {
    linklist p;

    if (lq == NULL) {
        printf("lq is NULL\n");
        return -1;

    }


    if ((p = (linklist) malloc(sizeof(linknode))) == NULL) {
        printf("malloc node failed\n");
        return -1;
    }
    p->data = x;
    p->next = NULL;
    lq->rear->next = p;
    lq->rear = p;

    return 0;

}

datatype dequeue(linkqueue *lq) {
    linklist p;

    if (lq == NULL) {
        printf("lq is NULL\n");
        return -1;

    }

    p = lq->front;
    lq->front = p->next;
    free(p);
    p = NULL;

    return (lq->front->data);
}

int queue_empty(linkqueue *lq) {
    if (lq == NULL) {
        printf("lq is NULL\n");
        return -1;
    }

    return (lq->front == lq->rear ? 1 : 0);

}

int queue_clear(linkqueue *lq) {
    linklist p;

    if (lq == NULL) {
        printf("lq is NULL\n");
        return -1;

    }
    while (lq->front->next) {
        p = lq->front;
        lq->front = p->next;
        printf("free:%d\n", p->data);
        free(p);
        p = NULL;
    }

}

linkqueue *queue_free(linkqueue *lq) {
    linklist p;

    if (lq == NULL) {
        printf("lq is NULL\n");
        return NULL;

    }

    while (lq->front) {
        p = lq->front;
        lq->front = p->next;
        printf("free:%d\n", p->data);
        free(p);
    }

    free(lq);
    lq = NULL;

    return NULL;
}

test.c

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

int main(int argc, const char *argv[]) {

    linkqueue *lq;
    lq = queue_create();
    if (lq == NULL)
        return -1;

    enqueue(lq, 10);
    enqueue(lq, 20);
    enqueue(lq, 30);
    enqueue(lq, 40);

    while (!queue_empty(lq)) {
        printf("dequeue:%d\n", dequeue(lq));

    }
    queue_clear(lq);
    lq = queue_free(lq);
    enqueue(lq, 50);
    return 0;
}

运行结果:

在这里插入图片描述
由上述结果看出,成功入队元素10,20,30,40,并且成功按先入先出的顺序出队,随后释放内存,致使元素50没有入队成功。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值