发布时间:2022-12-12 17:00
#include
#include
#include "queue.h"
int main()
{
int i;
Type x;
Type arr[] = {3,1,2,5,7,9};
QUEUE *q = NULL;
q = CreateQueue(10);
if(NULL == q)
return -1;
for(i = 0; i < sizeof(arr)/sizeof(*arr); i++)
{
EnQueue(q, arr + i);
}
FrontQueue(q, &x);
printf("x = %d\n", x);
DisptoryQueue(q);
return 0;
}
---------------------------------------------------------------
#ifndef _QUEUE_H__
#define _QUEUE_H__
struct node;
typedef int Type;
typedef struct node QUEUE;
QUEUE *CreateQueue(int);
void QueueMakeEmpty(QUEUE *);
int QueueIsEmpty(QUEUE *);
int QueueIsFull(QUEUE *);
int EnQueue(QUEUE *, const Type *);
int DeQueue(QUEUE *);
int FrontQueue(QUEUE *, Type *);
int FrontAndDeQueue(QUEUE *, Type *);
void DisptoryQueue(QUEUE *);
struct node{
Type *data;
int capacity;
int front;
int rear;
int size;
};
#endif
-------------------------------------------------------------------
#include
#include
#include "queue.h"
QUEUE *CreateQueue(int size)
{
QUEUE *q = malloc(sizeof(*q));
if(NULL == q)
return NULL;
q->data = malloc(sizeof(Type)*size); //队列的长度,队列的成员个数
if(NULL == q->data)
{
free(q);
return NULL;
}
q->capacity = size; //队列容量
QueueMakeEmpty(q);
return q;
}
void QueueMakeEmpty(QUEUE *q)
{
q->size = 0;
q->front = 1;
q->rear = 0;
}
int QueueIsEmpty(QUEUE *q)
{
return q->size == 0;
}
int QueueIsFull(QUEUE *q)
{
return q->size == q->capacity;
}
static int repeat(QUEUE *q, int rear) //队列队尾入队,
{
if(++rear == q->capacity)
rear = 0;
return rear;
}
int EnQueue(QUEUE *q, const Type *x)
{
if(QueueIsFull(q))
return -1;
q->rear = repeat(q, q->rear); //每次入队成功后,队尾rear置0.
q->data[q->rear] = *x;
q->size++;
return 0;
}
int DeQueue(QUEUE *q) //出队
{
if(QueueIsEmpty(q))
return -1;
q->front = repeat(q, q->front);
q->size--;
return 0;
}
int FrontQueue(QUEUE *q, Type *x) //查看队首
{
if(QueueIsEmpty(q))
return -1;
*x = q->data[q->front];
return 0;
}
int FrontAndDeQueue(QUEUE *q, Type *x) //查看队首并出队
{
if(FrontQueue(q, x) == 0)
return DeQueue(q);
return -1;
}
void DisptroyQueue(QUEUE *q)
{
free(q->data);
free(q);
}
【MindSpore-GPU-1.1.0】【LeNet5】训练报cudaHostAlloc failed
Dockerfile 和 docker-compose.yml的区别(转载)
Datenlord | Rust 语言无锁数据结构的内存管理
TensorFlow深度学习应用开发实战(深度学习简介和开发环境搭建)
Vue2.7正式发布!代号为:Naruto(火影忍者),原生支持 Composition API +终于可以在Vue2项目中使用Vue3的新特性了,真香~
手把手教你配置Pytorch环境并使用(Win10系统下基于Anaconda完成的pytorch1.7.1和torchvision0.8.2的Pytorch深度学习环境搭建)
Linux—用户新建目录和文件的默认权限设置:umask详解
Github Star 全球唯一的国产程序员!Apache 顶级项目Commiter。
matlab 分类神经网络,图事-matlab之神经网络分类