指针数组队列



掌叔
2008-06-17 09:43:19

摘自:[url]http://etual.cublog.cn[/url]

根普通的队列结构一样,不过数组的类型是指针数组
用来处理字符串用的。

/*********************************************
Array Queue Exercises
Etual
2008-1-14
**********************************************/
#include
#define MAXSIZE 4
typedef struct Queue_tag{
char *data[MAXSIZE];
int front,rear;
}QUEUE;
//global var
QUEUE Queue;
// queue initialize
void InitQueue(QUEUE* Queue)
{
int i;
for (i=0;i {
Queue->data[i]=NULL;
}
Queue->front=Queue->rear=0;
}

// whether queue is empty
int EmptyQueue(QUEUE* Queue)
{
return (Queue->front==Queue->rear);
}
int FullQueue(QUEUE* Queue)
{
return (((Queue->front+1)%MAXSIZE)==Queue->rear);
}
void InQueue(QUEUE* Queue,char *str)
{
if (!FullQueue(Queue))
{
Queue->data[Queue->front]=str;
Queue->front=(Queue->front+1)%MAXSIZE;
}else{
printf("Queue is FULL ,can not input!
");
}
}
char *OutQueue(QUEUE* Queue)
{
char *temp;
if (!EmptyQueue(Queue))
{
temp=Queue->data[Queue->rear];
Queue->rear=(Queue->rear+1)%MAXSIZE;
}else{
temp=0;
printf("Queue is empty");
}
return temp;
}
void PrintQueue(QUEUE* Queue)
{
int front=Queue->front;
int rear =Queue->rear;
if (front == rear)
{
printf("Queue is empty!");
}
while(front != rear)
{
printf("%s",Queue->data[rear]);
rear=(rear+1)%MAXSIZE;
}
printf("
");
}
int main(void)
{
InitQueue(&Queue);
InQueue(&Queue,"Hello World
");
InQueue(&Queue,"I am Etual
");
InQueue(&Queue,"This is my test program!");
PrintQueue(&Queue);
return 1;
}