Data Structure-C language description of the queue

Source: Internet
Author: User

// The C language description of the queue, which is selected from data structure (C language description), Xu xiaokai, He Guiying, and Tsinghua University Press.

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

Typedef int elemtype;

// The ordered storage structure of the queue can also be defined in a record type. If the record type is represented by queuesq, it is defined:
/*
Struct queuesq {
Elemtype queue [maxsize];
Int front, rear, Len;
};
*/
// To dynamically allocate the array space of the storage queue, the queuesq structure type can be defined:
Struct queuesq {
Elemtype * queue;/* point to the array space of the storage queue */
Int front, rear, Len;/* head pointer, end pointer, queue length variable */
Int maxsize;/* length of the queue array */
};

Void againmalloc (struct queuesq * q)
{
/* The original space is expanded to 2 times and directed by P. The original content is automatically
Copy to the bucket pointed to by p */
Elemtype * P;
P = realloc (Q-> queue, 2 * q-> maxsize * sizeof (elemtype ));
If (! P) {/* failed to allocate and quit running */
Printf ("storage space used up! \ N ");
Exit (1 );
}
/* Point the queue to the new queue space */
Q-> queue = P;
/* Move the tail content of the original queue to the maxsize position */
If (Q-> rear! = Q-> MaxSize-1 ){
Int I;
For (I = 0; I <= Q-> rear; I ++)
Q-> queue [I + q-> maxsize] = Q-> queue [I];
Q-> rear + = Q-> maxsize;/* move the team's tail pointer to the maxsize position */
}
/* Change the queue space size to a new length */
Q-> maxsize = 2 * q-> maxsize;
}

// 1. initialize the queue
// The first case is to implicitly allocate a fixed-size dynamic storage space for the storage queue. It is assumed that the size of the dynamic storage space is 10.
/*
Void initqueue (struct queuesq * q)
{
// Set the maximum initial length of the queue space to 10
Q-> maxsize = 10;
// Dynamic storage space allocation
Q-> queue = malloc (10 * sizeof (elemtype ));
// Leave the queue empty
Q-> front = Q-> rear = 0;
}
*/

// The second case is to allocate a dynamic bucket of the size specified by the parameter. Any initialization algorithm can be used.
Void initqueue (struct queuesq * q, int MS)
{
/* Check whether Ms is valid. If not, exit the operation */
If (Ms <= 0) {printf ("the MS value is invalid! \ N "); exit (1 );}
/* Set the queue space to Ms */
Q-> maxsize = MS;
/* Dynamic storage space allocation. If the allocation fails, the system exits */
Q-> queue = malloc (MS * sizeof (elemtype ));
If (! Q-> Queue ){
Printf ("memory space used up! \ N ");
Exit (1 );
}
/* Initially set the queue to null */
Q-> front = Q-> rear = 0;
}

// 2. Insert an element into the queue
Void enqueue (struct queuesq * q, elemtype X)
{
/* Dynamic redistribution when the queue is full */
If (Q-> rear + 1) % Q-> maxsize = Q-> front) againmalloc (Q );
/* Find the next position at the end of the Team */
Q-> rear = (Q-> rear + 1) % Q-> maxsize;
/* Assign the item value to the new team end position */
Q-> queue [q-> rear] = X;
} // The specific definition of the againmalloc () algorithm is as follows:


// 3. Delete the element from the queue and return
Elemtype outqueue (struct queuesq * q)
{
/* Terminate the operation if the queue is empty */
If (Q-> front = Q-> rear ){
Printf ("the queue is empty and cannot be deleted! \ N ");
Exit (1 );
}
/* Point the first pointer of the team to the next position */
Q-> front = (Q-> front + 1) % Q-> maxsize;
/* Return the first element of the Team */
Return Q-> queue [q-> front];
}

// 4. Read the first element of the queue without changing the queue status
Elemtype peekqueue (struct queuesq * q)
{
/* Exit if the queue is empty */
/* Terminate the operation if the queue is empty */
If (Q-> front = Q-> rear ){
Printf ("the queue is empty and cannot be read! \ N ");
Exit (1 );
}
/* The first element is the element in the next position of the queue pointer */
Return Q-> queue [(Q-> front + 1) % Q-> maxsize];
}

// 5. check whether a queue is empty. If yes, 1 is returned; otherwise, 0 is returned.
Int emptyqueue (struct queuesq * q)
{
If (Q-> front = Q-> rear) return 1; else return 0;
}

// 6. Clear a queue and release the dynamic storage space
Void clearqueue (struct queuesq * q)
{
If (Q-> queue! = NULL ){
Free (Q-> Queue );
Q-> queue = 0;
Q-> front = 0;
Q-> maxsize = 0;
}
}

Void main ()
{
Struct queuesq;
Int A [8] = {3, 8, 5, 17, 9, 30, 15, 22 };
Int I;
Initqueue (& Q, 5 );
For (I = 0; I <8; I ++) enqueue (& Q, a [I]);
Printf ("% d", outqueue (& Q); printf ("% d \ n", outqueue (& Q ));
While (! Emptyqueue (& Q) printf ("% d", outqueue (& Q ));
Printf ("\ n ");
Clearqueue (& Q );
}

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.