標籤:隊列 先進先出 資料結構 fifo
隊列是一種限定操作的線性表,它只能在表的一段插入,另外一段取出.
所以也稱為先進先出資料結構(FIFO---First In First Out)
C代碼如下(有小bug不想調了,作為參考即可):
#include<stdio.h>#define maxsize 5typedef 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<maxsize) { 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);}
結果圖:
轉載請註明作者:小劉