#include <iostream>
using namespace std;
#define MAXSIZE 5//隊列長度
typedef struct
{
int data[MAXSIZE];
int tail;//尾
int head;//頭
}queue;
//建立空隊列
queue* creat_queue()
{
queue* q=NULL;
if((q=(queue*)malloc(sizeof(queue)))==NULL)
{
free(q);
return NULL;
}
q->tail=0;
q->head=0;
return q;
}
//判斷隊列是否爲空
bool empty_queue(queue* q)
{
if(q->head==q->tail)
{
cout<<"empty_queue!"<<endl;
return true;
}
else
return false;
}
//判斷隊列是否滿了
bool full_queue(queue* q)
{
if(((q->tail+1)%MAXSIZE)==(q->head))
{
cout<<"full_queue!"<<endl;
return true;
}
else
return false;
}
//入隊
bool insert_queue(queue* q,int x)
{
q->data[q->tail]=x;
cout<<q->data[q->tail];
if(full_queue(q))
{
cout<<"隊列滿了,不能再入隊了!"<<endl;
return false;
}
else
{
q->tail=((q->tail+1)%MAXSIZE);
return true;
}
}
//出隊
int delete_queue(queue* q)
{
if(empty_queue(q))
{
cout<<"隊空,不能出隊!"<<endl;
return -1;
}
else
{
int x=q->data[q->head];
q->head=((q->head+1)%MAXSIZE);
return x;
}
}
int main(void)
{
//建空隊列
queue* q=creat_queue();
if(q)
cout<<"creat_queue is succeed!"<<endl;
else
cout<<"creat_queue is failed!"<<endl;
//入隊
for(int i=0;i<=5;i++)
{
if(!insert_queue(q,i))
break;
}
//出隊
delete_queue(q);
int x=delete_queue(q);
cout<<q->head<<,<<x<<endl;
free(q);
return 0;
}
隊列
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.