Queue implemented by static Array

Source: Internet
Author: User
/*_##################################### #######################################
_##
### Queue implemented by static Array
_ ## Author: xwlee
_ ## Time: 2006.12.31
### Chang'an University
_ ## Development condition: win2003 SERVER + vc6.0
_##
_ ## Static_array.cpp File
_####################################### ###################################*/
# Include "Stack. H"
# Include <stdio. h>

# Define queue_size 5 // maximum number of elements in the queue
// # Define array_size (queue_size) // The length of the array
# Define array_size (queue_size + 1) // array length (method 2)

// It is used to store arrays of queue elements and pointers to queue headers and tails.
Static queue_type queue [array_size];
Static size_t front = 1;
Static size_t rear = 0;
Static size_t qnumber = 0; // introduce a new variable to record the number of elements in the queue (method 1)

// Create_stack Function
Int create_stack (size_t size)
{
Return 1;
}

// Destroy_stack Function
Int destroy_stack (void)
{
Return 1;
}

// Insert Function
Void myinsert (queue_type value)
{
If (is_full ())
{
Printf ("queue is already full, insert is false./N ");
Exit (0 );
}
Rear = (Rear + 1) % array_size;
Queue [rear] = value;
Printf ("Rear = % d", rear );
Qnumber ++; // introduce new variables. (method 1)
}

// Delete function
Void mydelete (void)
{
If (is_empty () // If the queue is empty, the condition is true.
{
Printf ("queue already empty, delete is false./N ");
Exit (0 );
}
Front = (front + 1) % array_size;
Printf ("front = % d", front );
Qnumber --; // introduce new variables. (method 1)
}

// First Function
Queue_type first (void)
{
If (is_empty () // If the queue is empty, the condition is true.
{
Printf ("queue already empty./N ");
Exit (0 );
}
Return queue [Front];
}

// Is_empty Function
Int is_empty (void)
{
Return (Rear + 1) % array_size = front;
// Return qnumber = 0; // introduce new variables.
}

// Is_full Function
Int is_full (void)
{
Return (Rear + 2) % array_size = front;
// Return qnumber = array_size; // introduce new variables.
}

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.