钟球问题——栈和队列的应用

问题引入

球钟问题:球钟是利用球的移动来记录时间的装置
有三个可以容纳若干球的容器,分别是:分钟指示器、5分钟指示器、小时指示器,当分钟指示器里面有4个球,5分钟指示器里面有1个球,小时指示器里面有一个球,现在的时间就是一小时9分钟。

1、表示00:00到12:00需要多少个球

4(分钟指示器)+11(5分钟指示器)+11(小时指示器)+ 1 = 27

2、将队列恢复原来的顺序队列需要多久?

#include “sqstack.h”


typedef int data_t;

typedef struct _stSqStack
{
	data_t *data;
	int maxlen;
	int top;
}stSqStack;


stSqStack* CreateSqStack(int maxlen);
int PushSqStack(stSqStack *s,data_t value);
data_t PopSqStack(stSqStack *s);
data_t TopSqStack(stSqStack *s);
int FUllSqStack(stSqStack *s);
int EmptySqStack(stSqStack *s);
int ClearSqStack(stSqStack *s);
int FreeSqStack(stSqStack *s);

sqstack.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqstack.h"

sqstack * stack_create(int len) {
	sqstack * s;

	if ((s =(sqstack *)malloc(sizeof(sqstack))) == NULL) {
		printf("malloc sqstack failed\n");
		return NULL;
	}

	if ((s->data = (data_t *)malloc(len * sizeof(data_t)))==NULL) {
		printf("malloc data failed\n");
		free(s);
		return NULL;
	}

	memset(s->data, 0, len*sizeof(data_t));
	s->maxlen = len;
	s->top = -1;

	return s;
}

int stack_push(sqstack * s, data_t value) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}

	if (s->top == s->maxlen-1) {
		printf("stack is full\n");
		return -1;
	}

	s->top++;
	s->data[s->top] = value;

	return 0;
}

/*
 *@ret 1-empty
 * */
int stack_empty(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	return (s->top == -1 ? 1 : 0);
}

/*
 * @ret 1-full
 * */
int stack_full(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	return  (s->top == s->maxlen-1 ? 1 : 0);
}

data_t stack_pop(sqstack *s) {
	s->top--;
	return (s->data[s->top+1]);
}

data_t stack_top(sqstack *s) {
	return (s->data[s->top]);
}

int stack_clear(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	
	s->top = -1;
	return 0;
}

int stack_free(sqstack *s) {
	if (s == NULL) {
		printf("s is NULL\n");
		return -1;
	}
	
	if (s->data != NULL) 
		free(s->data);
	free(s);

	return 0;
}

linkqueue.c

typedef int datatype;

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

typedef struct _linkqueue {
    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 = (linkqueue *)malloc(sizeof(linkqueue));
    if(lq == NULL){
        printf("malloc linkqueue fail\n");
        return NULL;
    }

    lq->front = lq->rear = (linklist)malloc(sizeof(listnode));
    if(lq->front == NULL){
        printf("malloc listnode fail\n");
         return NULL;
    }

    lq->front->data = 0;
    lq->front->next = NULL;

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

    linklist p= (linklist)malloc(sizeof(listnode));
    if(p == NULL){
    printf("malloc node fail\n");
        return -1;
    }
    p->data = x;
    p->next = NULL;
    lq->rear->next = p;//连接
    lq->rear = p;//移位

    return 0;
}
datatype dequeue(linkqueue *lq){
    linklist p;
    datatype temp;
    if (lq == NULL){
        printf("lq is NULL\n");
        return 0;
    }
    if (lq->front == lq->rear){
        printf("lq is empty\n");
        return 0;
    }
    p = lq->front;
    lq->front= p->next;
    temp = lq->front->data;
    free(p);
    p = NULL;
    
    return temp;
}
int queue_empty(linkqueue *lq){
    if (lq == NULL){
        printf("lq is NULL\n");
        return -1;
    }

    if (lq->front == lq->rear){
        printf("linkqueue is empty\n");
        return 1;
    }
    return 0;
}


int queue_clear(linkqueue *lq){
    linklist p;  
    if (lq == NULL){
        printf("lq is NULL\n");
        return 0;
    }
    
    while (!(lq->front == lq->rear))
    {
        p = lq->front;
        lq->front = p->next;
        printf("clear:%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 0;
    }
    p = lq->front;
    while (lq->front)
    {
        lq->front = p->next;
        printf("free:%d\n",p->data);
        free(p);
        p = lq->front;
    }
    free(lq);
    lq = NULL;

    return lq;
}

main.c

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

int test();
int check(linkqueue* lq);

int main()
{
    printf("Main start!\n");
    test();
    return 0;
}

int test(){
    printf("test start!\n");

    //队列
    printf("lq start!\n");
    linkqueue* lq;
    lq = queue_create();
    if(lq == NULL){
        printf("lq is NULL");
        return -1;
    }
    for(int i = 0; i < 27; i++){
        enqueue(lq,i);
    }
    // while (!queue_empty(lq))
    // {
    //     printf("%d, ",dequeue(lq));
    // }

    //时
    printf("时 start!\n");
    stSqStack* s1;
    if((s1 = CreateSqStack(11)) == NULL){
        printf("s1 is NULL");
        return -1;
    }
    // for(int i = 0; i < 11; i++){
    //     PushSqStack(s1,i);
    // }
    // while (!EmptySqStack(s1))
    // {
    //     printf("%d, ",PopSqStack(s1));
    // }

    //5分
    printf("5分 start!\n");
    stSqStack* s2;
    if((s2 = CreateSqStack(11)) == NULL){
        printf("s2 is NULL");
        return -1;
    }


    //1分
     printf("1分 start!\n");
    stSqStack* s3;
    if((s3 = CreateSqStack(4)) == NULL){
        printf("s3 is NULL");
        return -1;
    }

    int min = 0;
    int temp = 0;
    int num = 0;
    while (1)
    {
        min++;
        if(!queue_empty(lq)){
            temp = dequeue(lq);
            if(!FUllSqStack(s3)){
                PushSqStack(s3,temp);
            }else {
               while (!EmptySqStack(s3)){
                enqueue(lq,PopSqStack(s3));
               }
                if(!FUllSqStack(s2)){
                    PushSqStack(s2,temp);
                }else {
                    while (!EmptySqStack(s2)){
                        enqueue(lq,PopSqStack(s2));
                    }
                    if(!FUllSqStack(s1)){
                        PushSqStack(s1,temp);
                    }else{
                        while (!EmptySqStack(s1))
                        {
                             enqueue(lq, PopSqStack(s1));
                        }
                         enqueue(lq,temp);//00:00
                        if(check(lq)){//1:升序
                            break;
                        }
                    }
                }
            }
        }
    }
    printf("min = %d\n",min);
    printf("over\n");
    while (!queue_empty(lq))
    {
        printf("%d, ",dequeue(lq));
    }


    queue_free(lq);
    FreeSqStack(s1);
    FreeSqStack(s2);
    FreeSqStack(s3);
    return 0;
}

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

    linklist p = lq->front->next;
    while (p && p->next)
    {
        if(p->data < p->next->data){
            p = p->next;
        }else{
            return 0;//无序
        }
    }
    return 1;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

li星野

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值