Use arrays to implement Queue (c)

Source: Internet
Author: User

[Html]
# Include <stdio. h>
# Include <stdlib. h>
 
Struct QueueRecord;
Typedef struct QueueRecord * Queue;
 
# Define MinQueueSize (5)
 
Struct QueueRecord
{
Int Capacity;
Int Front;
Int Rear;
Int Size;
Int * Array;
};
 
/*
* Create a queue.
*/
Queue CreateQueue (int capacity)
{
Queue queue;
Queue = (Queue) malloc (sizeof (Queue ));
If (queue = NULL)
{
Printf ("out of space !!! \ N ");
}
Queue-> Array = (int *) malloc (capacity * sizeof (int ));
If (queue-> Array = NULL)
{
Printf ("out of space !!! \ N ");
}
Queue-> Size = 0;
Queue-> Rear =-1;
Queue-> Front = 0;

Return queue;
}
 
 
 
/*
* If full, return 0; else return 1;
*/
Int IsFull (Queue queue, int capacity)
{
Return (queue-> Size <capacity );
}
 
/* If null return 1. else 0 */
Int IsEmpty (Queue queue)
{
Return (queue-> Size = 0 );
}
 
/*
* Print the queue.
*/
Int ShowQueue (Queue queue, int capacity)
{
Int I;

Printf ("queue-> Front = % d \ n", queue-> Front );
Printf ("queue-> Rear = % d \ n", queue-> Rear );
If (! IsEmpty (queue ))
{
For (I = queue-> Front; I <= queue-> Rear; I ++)
{
If (I <queue-> Rear)
{
Printf ("% d,", queue-> Array [I]);
}
Else
{
Printf ("% d", queue-> Array [I]);
}
 
}
}
Else
{
Printf ("oh, sorry. it is empty .");
}
 
}
/* Enter the Team */
Void Enqueue (Queue queue, int a, int capacity)
{
Queue-> Rear ++;
Queue-> Array [queue-> Rear] =;
Queue-> Size ++;
 
If (IsFull (queue, capacity ))
{
If (queue-> Rear = capacity)
{
Queue-> Rear = 0;
}

}

}
 
/* Team out */
Void Dequeue (Queue queue, int capacity)
{
Queue-> Front ++;
Queue-> Size --;

If (IsFull (queue, capacity ))
{
If (queue-> Front = capacity)
{
Queue-> Front = 0;
}

}
}
 
Int main (void)
{
Queue queue;
Int capacity = 10;
If (capacity <MinQueueSize)
{
Printf ("it is too small. \ n ");
}

Queue = CreateQueue (capacity );
Enqueue (queue, 1, capacity );
Enqueue (queue, 2, capacity );
Enqueue (queue, 3, capacity );
Enqueue (queue, 4, capacity );
Enqueue (queue, 5, capacity );
Enqueue (queue, 6, capacity );
Dequeue (queue, capacity );
Enqueue (queue, 12, capacity );
// Enqueue (queue, 3, capacity );
ShowQueue (queue, capacity );

}



From angelbosj's column

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.