/*
function:queue
created by : xilong
date: 2017.2.9
*/#include"iostream"
using namespace std;
#define OK 1#define ERROR 0#defineTRUE1#defineFALSE0
typedef int Elemtype;
typedef int Status;
typedef struct QNode
{
Elemtype data;
struct QNode* next;
} QNode, *QueuePrt;
typedef struct
{
QueuePrt front, rear;
} LinkQueue;
/*
initialize the queue
*/
Status Init_Queue(LinkQueue *q)
{
q->front = q->rear = (QueuePrt)malloc(sizeof(QNode));
if (!q->front)
exit(0);
q->front->next =NULL;
return OK;
}
/*
Insert an element into the queue, an element can only be inserted into the end of the queue
*/
Status Inster_Queue(LinkQueue *q, Elemtype e)
{
QueuePrt p;
p = (QueuePrt)malloc(sizeof(QNode));
if (p ==NULL)
exit(0);
p->data= e;
p->next =NULL;
q->rear->next = p;
q->rear = p;
return OK;
}
/*
Delete an element in the queue, an element which is in the front can only be deleted
*/
Status Delete_Queue(LinkQueue *q, Elemtype *e)
{
QueuePrt p;
if (q->front == q->rear)
{
cout <<"空队!"<< endl;
returnFALSE;
}
p = q->front->next;
q->front->next = p->next;
if (q->rear == p)
{
q->rear = q->front;
}
*e = p->data;
free(p);
returnTRUE;
}
/*
length of the queue
*/
int Length_Queue(LinkQueue *q)
{
QueuePrt p;
int len =0;
p = q->front->next;
if (q->front == q->rear)
cout <<"空队!"<< endl;
while (p)
{
len++;
p = p->next;
}
return len;
}
void main()
{
LinkQueue q;
Elemtype e;
int i, len;
Init_Queue(&q);
Inster_Queue(&q, 1);
Inster_Queue(&q, 2);
Inster_Queue(&q, 3);
Inster_Queue(&q, 4);
Inster_Queue(&q, 5);
Inster_Queue(&q, 6);
Inster_Queue(&q, 7);
Inster_Queue(&q, 8);
len = Length_Queue(&q);
cout <<"队列的长度为:";
cout << len << endl;
cout <<"出对列的顺序为:";
for (i =0; i < len; i++)
{
Delete_Queue(&q, &e);
cout << e <<" ";
}
cout << endl;
system("pause");
}