姓名:吴夕雨 日期:2018-9-12
今日学习任务:队列的学习
今日任务完成情况:学习了队列的基本知识,队列的顺序存储和链式存储。
队列的顺序存储的循环队列,什么是空队,队头队尾重合
队尾指针,以及如何判断循环队列的长度和判断队列是否满。代码量有500行左右。
今日未解决问题:清空队列的编译
今日开发收获:理解了关于队列的知识,以及用顺序存储、链式存储编写队列的程序
自我评价:基本能完成任务,对于新的知识要多加练习
顺序存储队列:
main.c
#include <stdio.h>
#include "queue.h"
int main()
{
Q queue;//定义队列
int ret,i;
ret=InitQueue(&queue);//初始化队列
if(ret==SUCCESS)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
//进队/
for(i=0;i<10;i++)
{
ret=EnterQueue(&queue,i+1);
if(ret==FAILURE)
{
printf("Enter Failure!\n");
}
else
{
printf("Enter %d Success!\n",i+1);
}
}
///////////////////
//////出队//////
for(i=0;i<5;i++)
{
ret=DelQueue(&queue);
if(ret==FAILURE)
{
printf("Delete Failure!\n");
}
else
{
printf("Delete %d Success!\n",ret);
}
}
//////////////////////////////////////
//////队列长度/////////////////////////////////////////////////////////////
ret=LengthQueue(queue);
printf("length is %d\n",ret);
////////////////////////////////////////////////////////////////////////////
//////////清空队列/////////////////////////////////////////////////////////
ret=ClearQueue(&queue);
if(ret==SUCCESS)
{
printf("Clear Success!\n");
}
else
{
printf("Clear Failure!\n");
}
///////////////////////////////////////////////////////////////////////////
////////销毁队列////////////////////////////////////////////////////////////
ret=DestroyQueue(&queue);
if(ret==SUCCESS)
{
printf("Destroy Success!\n");
}
else
{
printf("Destroy Success!\n");
}
//////////////////////////////////////////////////
queue.c
#include "queue.h"
#include <stdlib.h>
#include <stdio.h>
/////队列初始化///////////////////////////////////////////////////////////////////////////////////////////////
int InitQueue(Q *q)
{
if(q==NULL)//入参判断
{
return FAILURE;
}
//申请一块 内存,并让data指向该空间
q->data=(int *)malloc(sizeof(int) * MAXSIZE);
if(NULL==q->data)//返回值判断 (如果申请失败)
{
return FAILURE;
}
q->rear=q->front=0;//队头队尾指针指向同一个
return SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//进队//
int EnterQueue(Q *q,int e)
{
if(q==NULL)//入参判断
{
return FAILURE;
}
if((q->rear+1)%MAXSIZE==q->front)//队满
{
return FAILURE;
}
q->data[q->rear]=e;
q->rear=(q->rear+1)%MAXSIZE;
return SUCCESS;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////出队/////////////////////////////////////////////////////////////////////////////////////////////////////
int DelQueue(Q *q)
{
if(q==NULL)//入参判断
{
return FAILURE;
}
if(q->rear==q->front)//判断是否为空队
{
return FAILURE;
}
int e=q->data[q->front];
q->front=(q->front+1)%MAXSIZE;
return e;
}
////////////////////////////////////////////////////////////////////////////
/////////队列长度///////////////////////////////////////////////////////////
int LengthQueue(Q q)
{
return (q.rear-q.front+MAXSIZE)%MAXSIZE;
}
////////////////////////////////////////////////////////////////////////////
////////////////清空队列////////////////////////////////////////////////////
int ClearQueue(Q *q)
{
if(q==NULL)
{
return FAILURE;
}
q->rear = q->front;
return SUCCESS;
}
////////////////////////////////////////////////////////////////////
//////销毁队列/////////////////////////////////////////////////////////////
int DestroyQueue(Q *q)
{
if(NULL==q)//入参判断
{
return FAILURE;
}
free(q->data);//释放空间
return SUCCESS;
}
/////////////////////////////////////////////////////
queue.h
#ifndef QUEUE_H
#define QUEUE_H
#define MAXSIZE 10 //队列容量
#define SUCCESS 1000
#define FAILURE 1001
struct queue
{
int *data;//指向队列的存储空间
int front;//队头指针
int rear;//队尾指针
};
typedef struct queue Q;
int InitQueue(Q *q);//初始化
int EnterQueue(Q *q,int e);//进队
int DelQueue(Q *q);//出队
int LengthQueue(Q q);//队列长度
#ifndef QUEUE_H
#define QUEUE_H
#define MAXSIZE 10 //队列容量
#define SUCCESS 1000
#define FAILURE 1001
struct queue
{
int *data;//指向队列的存储空间
int front;//队头指针
int rear;//队尾指针
};
typedef struct queue Q;
int InitQueue(Q *q);//初始化
int EnterQueue(Q *q,int e);//进队
int DelQueue(Q *q);//出队
int LengthQueue(Q q);//队列长度
int ClearQueue(Q *q);//清空队列
int DestroyQueue(Q *q);//销毁队列
#endif