/*_############################################################################
_##
_## a queue implemented by a dynamic array
_## Author:xwlee
_## time:2006.12.31
_## Chang ' an University
_## Development condition:win2003 server+vc6.0
_##
_## dynamic_array.cpp File
_##########################################################################*/
#include "Stack.h"
#include <stdio.h>
Pointers for storing queue elements and pointers to queue headers and tails
Static Queue_type *queue;
Static size_t queue_size; The size of the queue
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_queue function
int Create_queue (size_t size)
{
if (queue_size! = 0)
{
printf ("Create queue is False,try again./n");
Exit (0);
}
queue_size = size;
Queue = (Queue_type *) malloc ((queue_size+1) * sizeof (queue_type)); Method 2
Queue = (Queue_type *) malloc (queue_size * sizeof (queue_type)); Method 1
if (queue = = NULL)
{
printf ("malloc is False,try again./n");
return 0;
}
return 1;
}
Destroy_queue function
int destroy_queue (void)
{
if (queue_size = = 0)
{
printf ("Destroy queue is false./n");
Exit (0);
}
queue_size = 0;
Free (queue);
queue = NULL;
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)% (queue_size+1); Method 2
Rear = (rear + 1)% (queue_size); Method 1
queue[rear] = value;
printf ("rear=%d", rear);
qnumber++; Introduce new variables to use. (Method 1)
}
Delete function
void Mydelete (void)
{
if (Is_empty ())//If the queue is empty, the condition is set.
{
printf ("Queue already empty, delete is false./n");
Exit (0);
}
Front = (front + 1)% (queue_size+1); Method 2
Front = (front + 1)% (queue_size); Method 1
printf ("front=%d", front);
qnumber--; Introduce new variables to use. (Method 1)
}
First function
Queue_type First (void)
{
if (Is_empty ())//If the queue is empty, the condition is set.
{
printf ("Queue already empty./n");
Exit (0);
}
return queue[Front];
}
Is_empty function
int is_empty (void)
{
Return (rear +1)% (queue_size+1) = = Front; Method 2
return QNumber = = 0; Introduce new variables to use. Method 1
}
Is_full function
int is_full (void)
{
Return (rear +2)% (queue_size+1) = = Front; Method 2
return QNumber = = (queue_size); Introduce new variables to use. Method 1
}
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