2008秋季-计算机软件基础-0917课堂用例(2)
循环队列
参见:http://www.cnblogs.com/emanlee/archive/2007/09/17/895463.html
#include<stdlib.h>
//定义队列的结构
struct queue
{
int q[4];//存放数据元素
int front;//队头指针,指向队头
int rear;//队尾指针,队尾指针始终指向队尾元素的后一个位置
};
//初始化队列
struct queue * InitialQueue()
{
struct queue * head;
head=(struct queue *)
malloc(sizeof( struct queue ));
head->front=0;
head->rear=0;
return head;
}
void EnterIntoQueue(struct queue * head,
int value)
{
if(head->front== (head->rear+1)%4)
{
printf("Queue is full. Enter failed.\n");
return;
}
head->q[head->rear]=value;
head->rear=( head->rear+1)%4;
}
void DeleteFromQueue(struct queue * head)
{
if(head->front==head->rear)
{
printf("Queue is empty, Delete failed\n");
}
else
{
head->front=(head->front+1)%4;
}
}
void ShowQueue(struct queue * head)
{
/* 输出要分开设计 */
int i;
printf("\n队列元素\n");
for(i=0;i<=3;i++)
printf(" %d ",head->q[i]);
}
void main()
{
struct queue * head;
head=InitialQueue();
EnterIntoQueue(head,1);
ShowQueue(head);
EnterIntoQueue(head,2);
ShowQueue(head);
}
//定义队列的结构
struct queue
{
int q[4];//存放数据元素
int front;//队头指针,指向队头
int rear;//队尾指针,队尾指针始终指向队尾元素的后一个位置
};
//初始化队列
struct queue * InitialQueue()
{
struct queue * head;
head=(struct queue *)
malloc(sizeof( struct queue ));
head->front=0;
head->rear=0;
return head;
}
void EnterIntoQueue(struct queue * head,
int value)
{
if(head->front== (head->rear+1)%4)
{
printf("Queue is full. Enter failed.\n");
return;
}
head->q[head->rear]=value;
head->rear=( head->rear+1)%4;
}
void DeleteFromQueue(struct queue * head)
{
if(head->front==head->rear)
{
printf("Queue is empty, Delete failed\n");
}
else
{
head->front=(head->front+1)%4;
}
}
void ShowQueue(struct queue * head)
{
/* 输出要分开设计 */
int i;
printf("\n队列元素\n");
for(i=0;i<=3;i++)
printf(" %d ",head->q[i]);
}
void main()
{
struct queue * head;
head=InitialQueue();
EnterIntoQueue(head,1);
ShowQueue(head);
EnterIntoQueue(head,2);
ShowQueue(head);
}
分类:
[18] 数据结构与算法
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 分享4款.NET开源、免费、实用的商城系统
2007-09-17 2008秋季-计算机软件基础-循环链队列
2007-09-17 2008秋-计算机软件基础-循环顺序队列