QUEUE -- QUEUE (procedure)

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>
# Include "queue. h"

Int main ()
{
Int I;
Type x;
Type arr [] = {3, 1, 2, 5, 7, 9 };
QUEUE * q = NULL;

Q = CreateQueue (10 );
If (NULL = q)
Return-1;
 
For (I = 0; I <sizeof (arr)/sizeof (* arr); I ++)
{
EnQueue (q, arr + I );
}
FrontQueue (q, & x );
Printf ("x = % d \ n", x );

DisptoryQueue (q );
Return 0;
}
---------------------------------------------------------------

# Ifndef _ QUEUE_H __
# Define _ QUEUE_H __

Struct node;
Typedef int Type;
Typedef struct node QUEUE;

QUEUE * CreateQueue (int );
Void QueueMakeEmpty (QUEUE *);
Int QueueIsEmpty (QUEUE *);
Int QueueIsFull (QUEUE *);
Int EnQueue (QUEUE *, const Type *);
Int DeQueue (QUEUE *);
Int FrontQueue (QUEUE *, Type *);
Int FrontAndDeQueue (QUEUE *, Type *);
Void DisptoryQueue (QUEUE *);

Struct node {
Type * data;
Int capacity;
Int front;
Int rear;
Int size;
};

# Endif
-------------------------------------------------------------------

# Include <stdio. h>
# Include <stdlib. h>
# Include "queue. h"

QUEUE * CreateQueue (int size)
{
QUEUE * q = malloc (sizeof (* q ));
If (NULL = q)
Return NULL;
Q-> data = malloc (sizeof (Type) * size); // queue length, number of queue members
If (NULL = q-> data)
{
Free (q );
Return NULL;
}
Q-> capacity = size; // queue capacity
QueueMakeEmpty (q );
Return q;
}
Void QueueMakeEmpty (QUEUE * q)
{
Q-> size = 0;
Q-> front = 1;
Q-> rear = 0;
}
Int QueueIsEmpty (QUEUE * q)
{
Return q-> size = 0;
}
Int QueueIsFull (QUEUE * q)
{
Return q-> size = q-> capacity;
}
Static int repeat (QUEUE * q, int rear) // enter the end of the QUEUE,
{
If (++ rear = q-> capacity)
Rear = 0;
Return rear;
}
Int EnQueue (QUEUE * q, const Type * x)
{
If (QueueIsFull (q ))
Return-1;
Q-> rear = repeat (q, q-> rear); // set the rear at the end of the team to 0 after each successful team entry.
Q-> data [q-> rear] = * x;
Q-> size ++;
Return 0;
}
Int DeQueue (QUEUE * q) // leaves the QUEUE
{
If (QueueIsEmpty (q ))
Return-1;
Q-> front = repeat (q, q-> front );
Q-> size --;
Return 0;
}
Int FrontQueue (QUEUE * q, Type * x) // view the first line
{
If (QueueIsEmpty (q ))
Return-1;
* X = q-> data [q-> front];
Return 0;
}
Int FrontAndDeQueue (QUEUE * q, Type * x) // view the first and outgoing queues
{
If (FrontQueue (q, x) = 0)
Return DeQueue (q );
Return-1;
}
Void DisptroyQueue (QUEUE * q)
{
Free (q-> data );
Free (q );
}

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.