C language implementation uses a static array to implement the loop queue

Source: Internet
Author: User

Queues are a first-in, first-out data structure that can be implemented using arrays, lists, and so on. We can insert elements at the end of the queue and take the elements out of the queue's head. Normal queues are generally used in circular queues because of the low utilization of space. The most important two actions in a loop queue are whether it is empty and full. When Head==tail, indicates that the queue is empty. When the (tail+1)%max_size = = Head indicates that the queue is full.

I judge the way the team is full: sacrificing a unit to distinguish between the empty and the full, the team less to use a queue unit, equivalent to wasting a storage space. "Team head pointer's next position as the team's full flag". Code uploaded to: Https://github.com/chenyufeng1991/Queue_Array.

(1) into the queue

Enter queue
void EnQueue (int value) {

    //To first determine if the queue is full
    if ((tail + 1)% Queue_size = head) {
        printf ("Queue is full, Cannot insert element \ n "from end of team);
    } else{

        Queue[tail] = value;
        Tail = (tail + 1)% queue_size
    }
}

(2) Out of the queue

Out Queue
int dequeue () {

    int temp;
    if (tail = = head) {
        printf ("Queue is empty, elements cannot be queued \ n");
    } else{

        temp = Queue[head];
        Head = (head + 1)% Queue_size;
    }

    printf ("%d\n", temp);
    return temp;
}

(3) To determine whether the queue is empty

Determines whether the queue is empty
int IsEmpty () {
    if (head = = tail) {
        printf ("Queue is empty \ n");
        return 1;
    }

    printf ("queue is not empty \ n");
    return 0;
}

(4) Determine if the queue is full

To determine if the queue is full
/**
 *  I'm here to judge the way the team is full:
 sacrifice a unit to distinguish between team space and full team, a queue unit is used less. If the size of the array is sized, then only (Size-1) elements are actually stored. (This is a more commonly used method) */
int isfull () {if (

    tail + 1)% Queue_size = = head) {
        printf ("queue full \ \ n");
        return 1;
    }

    printf ("queue is not full \ n");
    return 0;
}

(5) Print queue elements

Print out queue element
void PrintQueue () {

    for (int i = head; i < tail; i++) {
        printf ("%d", Queue[i]);
    printf ("\ n");
}

(6) Test code

int main (int argc, const char * argv[]) {

    EnQueue (4); EnQueue (1); EnQueue (2); EnQueue (9); EnQueue (8);
    PrintQueue ();

    Dequeue ();D equeue ();
    PrintQueue ();
    
    return 0;
}


Related Article

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.