C语言实现常用数据结构——队列
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 10
/* 用一个动态数组来实现队列 */ typedef struct Queue {
int Capacity;
int Front;
int Rear;
int Size;
int data[MAX_SIZE];
} Queue; void Error(char *error) {
printf("%s",error);
} void FatalError(char *fatalerror) {
printf("%s",fatalerror);
} int IsEmpty(Queue *Q) {
return Q->Size == ;
} int IsFull(Queue *Q) {
return Q->Size == Q->Capacity;
} void Init( Queue *Q ) {
Q->Size = ;
Q->Front = ;
Q->Rear = ;
Q->Capacity = MAX_SIZE;
} static int Succ(int value,Queue *Q) {
if(++value == Q->Capacity) {
value = ;
}
return value;
} void Enqueue(int X,Queue *Q) {
if( IsFull( Q ) )
FatalError("Full queue");
else {
Q->Size++;
Q->Rear = Succ(Q->Rear,Q);
Q->data[ Q->Rear ] = X;
}
} void Dequeue(Queue *Q) {
if(IsEmpty(Q))
FatalError("Empty queue");
else {
Q->Size--;
Q->Front = Succ(Q->Front,Q);
}
} int FrontAndDequeue(Queue *Q) {
int Tmp;
if(IsEmpty(Q))
Error("Empty queue");
else {
Q->Size--;
Tmp = Q->data[Q->Front];
Q->Front = Succ(Q->Front,Q);
return Tmp;
}
} void DisposeQueue( Queue *Q ) {
free(Q->data);
free(Q);
} main() {
Queue *q ;
Init(q);
int i;
for(i = ; i <MAX_SIZE; i++) {
Enqueue(i,q);
}
for(i = ; i <MAX_SIZE; i++) {
printf("%d\n",FrontAndDequeue(q));
}
}
C语言实现常用数据结构——队列的更多相关文章
- C语言实现常用数据结构——链表
#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; ...
- C语言实现常用数据结构——堆
#include<stdio.h> #include<stdlib.h> #define CAPACITY 20 /*堆有两个性质: * 1.结构性:堆必须是一颗完全二叉树 * ...
- C语言实现常用数据结构——图
#include<stdio.h> #include<stdlib.h> #define SIZE 20 #define LENGTH(a) (sizeof(a)/sizeof ...
- C语言实现常用数据结构——二叉树
#include<stdio.h> #include<stdlib.h> #define SIZE 10 typedef struct Tree { int data; str ...
- C语言实现常用数据结构——栈
#include<stdio.h> #include<stdlib.h> //用链表实现栈 typedef struct Node { int data; struct Nod ...
- C语言数据结构-队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作
1.数据结构-队列的实现-C语言 //队列的存储结构 #define MAXSIZE 100 typedef struct { int* base; //基地址 int _front; //头指针 i ...
- 1. C语言中的数据结构.md
C语言内建数据结构类型 整型 整型数据是最基本的数据类型,不过从整形出发衍生出好几种integer-like数据结构,譬如字符型,短整型,整型,长整型.他们都是最基本的方式来组织的数据结构,一般是几位 ...
- Java 集合框架(常用数据结构)
早在Java 2中之前,Java就提供了特设类.比如:向量(Vector).栈(Stack).字典(Dictionary).哈希表(Hashtable)这些类(数据结构)用来存储和操作对象组.虽然这些 ...
- C++常用数据结构的实现
常用数据结构与算法的实现.整理与总结 我将我所有数据结构的实现放在了github中:Data-Structures-Implemented-By-Me 常用数据结构与算法的实现.整理与总结 KMP字符 ...
随机推荐
- java 读取项目外面配置文件的方法
public static void loadProps(String propertiesUrl) { props = new Properties(); InputStream in = null ...
- xml报错(dtd):The markup declarations contained or pointed to by the document type declaration must be well-formed
文件后缀为.xml里如下一行报错“The markup declarations contained or pointed to by the document type declaration mu ...
- 《大规模Web服务开发技术》
Web 服务开发的心灵鸡汤 周末去上海陪妹子的两天在路途上看完了这本<大规模 Web 服务开发技术>. <大规模 Web 服务开发技术>是日本的 Hetena 团队以夏天举办的 ...
- C#6
C#6 1. 只读自动属性(Read-only auto-properties) C# 6之前我们构建只读自动属性: 1 public string FirstName { get; privat ...
- Android 平台下Ftp 使用模拟器需要注意的问题
以下代码在pc上测试通过,可是在android模拟器上就不工作,不过还可以链接到服务器但不能得到文件 纠结了半天,原来是模式的问题,具体请Google 模拟器中采用建议被动模式 public void ...
- 数据批量插入MSSQL
MSSQL数据批量插入优化详细 序言 现在有一个需求是将10w条数据插入到MSSQL数据库中,表结构如下,你会怎么做,你感觉插入10W条数据插入到MSSQL如下的表中需要多久呢? 或者你的批量数据 ...
- jQuery中对象的构建
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- String方法总结
蓝背景为与Array相同的方法 一.字符方法 charAt(index):返回在指定位置的字符. var str="abcdefg"; //undefined str[0] //& ...
- 图灵机(Turing Machine)
图灵机,又称图灵计算.图灵计算机,是由数学家阿兰·麦席森·图灵(1912-1954)提出的一种抽象计算模型,即将人们使用纸笔进行数学运算的过程进行抽象,由一个虚拟的机器替代人们进行数学运算. 所谓的图 ...
- Information centric network (icn) node based on switch and network process using the node
The present invention relates to an apparatus for supporting information centric networking. An info ...