栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

湖景房问题C语言队列实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

湖景房问题C语言队列实现

之前接单做了这个湖景房问题,感到非常有趣,上传到csdn记录下来。

#include 
#include 
#define MAXLEN 100


typedef struct _houseinfo
{
    int id;
    int height;
}House;


typedef struct _queue
{
    House a[MAXLEN];
    int fornt;
    int base;
}Queue;

//初始化队列
void init(Queue* q)
{
    q->base = 0;
    q->fornt = -1;
}

//入队
void push(Queue* q, House d)
{
    q->fornt++;
    q->a[q->fornt] = d;
}

//出队
int pop(Queue* q, House* e)
{
    if (q->fornt < q->base)
        return 0;
    *e = q->a[q->base];
    q->base++;
    return 1;
}

//判断是否为空
int empty(Queue q)
{
    if (q.base > q.fornt)
        return 1;
    else
        return 0;
}


int main()
{
    int n, i;
    int hmax=0;
    Queue q;
    init(&q);
    printf("请输入n:");
    scanf("%d", &n);
    printf("请输入从东向西%d个房屋的高度:",n);
    int* a = (int*)malloc(n * sizeof(int));
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    //入队
    for (i = n - 1; i >= 0; i--)
    {
        House h;
        h.height = a[i];
        h.id = i + 1;
        push(&q, h);
    }
    //第一个出队
    House hh;
    printf("所有的海景房:n");
    while (!empty(q))
    {
        pop(&q, &hh);
        if (hh.height > hmax)
        {
            printf("第%d栋,房高%d米n", hh.id,hh.height);
            hmax = hh.height;
        }
    }
    return 0;
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号