循环队列(严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 ...
随机推荐
- 一文带你速懂虚拟化KVM和XEN
来源 :蛋蛋团 前言 "云计算"这个技术经过十余年的普及到如今已经可以称得上是家喻户晓了,基于云计算平台,在多个领域内创造了一个又一个的记录:电子商务里亿万人同时在线抢购的的&qu ...
- .net core JWT验证,HttpContext.User为空的问题
这几天在学习张老师.net core教程JWT部分,链接 https://mp.weixin.qq.com/s/7135y3MkUlPIp-flfwscig 教程使用的.net core 2.2, 在 ...
- ansible模块的介绍与使用
ansible-doc的使用 1.ansible-doc -h可以看见ansible-doc的所有参数 2.ansible-doc 命令格式:ansible-doc [-l|-F|-s] [optio ...
- numpy学习Ⅱ
今天有空再把numpy看一下,补充点不会的,再去看matplotlib 回顾之前笔记,发现之前的numpy学习Ⅰ中关于numpy的行.列.维可能表述有点不清晰,这里再叙述一下 import numpy ...
- ubuntu helpers
linux 命令大全 Apt proxy configuration on Ubuntu 20.04 Focal Fossa Linux 临时使用socks代理apt-get的方法 docker - ...
- Fail2ban 简介
Fail2ban是一个基于日志的IP自动屏蔽工具.可以通过它来防止暴力破解攻击. Fail2ban通过扫描日志文件(例如/var/log/apache/error_log),并禁止恶意IP(太多的密码 ...
- IDEA快捷生成循环♻️
itar 生成array for代码块 //itar for (int i = 0; i < array.length; i++) { = array[i]; } itco 生成Collecti ...
- Es6语法+v-on参数相关+vue虚拟dom
Es6的语法 Es5:if和for 都没有块级作用域,函数function有作用域. Es6:加入let使得if和for有作用域 .建议: 在Es6中优先使用const,只有需要改变某一个标识符的时候 ...
- drools执行完某个规则后终止别的规则执行
目录 1.背景 2.需求 3.实现方案 1.通过Fact判断 2.通过全局变量判断 3.通过halt方法 4.实现上述需求 4.1 drl 文件编写 4.2 运行结果 5.完整代码 1.背景 在我们开 ...
- 【Unity Shader学习笔记】Unity光照基础-半兰伯特光照
在光照无法达到的区域,模型的外观通常是全黑的,没有任何明暗变化,这会使模型的背光区域看起来就像一个平面. 使用半兰伯特光照可以解决这个问题. 逐顶点光照技术也被称为兰伯特光照模型.因为它符合兰伯特定律 ...