C language implementation of chain queue

Source: Internet
Author: User

#include "stdio.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20/* Storage space Initial allocation */

typedef int STATUS;

typedef int QELEMTYPE; /* The Qelemtype type is based on the actual situation and is assumed to be int */

typedef struct QNODE/* Node structure */
{
Qelemtype data;
struct Qnode *next;
}qnode,*queueptr;

typedef struct/* queue's Linked list structure */
{
Queueptr front,rear; /* team head, tail hands */
}linkqueue;

Status visit (qelemtype c)
{
printf ("%d", c);
return OK;
}

/* Construct an empty queue Q */
Status initqueue (Linkqueue *q)
{
Q->front=q->rear= (queueptr) malloc (sizeof (Qnode));
if (! Q->front)
Exit (OVERFLOW);
q->front->next=null;
return OK;
}

/* Destroy Queue Q */
Status destroyqueue (Linkqueue *q)
{
while (Q->front)
{
q->rear=q->front->next;
Free (Q->front);
q->front=q->rear;
}
return OK;
}

/* Clear Q to empty queue */
Status clearqueue (Linkqueue *q)
{
Queueptr p,q;
q->rear=q->front;
p=q->front->next;
q->front->next=null;
while (p)
{
Q=p;
p=p->next;
Free (q);
}
return OK;
}

/* If q is an empty queue, returns TRUE, otherwise false */
Status queueempty (Linkqueue Q)
{
if (q.front==q.rear)
return TRUE;
Else
return FALSE;
}

/* Ask for the length of the queue */
int Queuelength (Linkqueue Q)
{
int i=0;
Queueptr p;
P=q.front;
while (Q.REAR!=P)
{
i++;
p=p->next;
}
return i;
}

/* If the queue is not empty, use E to return the team header element of Q and return OK, otherwise return error */
Status GetHead (linkqueue q,qelemtype *e)
{
Queueptr p;
if (q.front==q.rear)
return ERROR;
p=q.front->next;
*e=p->data;
return OK;
}


/* Insert element e As Q's new team tail element */
Status EnQueue (Linkqueue *q,qelemtype e)
{
Queueptr s= (queueptr) malloc (sizeof (Qnode));
if (!s)/* Storage Allocation failed */
Exit (OVERFLOW);
s->data=e;
s->next=null;
q->rear->next=s;/* The new node s with element E assigned to the successor of the original team tail node, see figure ①*/
q->rear=s;/* sets the current s to the tail node of the team, rear points to s, see figure ②*/
return OK;
}

/* If the queue is not empty, delete the team header element of Q, return its value with E, and return to OK, otherwise return error */
Status DeQueue (linkqueue *q,qelemtype *e)
{
Queueptr p;
if (q->front==q->rear)
return ERROR;
p=q->front->next;/* The team head node you want to delete to p, see figure ①*/
*e=p->data;/* assigns the value of the team header node that you want to delete to E */
q->front->next=p->next;/* the successor P->next of the original team head node is assigned to the successor of the head node, see figure ②*/
if (q->rear==p)/* If the team head is the end of the team, then delete the rear point to the head node, see figure ③*/
q->rear=q->front;
Free (p);
return OK;
}

/* Output of each element in queue Q from the head to the end of the team */
Status Queuetraverse (Linkqueue Q)
{
Queueptr p;
p=q.front->next;
while (p)
{
Visit (P->data);
p=p->next;
}
printf ("\ n");
return OK;
}

int main ()
{
int i;
Qelemtype D;
Linkqueue Q;
I=initqueue (&Q);
if (i)
printf ("Successfully constructed an empty queue!\n");
printf ("is empty queue?") %d (1: Empty 0: No) ", Queueempty (q));
printf ("Queue Length is%d\n", Queuelength (q));
EnQueue (&q,-5);
EnQueue (&q,5);
EnQueue (&q,10);
printf ("Insert 3 elements ( -5,5,10), queue Length is%d\n", Queuelength (q));
printf ("is empty queue?") %d (1: Empty 0: No) ", Queueempty (q));
printf ("The elements of the queue are sequentially:");
Queuetraverse (q);
I=gethead (Q,&D);
if (I==ok)
printf ("Team head element is:%d\n", D);
DeQueue (&Q,&D);
printf ("deleted team head element%d\n", d);
I=gethead (Q,&D);
if (I==ok)
printf ("New team head element is:%d\n", D);
Clearqueue (&Q);
printf ("Empty queue, q.front=%u q.rear=%u q.front->next=%u\n", q.front,q.rear,q.front->next);
Destroyqueue (&Q);
printf ("Destroy queue, q.front=%u q.rear=%u\n", Q.front, Q.rear);

return 0;
}

C language implementation of chain queue

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.