Data structure C language Implementation series--queue

Source: Internet
Author: User
Tags array length
#include < stdio.h >
#include < stdlib.h >

typedef int ELEMTYPE;
/* ********************************************************************** */
/* The following are 6 algorithms for queue sequential storage operations * *
/* ********************************************************************** */

struct queue{
Elemtype * queue; /* The array space to point to the storage queue * *
int front, rear, Len; /* Team first pointer (subscript), team tail pointer (subscript), Queue Length variable * *
int maxSize; /* Queue Array Length * *
};

void Againmalloc (struct queue * q)
{
/* Space expansion to the original twice times, the original content is automatically copied to the storage space that P is pointing to * *
Elemtype * p;
p = realloc (q-> queue, 2 * q-> maxSize * sizeof (elemtype));
/* Dynamic storage space allocation, if the failure to exit the operation * *
if (! P) {
printf ("Space allocation failed.") " );
Exit (1);
}
Q-> queue = p; /* Causes the queue to point to the new queue space * *
* * To move the tail content of the original queue back 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; /* Team tail pointer move maxsize position * *
}
Q-> maxSize = 2 * q-> maxSize; /* Change the queue space size to the new length * *
return;
}

/* 1. Initialization queue * *
void Initqueue (struct queue * q, int ms)
{
/* Check MS is valid, if invalid then quit running
if (MS <= 0) {
printf ("Ms value Illegal!");
Exit (1);
}
Q-> maxSize = ms; /* The size of the queue space is MS * *
/* Dynamic storage space allocation, if the failure to exit the operation * *
Q-> queue = malloc (MS * sizeof (ELEMTYPE));
if (! Q-> Queue) {
printf ("Memory space allocation failed.") " );
Exit (1);
}
Q-> front = q-> rear = 0; /* Initial queue is empty/*
return;
}

/* 2. Insert element x into the queue */
void EnQueue (struct queue * Q, Elemtype x)
{
/* Dynamic allocation when queue is full * *
if (q-> rear + 1)% q-> maxSize = = q-> front) {
Againmalloc (q);
}
Q-> rear = (q-> rear + 1)% q-> maxSize; * * Find the next position at the end of the team * *
Q-> queue[q-> rear] = x; /* Assign the value of X to the new team tail * *
return;
}

/* 3. Delete element from queue and return * *
Elemtype outqueue (struct queue * q)
{
/* If the queue is empty then terminate the operation * *
if (q-> front = = q-> rear) {
printf (the queue is empty and cannot be deleted.) " );
Exit (1);
}
Q-> front = (q-> front + 1)% q-> maxSize; /* Make the team head pointer to the next position.
return q-> queue[q-> Front]; /* Return to Team first element * *
}

* 4. Read the first element of the team, do not change the queue status * *
Elemtype peekqueue (struct queue * q)
{
/* If the queue is empty then terminate the operation * *
if (q-> front = = q-> rear) {
printf (the queue is empty and cannot be deleted.) " );
Exit (1);
}
return q-> queue[(q-> front + 1)% q-> maxSize]; /* Team first element is the element in the next position of the team's first pointer.
}

* 5. Check whether a queue is empty, if it returns 1, otherwise return 0 * * *
int emptyqueue (struct queue * q)
{
if (q-> front = = q-> rear) {
return 1;
} else {
return 0;
}
}

* 6. Clear a queue and release the dynamic storage space * *
void Clearqueue (struct queue * q)
{
if (q-> queue!= NULL) {
Free (q-> queue);
Q-> queue = NULL; /* Set queue space pointer to NULL */
Q-> front = q-> rear = 0; /* Set Queue to Empty * *
Q-> maxSize = 0; /* Set Queue size of 0 * *
}
return;
}

/* ********************************************************************** */

int main (int argc, char * argv[])
{
struct queue q;
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", Outqueue (& Q));
EnQueue (& Q, 68);
printf ("%d", Peekqueue (& Q)); printf ("%d", Outqueue (& Q));
while (! Emptyqueue (& Q)) {
printf ("%d", Outqueue (& Q));
}
printf ("");
Clearqueue (& Q);
System ("pause");
return 0;
}
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.