//队列的顺序实现
//线性表 先进先出
#include<iostream>
using namespace std;
#define MaxSize 100
typedef struct LinkNode
{
char data;
struct LinkNode *next;
}LinkNode,*QueuePtr;
typedef struct{
QueuePtr front,rear;
}LinkQueue;
//初始化
void InitQueue(LinkQueue&Q){
Q.front=Q.rear=new LinkNode;
Q.front->next=NULL;
}
//判断是否为空
bool QueueEmpty(LinkQueue &Q,char e)
{
if(Q.rear==Q.front)
return true;
else return false;
}
//入队
bool EnQueue(LinkQueue &Q,char e)
{
LinkNode *p;
p=new LinkNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return true;
}
//出队
char DeQUeue(LinkQueue &Q)
{
char e;
LinkNode *p;
if(Q.rear==Q.front)
return -1;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
delete p;
return e;
}
//获取队头元素的值
char GetHead(LinkQueue &Q)
{
if (Q.rear == Q.front)
return -1;
return Q.front->next->data;
}
//置空
void zhi(LinkQueue &Q)
{
LinkNode *p,*q;
p=Q.front->next;
while(p)
{
q=p->next;
delete p;
p=q;
}
Q.rear=Q.front;
}
//销毁
void DestroyQueue(LinkQueue &Q)
{
LinkNode *p;
while(Q.front)
{
p=Q.front->next;
delete Q.front;
Q.front=p;
}
}
int main(){
LinkQueue l;
InitQueue(l);
for(int i=0;i<5;i++)
{
char a;
cin>>a;
EnQueue(l,a);
}
cout<<GetHead(l)<<endl;
for(int i=0;i<5;i++)
cout<<DeQUeue(l)<<" ";
zhi(l);
DestroyQueue(l);
return 0;
}
标签:return,链队,LinkQueue,c++,next,front,LinkNode,rear From: https://blog.csdn.net/2301_79727388/article/details/142989689