队列是一种限定操作的线性表,它只能在表的一段插入,另外一段取出.

所以也称为先进先出数据结构(FIFO---First

In First Out)

C代码如下(有小bug不想调了,作为参考即可):

#include

#define maxsize 5

typedef int ElemType;

typedef struct queue

{

int head;

int tail;

ElemType Data[maxsize];

}Queue;

void InitQueue(Queue *Q)

{

Q->tail=0;

Q->head=0;

}

void EnQueue(Queue *Q)

{

int value;

int i;

printf("Input Queue Value:\n");

scanf("%d",&value);

Q->head++;

int len=Q->head-Q->tail;

if(len

{

for(i=len-1;i>=0;i--)

Q->Data[i+1]=Q->Data[i];

}

Q->Data[Q->tail]=value;

printf("\n");

}

void DeQueue(Queue *Q)

{

int len=Q->head-Q->tail-1;

Q->head=Q->head-1;

if(len<=maxsize)

{

printf("Out put Value:\n");

printf("%d ",Q->Data[len]);

}

printf("\n");

}

void IsEmpty(Queue *Q)

{

if(Q->head==0&&Q->tail==0)

printf("Queue is empty.\n");

else

printf("Queue is not empet.\n ");

printf("\n");

}

void IsFull(Queue *Q)

{

if(Q->head-Q->tail>=maxsize)

printf("Queue is Full.\n");

else

printf("Queue is not Full.\n");

printf("\n");

}

void main()

{

Queue Q;

InitQueue(&Q);

EnQueue(&Q);

EnQueue(&Q);

EnQueue(&Q);

EnQueue(&Q);

EnQueue(&Q);

IsEmpty(&Q);

IsFull(&Q);

DeQueue(&Q);

DeQueue(&Q);

DeQueue(&Q);

DeQueue(&Q);

DeQueue(&Q);

IsEmpty(&Q);

IsFull(&Q);

}

结果图:

73ee01c6c41b3e3ae23bc2a0f160d53e.bmp

转载请注明作者:小刘

原文:http://blog.csdn.net/u013018721/article/details/38268603

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐