Using static arrays in C

Source: Internet
Author: User

Using static arrays in C

A queue is an advanced, FIFO data structure. We can also use arrays, linked lists, and so on. We can insert elements at the end of the queue and retrieve the elements in the queue header. Because the space utilization of common queues is not high, we generally use cyclic queues. The two most important operations in the cyclic queue are to determine whether it is null or full. When head = tail, the queue is empty. When (tail + 1) % MAX_SIZE = head, the queue is full.

How can I determine whether a team is full: sacrifice one unit to distinguish whether the team is full or not, and use less than one queue unit to join the team, which is equivalent to a waste of storage space. "The next position of the team's head pointer at the end of the Team serves as a full mark of the team ". Code upload to: https://github.com/chenyufeng1991/Queue_Array.

(1) queue

 

// Enter the queue void EnQueue (int value) {// first determine whether the queue is full if (tail + 1) % QUEUE_SIZE = head) {printf ("the queue is full, element \ n ") cannot be inserted from the end of the team;} else {queue [tail] = value; tail = (tail + 1) % QUEUE_SIZE ;}}

(2) outbound queue

 

 

// Output queue int DeQueue () {int temp; if (tail = head) {printf ("the queue is empty, and the element cannot output queue \ n ");} else {temp = queue [head]; head = (head + 1) % QUEUE_SIZE;} printf ("% d \ n", temp); return temp ;}

(3) Determine whether the queue is empty

 

 

// Determine whether the queue is empty int IsEmpty () {if (head = tail) {printf ("queue is empty \ n"); return 1 ;} printf ("the queue is not empty \ n"); return 0 ;}

(4) Determine whether the queue is full

 

 

// Determine whether the queue is full/*** how can I determine whether the queue is full here: Sacrifice a unit to distinguish between the empty queue and the full queue, and use less than one queue unit when entering the queue. If the Size of the array is Size, only (Size-1) elements can be stored. (This is a commonly used method for determining full) **/int IsFull () {if (tail + 1) % QUEUE_SIZE = head) {printf ("queue full \ n"); return 1;} printf ("queue below \ n"); return 0 ;}

(5) print queue Elements

 

 

// Print out the 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();DeQueue();    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.