数据结构——链式队列


前言

顺序对列上一篇;链接如下
顺序对列

一、链式队列有假溢出么?链式队列需要判空判满么?为什么??

链式队列没有假溢出、因为在链式队列中,入队操作是在队尾进行的,出队操作是在队头进行的
链式队列需要判空,因为链式队列可能没有元素当链式队列为空时,我们无法执行出队操作
链式队列不需要判满,因为链式队列可以动态地申请空间当需要入队时,我们可以根据需要动态地分配新的节点来存储元素

二、写出链式队列;

1.创建链式队列

代码如下(示例):

typedef int datatype;
 
typedef struct node {
    datatype data;
    struct node *next;
}listnode , *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);

2.实现链式队列各个功能代码块

代码如下(示例):

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(listnode));
    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(listnode))) == 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("clear free:%d\n", p->data);
        free(p);
        p = NULL;
    }
    return 0;
}
 
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;
}

3.测试链式队列

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;
}
  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值