方法一:
status.h
#ifndef STATUS_H
#define STATUS_H
#define YES 1
#define NO 0
typedef int Status;
typedef int ElemType;
#endif
list.h
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include "status.h"
#define MAX_SIZE 5
typedef struct Queue{
ElemType sq[MAX_SIZE];
//ElemType *sq;
int front;
int real;
int length;
}Queue;
Status InitQueue(Queue *p);
Status QueueEmpty(Queue *p);
Status QueueFull(Queue *p);
Status EnQueue(Queue *p,ElemType value);
void PutQueue(Queue *p);
Status DeQueue (Queue *p,ElemType *value);
Status GetHead(Queue *p,ElemType *value);
#endif
list.c
#include <stdio.h>
#include "list.h"
//初始化队列
Status InitQueue(Queue *p)
{
//sq=()malloc();
p->front=p->real=0;
p->length=0;
return YES;
}
//判断队列是否为空
Status QueueEmpty(Queue *p)
{
if(p->length==0)
return YES;//空
else
return NO;//非空
}
//判断队列是否满了
Status QueueFull(Queue *p)
{
if(p->length==MAX_SIZE)
return YES;//满
else
return NO;//未满
}
//入队
Status EnQueue(Queue *p,ElemType value)
{
if(QueueFull(p))
return NO;
p->sq[p->real]=value;
p->length++;
//p->real=(p->real+1)%MAX_SIZE;
p->real++;
if(p->real==MAX_SIZE)
p->real=0;
return YES;
}
//输出
void PutQueue(Queue *p)
{
int i,k;
k=p->front;
printf("此时队列中的元素为;");
for(i=0;i<p->length;i++,k++)
{
printf("%d ",p->sq[k]);
if(k==MAX_SIZE-1)
k=-1;
}
printf("\n");
}
//出队
Status DeQueue (Queue *p,ElemType *value)
{
if(QueueEmpty(p))
return NO;
*value=p->sq[p->front];
p->front++;
p->length--;
if(p->front==MAX_SIZE)
p->front=0;
return YES;
}
//获取队头元素
Status GetHead(Queue *p,ElemType *value)
{
if(QueueEmpty(p))
return NO;
*value=p->sq[p->front];
return YES;
}
main.c
#include <stdio.h>
#include "list.h"
int main()
{
Queue qu;
Queue *p=&qu;
ElemType value;
int i,t;
printf("================初始化队列:=================\n");
t=InitQueue(p);
if(t==NO)
printf("初始化失败");
else
printf("初始化成功") ;
printf("\n===============判断队列是否为空=============\n");
t=QueueEmpty(p);
if(t==YES)
printf("队列为空");
else
printf("队列不为空");
printf("\n===============判断队列是否满=============\n");
t=QueueFull(p);
if(t==YES)
printf("队列满");
else
printf("队列未满");
printf("\n================入队====================\n");
printf("请依次输入%d个元素:",MAX_SIZE);
for(i=0;i<MAX_SIZE;i++)
{
scanf("%d",&value);
EnQueue(p,value) ;
}
PutQueue(p);
printf("================出队====================\n");
DeQueue (p,&value);
printf("出队的元素为:%d\n",value);
PutQueue(p);
printf("================获取队头元素====================\n");
GetHead(p,&value);
printf("队头元素为:%d\n",value);
}
方法二:
status.h
#ifndef STATUS_H
#define STATUS_H
#define YES 1
#define NO 0
typedef int Status;
typedef int ElemType;
#endif
list.h
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include "status.h"
#define MAX_SIZE 5
typedef struct Queue{
ElemType sq[MAX_SIZE];
int front;//指向队头元素的下标
int real;//指向队尾元素的下标
int length;
}Queue;
/*
(1)初始化队列 InitQueue(Q)
(2)入队 EnQueue(Q,item)
(3)出队 DeQueue(Q,item)
(4)获取队头元素内容 GetHead(Q,item)
(5)判断队列是否为空 QueueEmpty(Q)*/
Status InitQueue(Queue *p);
Status QueueEmpty(Queue *p);
Status QueueFull(Queue *p);
Status EnQueue(Queue *p,ElemType value);
void PutQueue(Queue *p);
Status DeQueue (Queue *p,ElemType *value);
Status GetHead(Queue *p,ElemType *value);
#endif
list.c
#include <stdio.h>
#include "list.h"
//初始化队列
Status InitQueue(Queue *p)
{
p->front=p->real=0;
p->length=0;
return YES;
}
//判断队列是否为空
Status QueueEmpty(Queue *p)
{
if(p->length==0)
return YES;//空
else
return NO;//非空
}
//判断队列是否满了
Status QueueFull(Queue *p)
{
if(p->length==MAX_SIZE)
return YES;//满
else
return NO;//未满
}
//入队
Status EnQueue(Queue *p,ElemType value)
{
p->sq[p->real]=value;
p->length++;
if(QueueFull(p))
return NO;
if(p->real+1==MAX_SIZE)
p->real=0;
else
p->real++;
return YES;
}
//输出
void PutQueue(Queue *p)
{
int i;
printf("此时队列中的元素为;");
i = p->front;
for(;i!=p->real;i++)
{
printf("%d ",p->sq[i]);
if(i==MAX_SIZE-1)
i=-1;
}
printf("%d ",p->sq[i]);
p->length++;
printf("\n");
}
//出队
Status DeQueue (Queue *p,ElemType *value)
{
if(QueueEmpty(p))
return NO;
*value=p->sq[p->front];
p->front=p->front+1;
p->length--;
return YES;
}
//获取队头元素
Status GetHead(Queue *p,ElemType *value)
{
if(QueueEmpty(p))
return NO;
*value=p->sq[p->front];
return YES;
}
main.c
#include <stdio.h>
#include "list.h"
int main()
{
Queue qu;
Queue *p=&qu;
ElemType value;
int i,t,n;
printf("================初始化队列:=================\n");
t=InitQueue(p);
if(t==NO)
printf("初始化失败");
else
printf("初始化成功") ;
printf("\n===============判断队列是否为空=============\n");
t=QueueEmpty(p);
if(t==YES)
printf("队列为空");
else
printf("队列不为空");
printf("\n===============判断队列是否满=============\n");
t=QueueFull(p);
if(t==YES)
printf("队列满");
else
printf("队列未满");
printf("\n================入队====================\n");
printf("请依次输入%d个元素:",MAX_SIZE);
for(i=0;i<MAX_SIZE;i++)
{
scanf("%d",&value);
EnQueue(p,value) ;
}
PutQueue(p);
printf("================出队====================\n");
DeQueue (p,&value);
printf("出队的元素为:%d\n",value);
PutQueue(p);
printf("================获取队头元素====================\n");
GetHead(p,&value);
printf("队头元素为:%d\n",value);
}