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

数据结构---4.队列的应用

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

数据结构---4.队列的应用

数据结构—4.队列的应用

一、应用1

  1. 解码,对于一串数字,将第一个数字删除,第二个数字排至这串数字的末尾,将第三个数字删除,将第四个数字排至末尾,循环下去直到这串数字都删除了。删除的数字就是真正的密码。举个例子:


2. 代码实现


#include 
#include 

#define SIZE 512

char queue[SIZE];
int head, tail = 0;//队列的两个关键要素,头和尾。

void enqueue(char c);
char dequeue(void);
int is_empty(void);
int is_full(void);

int main(void)
{
        char code[12];
        int n;
        int i = 0;
        char num;

        printf("Please en a code:n");
        gets(code);

        for(n = 0; n < strlen(code); n++)
        {
                if(!is_full())
                {
                        enqueue(code[n]);//入队列
                }
        }

        while(!is_empty())//当不为空的时候,
        {
                code[i++] = dequeue();//出队列,放在code里面,第一次执行,将第一个元素放入code[]数组。牢记出队列的特点。
                if(!is_empty())
                {
                        num = dequeue();//第二次出队列,暂存在num里,
                        if(!is_full())
                                enqueue(num);//放在队列的最后,
                }
        //直到队列为空。
        }

        printf("Origral code is :n");
        for(n = 0; n < strlen(code); n++)
                printf("%c", code[n]);
        printf("n");

}


void enqueue(char c)
{
        queue[tail] = c;//不断有元素入队列,表现形式是队列的尾在不断增加。
        tail = (tail + 1) % SIZE;
}


char dequeue(void)
{
        char ch;

       ch = queue[head];
        head = (head + 1) % SIZE;

        return ch;//出队列,队列的头不断增加。
}


int is_empty(void)
{
        return head == tail;
}


int is_full(void)
{
        return ((tail + 1) % SIZE) == head;
}

运行结果:

Please en a code:
765730314892
Origral code is :
753349608721
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604397.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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