A queue is a linear table with limited operations. It can only be inserted in one segment of the table and retrieved in another segment.
This is also known as the first-in-first-out data structure (FIFO --- first in first out)
The C code is as follows (a small bug does not need to be called, as a reference ):
#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);}
Result chart:
Reprinted by Liu