方法一:
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);
}