#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h> //队列长度自定义
#define ASIZE 1001
#define MAX_INT 2147483647 typedef struct quque {
int data[ASIZE];
int front;
int rear;
}Queue; //空0,满-1,正常 1
int notEmpty(Queue *queue)
{
if ((*queue).front == (*queue).rear)
return ;
else if ( ((*queue).rear+)%ASIZE == (*queue).front )
return -;
else return ;
} //测量队列长度
int lengthOfQueue (Queue (*queue))
{
if ((*queue).rear >= (*queue).front)
return (*queue).rear-(*queue).front;
else return (*queue).rear + ASIZE - (*queue).front;
} //push
void pushToQueue(Queue (*queue), int one)
{
if (notEmpty(queue) == -)
{
printf("⚐队列已满,无法PUSH");
return;//队满
}
(*queue).data[(*queue).rear] = one;
(*queue).rear = ((*queue).rear +)%ASIZE;
} //pop,队空返回max_int
int popFromQueue(Queue (*queue))
{
if (!notEmpty(queue))
{
printf("⚐队列为空,无法POP");
return MAX_INT;
}
int pop = (*queue).data[(*queue).front];
(*queue).front = ((*queue).front + )%ASIZE;
return pop;
}
//仅读取队首,不出队
int readTopOfQueue(Queue (*queue))
{
if (notEmpty(queue))
return (*queue).data[(*queue).front];
else
{
printf("⚐队列为空,无法读取");
return -;
}
}
//创建队列
Queue* creatAQueue()
{
Queue *queue = (Queue *)malloc(sizeof(Queue));
(*queue).front = ;
(*queue).rear = ;
return queue;
} //打印队列
void printQueue(Queue (*queue))
{
if ((*queue).front == (*queue).rear)
{
printf("\n⚐队列为空\n");
return;
}
printf("\n队首的下标为 %d ,队尾的下标为 %d \n队列为:",(*queue).front,(*queue).rear);
for (int i = (*queue).front ; i != (*queue).rear ; i = (i+)%ASIZE )
{
printf (" %d ",(*queue).data[i]);
}
printf("\n");
} int main()
{
Queue *queue = creatAQueue();
int order;
int one;
while () {
printf("\n☀︎命令:1-进行入队操作,2-进行出队操作,3-打印队列\n✎请输入指令:");
scanf("%d",&order);
switch (order) {
case :
printf("\n✎请输入需要入队的数值:");
scanf("%d",&one);
if (notEmpty(queue) == -)
{
printf("\n⚐队列已满\n");
break;
}
pushToQueue(queue,one);
break;
case :
if (notEmpty(queue) == )
{
printf("\n⚐队列还是空的哟~\n");
break;
}
one = popFromQueue(queue);
printf("\n出队的数据为%d\n",one);
break;
case :
printQueue(queue);
break;
default:
printf("\n⚐指令有误\n");
break;
}
}
return ;
}

[数据结构] 用C语言模拟一个简单的队列程序的更多相关文章

  1. 用C语言编写一个简单的词法分析程序

    问题描述: 用C或C++语言编写一个简单的词法分析程序,扫描C语言小子集的源程序,根据给定的词法规则,识别单词,填写相应的表.如果产生词法错误,则显示错误信息.位置,并试图从错误中恢复.简单的恢复方法 ...

  2. socket实例C语言:一个简单的聊天程序

    我们老师让写一个简单的聊天软件,并且实现不同机子之间的通信,我用的是SOCKET编程.不废话多说了,先附上代码: 服务器端server.c #include <stdio.h> #incl ...

  3. java 使用LinkedList模拟一个堆栈或者队列数据结构

    近期在复习下java基础,看了下java基础,在看到集合时突然发现想起来曾经面试有一道笔试题:模拟一个堆栈或者队列数据结构,当时还没做出来,今天就写一下,首先得明确堆栈和队列的数据结构 堆栈:先进后出 ...

  4. 使用LinkedList模拟一个堆栈或者队列数据结构

    使用LinkedList模拟一个堆栈或者队列数据结构. 堆栈:先进后出  如同一个杯子. 队列:先进先出  如同一个水管. import java.util.LinkedList; public cl ...

  5. java集合 collection-list-LinkedList 模拟一个堆栈或者队列数据结构。

    /* 使用LinkedList模拟一个堆栈或者队列数据结构. 堆栈:先进后出 如同一个杯子. 队列:先进先出 First in First out FIFO 如同一个水管. */ import jav ...

  6. Java LinkedList特有方法程序小解 && 使用LinkedList 模拟一个堆栈或者队列数据结构。

    package Collection; import java.util.LinkedList; /* LinkedList:特有的方法 addFirst()/addLast(); getFirst( ...

  7. 面试题:使用LinkedList来模拟一个堆栈或者队列数据结构

    请使用LinkedList来模拟一个堆栈或者队列数据结构. 堆栈:先进后出 First In Last Out  (FILO) 队列:先进先出 First In First Out  (FIFO) 我 ...

  8. 用Go语言实现一个简单的聊天机器人

    一.介绍 目的:使用Go语言写一个简单的聊天机器人,复习整合Go语言的语法和基础知识. 软件环境:Go1.9,Goland 2018.1.5. 二.回顾 Go语言基本构成要素:标识符.关键字.字面量. ...

  9. 编写一个简单的C++程序

    编写一个简单的C++程序 每个C++程序都包含一个或多个函数(function),其中一个必须命名为main.操作系统通过调用main来运行C++程序.下面是一个非常简单的main函数,它什么也不干, ...

随机推荐

  1. D3 learning notes

    D3 https://d3js.org/ 数据驱动文档显示, 利用 SVG HTML CSS技术. D3.js is a JavaScript library for manipulating doc ...

  2. 动态解析xml,并生成excel,然后发邮件。

    直接贴代码了! DECLARE @CurrentServer NVARCHAR(100)DECLARE @CurrentDatabase NVARCHAR(100)DECLARE @CurrentLo ...

  3. 浅入深出Vue系列

    浅入深出Vue导航 导航帖,直接点击标题即可. 文中所有涉及到的资源链接均在最下方列举出来了. 前言 基础篇 浅入深出Vue:工具准备之WebStorm搭建及配置 浅入深出Vue之工具准备(二):Po ...

  4. vmware虚拟机磁盘挂载

    执行mount命令时找不到介质或者mount:no medium found的解决办法 使用vmware时,在虚拟机设置里,设置CD/DVD为系统镜像,挂载时,有时会有找不到介质或者no medium ...

  5. 命名空间的using声明

    using声明具有如下的形式: using namespace::name; 一旦声明了上述语句,就可以直接访问命名空间中的名字: #include<iostream> //using声明 ...

  6. sql注入中关于--+的一点探索

    在sql-labs游戏中,经常使用--+放在最后注释多余部分,而mysql中的注释符为#和-- 却不能直接使用,以前没学过mysql,一直不理解,也不知道+号的作用,今天有时间特地探索了一下,算是搞明 ...

  7. 函数节流和函数防抖JavaScript实现

    函数节流 function throttle(fn, delay = 1000) { let Running = false; return function () { if (Running) { ...

  8. NB群友

    链接:https://ac.nowcoder.com/acm/contest/625/A来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言26214 ...

  9. SpringBoot相关错误

    1.org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V 搭建spr ...

  10. 解决:win7右键打开方式添加应用程序无法设置和删除多余的打开方式

    win7右键打开方式添加应用程序无法设置 点击“开始”—“运行”,输入“regedit”打开注册表,在“HKEY_CLASSES_ROOT\Applications\”中找到无法添加的程序 ( 比如“ ...