链队列的建立、入队、出队、判空。
#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxSize 100
//队结点类型定义
typedef struct QNode
{
int data;
struct QNode *next;
} QNode;
//链队类型定义
typedef struct
{
QNode *front;
QNode *rear;
} LiQueue;
void initQueue(LiQueue *&lqu)
{
lqu=(LiQueue*)malloc(sizeof(LiQueue));
lqu->front=lqu->rear=NULL;
}
int isEmpty(LiQueue *lqu)
{
if(lqu->front==NULL||lqu->rear==NULL)
cout<<"is empty."<<endl;
else
cout<<"is not empty."<<endl;
}
void enQueue(LiQueue *lqu,int x)
{
QNode *p;
p=(QNode*)malloc(sizeof(QNode));
p->data=x;
p->next=NULL;
if(lqu->rear==NULL)
lqu->front=lqu->rear=p;
else
{
lqu->rear->next=p;
lqu->rear=p;
}
}
int deQueue(LiQueue *lqu,int x)
{
QNode *p;
if(lqu->rear==NULL)
return 0;
else
p=lqu->front;
if(lqu->front==lqu->rear)
lqu->front=lqu->rear=NULL;
else
lqu->front=lqu->front->next;
x=p->data;
free(p);
return 1;
}
void print(LiQueue *lqu)
{
QNode *p;
p=lqu->front;
while(p->next!=NULL)
{
cout<<p->data;
p=p->next;
}
if(p->next==NULL)
cout<<p->data;
}
int main()
{
LiQueue *lqu;
int x;
initQueue(lqu);
isEmpty(lqu);
enQueue(lqu,1);
enQueue(lqu,2);
enQueue(lqu,3);
print(lqu);
cout<<endl;
isEmpty(lqu);
deQueue(lqu,x);
print(lqu);
cout<<endl;
}