循环队列(严3.30)--------西工大NOJ习题.9

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct _Queue
{
int max_size;
int rear;
int lenth;
int* data;
}Queue;
Queue* Create(int n)
{
Queue* Q = (Queue*)malloc(sizeof(Queue));
Q->data = (int*)calloc(n, sizeof(int));
Q->lenth = 0;
Q->max_size = n;
Q->rear = 0;//注意:当循环队列内部元素个数为0时,rear无意义。
return Q;
}
int Push(Queue* Q, int e)
{
if (Q->lenth == 0)
{
Q->rear = 0;
Q->lenth = 1;
Q->data[0] = e;
return 1;
}
if (Q->lenth >= Q->max_size)
{
return 0;
}
Q->rear = (Q->rear + 1) % Q->max_size;
Q->data[Q->rear] = e;
Q->lenth++;
return 1;
}
int Get_head(Queue* Q)
{
int t = Q->rear - Q->lenth + 1;
if (t < 0)
{
t = Q->max_size + t;
}
return Q->data[t];
}
int Full(Queue* Q)
{
if (Q->lenth >= Q->max_size)
return 1;
else
return 0;
}
int Empty(Queue* Q)
{
if (Q->lenth == 0)
return 1;
else
return 0;
}
void Print(Queue* Q)
{
int t = Q->rear - Q->lenth + 1;
if (t < 0)
{
t = Q->max_size + t;
}
for (int i = 0; i < Q->lenth; i++)
{
printf("%d ", Q->data[t]);
t = (t + 1) % Q->max_size;
}
}
void Read(Queue* Q)
{
char buf[12];
while (1)
{
scanf("%s", buf);
if (buf[0] == 'n')
return;
if (buf[0] == 'y')
return;
Push(Q, atoi(buf));
}
}
void Delete(Queue* Q, int e)
{
int t = Q->rear - Q->lenth + 1;
if (t < 0)
{
t = Q->max_size + t;
}
while (Q->data[t] != e)
{
Q->lenth -= 1;
t = (t + 1) % Q->max_size;
}
if (Get_head(Q) == e)
Q->lenth -= 1;
}
int main()
{
int n;
scanf("%d", &n);
Queue* Q = Create(n);
Read(Q);
int elem;
scanf("%d", &elem);
Delete(Q,elem);
Print(Q);
putchar('\n');
printf("%d", Get_head(Q));
return 0;
}
循环队列(严3.30)--------西工大NOJ习题.9的更多相关文章
- K阶斐波那契数列--------西工大NOJ习题.10
K阶斐波那契数列--------西工大NOJ习题.10 原创不易,转载请说明出处!!! 科普:k阶斐波那契数列的0到n-1项需要有初始值. 其中,0到n-2项初始化为0,第n-1项初始化为1. 在这道 ...
- 求广义表深度(严5.30)--------西工大noj
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef enum{ATOM, LIST ...
- 建立二叉树的二叉链表(严6.65)--------西工大noj
需要注意的点:在创建二叉树的函数中,如果len1==len2==0,一定要把(*T)置为NULL然后退出循环 #include <stdio.h> #include <stdlib. ...
- 二叉排序树的合并(严3.98)--------西工大noj
二叉排序树的合并有三种方法 先存入数组,然后..... 直接在第二个树上添加第一个数的元素,时间复杂度为O(NlogN) 就像是合并数组一样合并二叉排序树,分别扫描,时间复杂度极低. 第三种我写了一下 ...
- 输出以二叉树表示的算术表达式(严6.51)--------西工大noj
题解 这道题目说的很诡异,其实没有什么把括号补上....仅仅是先序读入,然后中序输出就行了 代码 #include <stdio.h> #include <stdlib.h> ...
- 建立二叉树的二叉链表存储结构(严6.70)--------西工大noj
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct TreeNode ...
- 以十字链表为存储结构实现矩阵相加(严5.27)--------西工大noj
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef int ElemT ...
- 基于图的广度优先搜索策略(耿7.11)--------西工大noj.20
目录 代码 代码 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct ...
- 基于图的深度优先搜索策略(耿7.10)--------西工大noj
代码 代码 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct ...
随机推荐
- 请收藏,Linux 运维必备的 40 个命令总结,收好了~
点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 1.删除0字节文件 find -type f -size ...
- Unity实现简单的对象池
一.简介 先说说为什么要使用对象池 在Unity游戏运行时,经常需要生成一些物体,例如子弹.敌人等.虽然Unity中有Instantiate()方法可以使用,但是在某些情况下并不高效.特别是对于那些需 ...
- openstack命令创建云主机实例
@ 目录 前言 上传centos镜像 创建实例 创建外网卡 修改安全组规则 创建云主机实例 前言 简单创建云主机实例只需要上传一个测试镜像.创建一张外网卡.创建一个实例类型.修改安全组规则即可 注:这 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- H5如何实现唤起APP
前言 写过hybrid的同学,想必都会遇到这样的需求,如果用户安装了自己的APP,就打开APP或跳转到APP内某个页面,如果没安装则引导用户到对应页面或应用商店下载.这里就涉及到了H5与Native之 ...
- 1.Spring开发环境搭建——intellj
1.在intellj中新建项目,选择JDK版本(1.8版本) 2.选择相关信息填写,注意Java版本要和上面步骤选择的版本一致. 3.选择springBoot版本,勾选Spring Web选项. 4. ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- dd-文件系统内容映射
复制文件并对原文件的内容进行转换和格式化处理. 语法 dd [OPTIONS]... 选项 if=filename 指定源文件. of=filename 指定目的文件. ibs=bytes 指定一个块 ...
- CSS的几种选择器
选择器 目录 选择器 基础选择器 标签选择器 类选择器 id选择器 通配符选择器 复合选择器 后代选择器 子选择器 并集选择器 伪类选择器 基础选择器 标签选择器 标签选择器可以把一类标签全部选择出来 ...
- Docker容器编译安装Redis
Docker容器编译安装Redis 1.创建容器 -i 交互模式 -d 后端运行 -h 容器的hostname --name 容器名 --network 网卡 --ip IP地址 -p 端口映射 -- ...