C language queue (circular queue)

Linear structure: there is only one root node, and each node has a direct precursor and a maximum non-null data structure of a direct successor

Data structure does not satisfy the linear structure: nonlinear structure

queue

General queue into two categories: chained queues and the queues

         --- chain chain queue linked list queues implemented queue i.e.

         --- the queues queue array the queues is implemented, the queues must generally circular queue

1, the basic concepts:

  Queue means at one end to allow insertion, deletion linear form at the other end, known as "FIFO" in the linear form

  Queue is a special linear structure, which allows only the deletion in the header (head) of the queue, which is called dequeue

  The tail: allowing insertion end, with a tail pointer pointing to the position of the tail element

  First team: Allow one end removed, with the head pointer to the first element

Circular queue (the queues) to achieve:

#include <stdio.h>

#define LENGTH 11 / * define the maximum length of the array * /
#define OUT. 1 / dequeuing * * /
#define the GET 2 / enqueue * * /
struct Queue
{
    int Data [the LENGTH];
    int head; / * first team * /
    int tail; / * the tail * /
};
/ * achieve circular queue * /
main ()
{
    struct queue Q;
    int I;
    int KK = 0;
    / * initialize queue * /
    q.head q.tail = 0 = ; / * initialize the tail head of the queue * /
    / * insert sequence number 10 * /
    for (I = 0; I <the LENGTH -. 1; I ++)
    {
        scanf_s ( "% D", & q.data [q.tail]) ;
        q.tail = (+ q.tail. 1) the LENGTH%;
    }
    the while (. 1)
    {
        IF (q.head == q.tail)
        {
            printf ( "queue empty");
            BREAK;
        }
        printf ( "dequeue: 1 \ n enqueuing: 2 \ n-");
        scanf_s ( "% D", & KK);
        IF (KK == OUT)
        {
           
            Q. = head (+ q.head. 1) the LENGTH%;
        }
        the else IF (the GET == KK)
        {
            IF (q.head == (+ q.tail. 1) the LENGTH%)
            {
                the printf ( "queue full");
                BREAK ;
            }
            the printf ( "enter into the team elements: \ n-");
            scanf_s ( "% D", & q.data [q.tail]);
            q.tail = (+ q.tail. 1) the LENGTH%;
        }

        if (q.tail != q.head)
        {
            printf("队列中剩余元素:");
            for (i = q.head; ;)
            {
                printf("%d ", q.data[i]);
                i = (i + 1) % LENGTH;
                if(i == q.tail)
                {
                    putchar('\n');
                    break;
                }
            }
        }
    }
  68

    return 0;
}

2, why the queues must generally circular queue

Cyclic queue is the queue sequence for a limited maximum amount of memory a solution, where one queue (array) and then inserting a new element is not available but the actual space in the queue is not filled to a reasonable solution to the problem

3, the queues of the difference between ordinary

General order of the queue when you insert a new element (the team) is q.tail ++, delete the old elements (the team) when q.head ++ (q.tail and q.head are int type, but in order to facilitate the description here say pointer ), in the team and then want to insert a new element that is the last one and this time q.tail there will not fill the space actually available but you can not insert a new element of the problem at the end of the array, the array will cause forced into cross-border, We should not continue to expand the array of space, resulting in a waste of space

The core way is to increase the circular queue head pointer and the tail team pointer

q.head = (q.head + 1) % LENGTH;

q.tail = (q.tail + 1) % LENGTH;

This achieves the effect of a curved imaginary storage space, but also to solve the waste of space (recommended paper simulation loop array dequeue the queue enqueue operation, to be understood that the above two lines of code)

It is noteworthy that

q.head points to the team first element

q.tail points to the tail element of a post, a waste of space, such as an array q.data [11] After the insertion element 10 is directed q.tail q.data [10] i.e. q.tail = 10;

4, new issues circular queue

When the queue is empty q.head = q.tail, when the queue is full there q.head = q.tail caused judgment queue is empty or full of ambiguity

Solution: 1. Add a parameter, so as to delete the element 1, element is inserted into the 0

                     2. Add one parameter, i.e., recording the number of elements in the queue length

          3. an empty cell, so that if (q.head == (q.tail + 1)% LENGTH) conditions queue is full, in order to resolve ambiguities

5, pay attention

Circular queue head pointer and tail pointer reduced or increased manner

The queue is full or empty condition determination

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159767.htm