【数据结构】链式队列功能实现:输入数字入队,输入字符出队。

问题描述:链式队列功能实现:输入数字入队,输入字符出队。

代码实现:

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

linkqueue.h

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 <stdio.h>
#include "linkqueue.h"

int main(int argc, const char *argv[])
{
    linkqueue *lq;
    int value;

    lq = queue_create();
    if(lq == NULL){
        return -1;
    }

    printf("nums join,char out queue:\n");
    printf("input:");
    while(scanf("%d",&value)){
        enqueue(lq,value);
        printf("input:");
    }
    while(!queue_empty(lq)){
        printf("%d\n",dequeue(lq));
    }
    queue_free(lq);
    return 0;
}

运行结果:

在这里插入图片描述

由上述结果可知,输入10 20 30 正常入队,输入r则停止入队,并打印出队。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值