day9 队列实现及其应用(下)

链式队列的原理

定义:插入操作在队尾进行删除操作在队头进行,(两端操作的链表)由队头指针和队尾指针控制队列的操作。

链式队列结点结构体声明

typedef int datatype;

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

链式队列结构

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

链式队列创建

函数原型:

linkqueue * queue_create();

算法思路:

如图所示链式队列需要创建两次空间。

代码实现:

linkqueue * queue_create() {
    linkqueue *lq;
    if ((lq = (linkqueue *)malloc(sizeof(linkqueue))) == NULL){
        printf("malloc linkqueue failed\n");
        return NULL;
    }
    lq->front = lq->reae = (linklist)malloc(sizeof(listnode));
    if (lq->front == NULL) {
        printf("malloc node failed\n");
        return NULL;
    }
    lq->front->data = 0;
    lq->rear->next = NULL;
    return lq;
}

入队操作

函数原型:

int enqueue(linkqueue *lq, datatype x);

算法思路:

(1)封装一个结点,指针变量p指向要插入的结点;

(2)将结点与队列链接;

(3)更新队尾。

代码实现:

int enqueue(linkqueue *lq, datatype x) {
    if (lq == NULL) {
        printf("lq is NULL\n");
        retirn -1;
    }
    linklist p;
    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);

算法思路:出队有两种方法,方法一,直接删除要出队的结点然后更新front的指向,但存在特殊情况当链式队列中只存在一个结点时,还需要考虑rear指针重新回到头结点的操作;方法二,当遇到队列只有一个结点时,删除头结点更新front的指向释放内存。

(1)用指针变量p指向待删除的结点;

(2)更新front指针的指向;

(3)释放内存

代码实现:

 datatype dequeue(linkqueue *lq) {
    linklist p;
    p = lq->front;
    lq->front = p->next;
    free(p);
    p = NULL;
    return (lq->front->data);
}

队列是否为空

函数原型:

int queue_empty(linkqueue *lq);

代码实现:

int queue_empty(linkqueue *lq) {
    if (lq == NULL) {
        printf("lq is NULL\n");
        retirn -1;
    }
    return (lq->front == lq->rear ? 1 : 0);
}
        

队列清空

函数原型:

int queue_clear(linkqueue *lq);

代码实现:

int queue_clear(linkqueue *lq){
    linklist p;
    if (lq == NULL) {
        printf("lq is NULL\n");
        retirn -1;
    }
    while(lq-front->next) {
        p = lq->front;
        lq->front = p->next;
        print("free:%d\n", p->data);
        free(p);
        p = NULL;
    }
    return 0;
}

队列内存释放

函数原型:

linklist * queue_free(linkqueue *lq);

算法思路:为防止内层空间丢失,先释放内层空间;p指向待删除的结点,front指向下一个结点。

代码实现;

linklist * queue_free(linkqueue *lq){
    linklist p;
    if (lq == NULL) {
        printf("lq is NULL\n");
        retirn NULL;
    }
   
    while(lq->front) {
         p = lq->front;
        lq->front = p->next;
        printf("clrea_free:%d\n", p->data);
        free(p);
    }
    free(lq);
    lq = NULL;

    return NULL;
}

完整代码

linkqueue.h

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);

linkqueue.c

#include <stdio.h>
#include <stdlib.h>
#include "linkqueue.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(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;
}

main.c

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

栈和队列的应用

球钟问题的原理

球钟是一个利用球的移动来记录时间的简单装置,它有三个可以容纳若干个球的指示器:分钟指示器,五分钟指示器,小时指示器;

若分钟指示器有2个球,五分钟指示器有6个球,小时指示器有5个球,则时间为:5:32。

工作原理

用到了一个链式队列(球队列)三个顺序栈(三个指示器)

  • 每过一分钟,球钟就会从球队列的队首取出一个球放入分钟指示器,分钟指示器最多可容纳4个球

  • 当放入第五个球时,在分组指示器的4个球就会按照他们被放入时相反的顺序加入球队列的队尾(栈)。而第五个球就会进入五分钟指示器。

  • 按此类推,五分钟指示器最多可以放11个球小时指示器最多11个球。

  • 当小时指示器放入第12个球时,原来的11个球按照他们被放入时的相反顺序加入球队列的队尾,然后第12个球也回到队尾。这时,三个指示器均为空,回到初始状态,从而形成一个循环。因此,该球钟表示时间的范围是从0:00到11:59。

问题:

现设初始时球队列的球数为27,球钟的三个指示器初态均为空

问:要经过多久,球队列才能恢复到原来的顺序

球钟问题代码实现:

用到了day7的顺序栈中的sqstac.h sqstack.c以及本章的linkqueue.h linkqueue.c

main.c函数如下

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

int check(linkqueue * lq);

int main(int argc, const char *argv[])
{
    linkqueue *lq;
    sqstack *s_hour, *s_five, *s_min;   //三个指示器
    int value;                         //value 就是球
    int i, min = 0;                     //min变量用于计时                 

    if ((lq = queue_create()) == NULL) {  //创建顺序队列(球队列)
        return -1;
    }

    for (i = 1; i <= 27; i++) {
        enqueue(lq, i);
    }

    if ((s_hour = stack_create(11)) == NULL) {    //小时栈创建,容量11
        return -1;
    }

    if ((s_five = stack_create(11)) == NULL) {     //五分钟创建 ,栈容量11
        return -1;
    }

    if ((s_min = stack_create(4)) == NULL) {       //分钟栈创建 ,容量为4
        return -1;
    }
//恢复时间未知,使用while(1)循环到结果时退出
    while (1) {                    
        min++;
//过去1分钟value出队
        if (!queue_empty(lq)) {
            value = dequeue(lq);  
 //value入分钟指示器中 即入栈     
            if (!stack_full(s_min)) {
                stack_push(s_min, value);  
            } else {
//当分钟指示器已满时执行出栈 入队操作
                while (!stack_empty(s_min)) {
                    enqueue(lq, stack_pop(s_min)); 
                }
//value入队后当五分钟指示器未满是,入栈五分钟指示器
                if (!stack_full(s_five)) {
                    stack_push(s_five, value);
                } else {
//当五分钟栈满时value出栈入队
                    while (!stack_empty(s_five)) {
                        enqueue(lq, stack_pop(s_five));
                    }
//当小时栈未满是value出队入小时栈
                    if (!stack_full(s_hour)) {
                        stack_push(s_hour, value);
                    } else {
//当小时栈满时value出栈入队
                        while (!stack_empty(s_hour)) {
                            enqueue(lq, stack_pop(s_hour));
                        }
 //此时为  0:0
                        enqueue(lq, value);
//检查队列是否是升序,返回1则为升序
                        if (check(lq) == 1) {
                            break;
                        }
                    }
                }

            }
        }
    }
    printf("total:%d\n", min);

    printf("dequeue:");
    while (!queue_empty(lq)) 
        printf("%d ", dequeue(lq));
    
    puts("");

    return 0;
}

int check(linkqueue * lq) { 
    linklist p;

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

    p = lq->front->next;

    while (p && p->next) {
        if (p->data < p->next->data) {
            p = p->next;
        } else {
            return 0;
        }
    }
    return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值