链队列的基本实现操作,多思考用手画图思考辅助,思考多种情况
#include<iostream>
//队列的链式表达,队列多用链式表达.
using namespace std;
typedef struct quee
{
int age;
linkquee next;
}quee,*linkquee;
typedef struct
{
linkquee front;
linkquee rear;
}queelist;
void innitialquee(queelist *q)
{
q->front= q ->rear= new quee;
if (q->front)
{
cout << "队列初始化成功" << endl;
q->front = q->rear;
q->front->next = NULL;
return;
}
else
cout << "初始化失败" << endl;
}//链队列初始化
void insertquee(queelist* q, int t)
{
linkquee q1 = new quee;
if (!q1)
{
cout << "失败" << endl;
}
else
{
q1->age = t;
q1->next = NULL;
q->rear->next = q1;
q->rear = q1;
}
}//入链队列操作
void outquee(queelist* q)
{
if (q->front == q->rear)
{
cout << "队列为空" << endl;
return;
}
else
if (q->front->next == q->rear->next)
{
cout << "队列只有一个数据" << endl;
cout << q->front->next->age << endl;
q->rear->next = q->front->next=NULL;
return;
}
else
{
cout << q->front->next->age << endl;
linkquee p = q->front->next;
q->front = p->next;
free(p);
}
}//出链队的操作
void deletequee(queelist* q)
{
while (q->front != NULL)
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
}//队列的销毁
int main()
{
queelist* q;
innitialquee(q);
insertquee(q, 3);
outquee(q);
deletequee(q);
return 0;
}