Data structure Queue _ data structure

Source: Internet
Author: User

Queue characteristics: Advanced First Out (FIFO)--The elements of the advanced queue are first out of the queue. Come from the queues in our Lives (queue first and do it first).

The queue has the following actions: Initqueue ()--Initializing the queue EnQueue ()--Enter Queue dequeue ()--out Queue isqueueempty ()--to determine whether the queue is empty isqueuefull ()- -Determine if the queue is full

Queues can be implemented as queue operations (C language) by array and list, with the following arrays as an example:

Array implementation:

Queue data structure

typedef struct Queue
{
        int queuesize;   The size of the array
        int head, tail;  The head and tail subscript int *q of the queue
        ;          Array head pointer
}queue;

Initqueue ()--initializing queues

void Initqueue (Queue *q)
{
        q->queuesize = 8;
        Q->Q = (int *) malloc (sizeof (int) * q->queuesize); allocating memory
        q->tail = 0;
        Q->head = 0;
}

This has a flaw, the space utilization is not high. Use loop queue:

EnQueue ()--Enter queue

void EnQueue (Queue *q, int key)
{
        int tail = (q->tail+1)% q->queuesize;//remainder guarantee, when quil=queuesize-1, then back to 0
  if (tail = = Q->head)                   //At this time the queue does not have space
        {
            printf ("The queue has been filled");
        }
        else
        {
            Q->q[q->tail] = key;
            Q->tail = tail;
        }

Dequeue ()--out queue

int dequeue (Queue *q)
{
        int tmp;
        if (Q->tail = = Q->head)     //judge the queue is not empty
        {
            printf ("The queue is null\n");
        }
        else
        {
            tmp = q->q[q->head];
            Q->head = (q->head+1)% q->queuesize;
        }
        return tmp;
}

Isqueueempty ()--Determines whether the queue is empty

int Isqueueempty (Queue *q)
{
        if (Q->head = = Q->tail)
        {return
            1;
        }
        else
        {return
            0;
        }
}

Isqueuefull ()--Determine if the queue is full

int Isqueuefull (Queue *q)
{
    if (q->tail+1)% q->queuesize = = Q->head)
    {return
        1;
    }
    Else
    {return
        0;
    }
}

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.