呵呵,你也在看"数据结构"吗?刚好以前初学时也写了个,队列实现,又臭又长,勿笑.
/*////////////////Queue.h///////////////*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
typedef int ElemType;
typedef struct Queue
{
ElemType data[SIZE];
int front,rear;
}SeQueue;
void InitQueue(SeQueue **q)
{
*q=(SeQueue *) malloc (sizeof(SeQueue));
(*q)->front=0;
(*q)->rear=0;
}
int QueueEmpty(SeQueue *q)
{
return(q->front==q->rear);
}
int QueueFull(SeQueue *q)
{
return((q->rear+1)%SIZE==q->front);
}
void EnQueue(SeQueue *q,ElemType e)
{
if(QueueFull(q))
printf("Queue is Full!\n ");
else
{
q->data[q->rear]=e;
q->rear=(q->rear+1)%SIZE;
}
}
void DeQueue(SeQueue *q,ElemType &e)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
e=q->data[q->front];
q->front=(q->front+1)%SIZE;
}
}
void GetTop(SeQueue *q,ElemType &e)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
e=q->data[q->front];
}
}
void GetLen(SeQueue *q,int &l)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
int tmp=q->front;
for(;tmp!=q->rear;tmp++)
{
l++;
}
}
}
void display(SeQueue *q)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
int tmp=q->front;
printf("\n------------------\n");
for(;tmp!=q->rear-1;tmp++)
{
printf("%d ",q->data[tmp]);
}
printf("\n------------------\n");
}
}
/*////////////////yanghui.c///////////////*/
#include "Queue.h"
int main()
{
int s,e;
SeQueue *my;
InitQueue(&my);
EnQueue(my,0);
EnQueue(my,1);
EnQueue(my,0);
int n,i;
for(n=0;n<10;n++)
{
for(i=1;i<=10-n;i++)
printf("%2c",'
');
do{
DeQueue(my,s);
GetTop(my,e);
if(!e) printf("\n");
else printf("%3d ",e);
EnQueue(my,s+e);
}while(e!=0);
EnQueue(my,0);
}
display(my);
return 0;
}
[[italic] 本帖最后由 iaai315313 于 2007-12-21 14:04 编辑 [/italic]]