![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//队列的表示和实现
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
struct Qnode//队列的数据结构
{
int data;
struct Qnode *next;
};
struct LinkQueue
{
struct Qnode *front;//队头指针
struct Qnode *rear;//队尾指针
};
int InitQueue(struct LinkQueue *Q)//队列的初始化
{
Q->front=Q->rear=(struct Qnode *)malloc(sizeof(Qnode));
if(!Q->front)
exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}
int QueueEmpty(struct LinkQueue *Q)//若队列为空,返回TRUE,否则返回FALSE
{
if(Q->front==Q->rear)
return TRUE;
else
return FALSE;
}
int DestroyQueue(struct LinkQueue *Q)//销毁队列
{
while(Q->front){
Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear;
}
return OK;
}
int EnQueue(struct LinkQueue *Q,int e)//插入元素e为Q的新的队尾元素
{
struct Qnode *p;
p=(Qnode *)malloc(sizeof(Qnode));
if(!p)
exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}
int DelQueue(struct LinkQueue *Q,int *e)//若队列不为空,则删除Q的队头元素,并用e返回其值,并返回OK
{
struct Qnode *p;
if(Q->front==Q->rear)
return ERROR;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
return OK;
}
void Queue()
{
LinkQueue Q;
int N;
int e;
InitQueue(&Q);
scanf("%d",&N);
while(N)
{
EnQueue(&Q,N%8);
N=N/8;
}
while(!QueueEmpty(&Q))
{
DelQueue(&Q,&e);
printf("%d",e);
}
}//Queue
int main()
{
printf("Input:\n");
Queue();
printf("\n");
return OK;
}
![](https://www.cnblogs.com/Emoticons/tusiji/203330130.gif)
![](https://www.cnblogs.com/Emoticons/tusiji/203330130.gif)
![](https://www.cnblogs.com/Emoticons/tusiji/203330130.gif)