C++实现链队
代码如下:
#include <iostream>
using namespace std;
typedef struct QNode
{
int data;
struct QNode *next;
}qnode;
typedef struct LIqueue
{
struct QNode *front;
struct QNode *rear;
}liqueue;
void initqueue(liqueue *&C)//初始化
{
C=(liqueue*)malloc(sizeof(liqueue));
C->front=C->rear=NULL;
}
int isempty(liqueue *C)//判空
{
if(C->rear==NULL||C->front==NULL)
{return 1;}
else
{return 0;}
}
void enterqueue(liqueue *&C,int x)//进队
{
struct QNode *S;
S=(QNode*)malloc(sizeof(QNode));
S->data=x;
S->next=NULL;
if(C->rear==NULL)
{C->front=C->rear=S;}
else
{
C->rear->next=S;
C->rear=S;
}
}
int deletequeue(liqueue *&C,int &e)//出队
{
struct QNode *S;
if(C->rear==NULL)
{return 0;}
else
{S=C->front;}
if(C->front==C->rear)
{C->front=C->rear=NULL;}
else
{C->front=C->front->next;}
e=S->data;
free(S);
return 1;
}
int main()
{
struct LIqueue *C;
int x;int e;
initqueue(C);
if(isempty(C)==1)
{cout<<"链队为空"<<endl;}
else
{cout<<"链队不为空"<<endl;}
cout<<"输入进队的元素:";
cin>>x;
enterqueue(C,x);
if(isempty(C)==1)
{cout<<"链队为空"<<endl;}
else
{cout<<"链队不为空"<<endl;}
deletequeue(C,e);
cout<<"出队元素为:"<<e<<endl;
if(isempty(C)==1)
{cout<<"链队为空"<<endl;}
else
{cout<<"链队不为空"<<endl;}
return 0;
}
运行结果: