Data Structure-queue-C Array Implementation

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.