Data structure-a single-chain queue-related operation algorithm

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>

#define OVERFLOW-2
#define OK 1
#define ERROR 0

typedef int QELEMTYPE;

Single-stranded queue structure definition
typedef struct QNODE {
Qelemtype data;
struct Qnode *next;
}qnode,*queueptr;
typedef struct {
Queueptr Front;
QUEUEPTR Rear;
}linkqueue;

When all custom functions are written in front of the main function, it is not necessary to declare each function; When the main function is written first, all the custom functions are declared before the main function.

//function declaration (not declared at this time)
int Initqueue (Linkqueue *q);//Initialize Queue
int Destoryqueue (Linkqueue *q);//Destroy queue
int Emptyqueue (Linkqueue *q);//Determine if the queue is empty
int EnQueue (Linkqueue *q,qelemtype e);//Insert Element e as a new tail element of Q
int DeQueue (linkqueue *q,qelemtype *e);//delete the team header element of the non-empty queue and return the value of the deleted element with E
int Clearqueue (Linkqueue *q);//Empty queue
int Queuelength (Linkqueue *q);//Get the length of the queue
int GetHead (linkqueue *q,qelemtype *e);//Get Team head element
void visit (Qelemtype e);//Custom visit function, representing the value of the output element
void Queuetraverse (Linkqueue *q,void *visit (Qelemtype));//Call visit function on each element in queue Q from the head to the end of the team

//Initialize queue
int Initqueue (Linkqueue *q) {
Q->front = Q->rear = (queueptr) malloc (sizeof (Qnode));
if (! Q->front) exit (OVERFLOW);
Q->front->next = NULL;
return OK;
}

//Destroy queue
int Destoryqueue (Linkqueue *q) {
while (Q->front) {
Q->rear = q->front->next;
Free (Q->front);
Q->front = q->rear;
}
return OK;
}

//Determines whether the queue is empty, returns 1 for null, and returns 0 without null
int Emptyqueue (Linkqueue *q) {
if (Q->front = = q->rear) {
return OK;
} else {
return ERROR;
}
}

//Insert element E for Q's new team tail element
int EnQueue (Linkqueue *q,qelemtype e) {
Queueptr p;
p = (queueptr) malloc (sizeof (Qnode));
if (!p) exit (OVERFLOW);
P->data = e;
P->next = NULL;
Q->rear->next = p;
Q->rear = p;
return OK;
}

//Delete the team header element of the non-empty queue and return the value of the deleted element with E
int DeQueue (linkqueue *q,qelemtype *e) {
Queueptr p;
if (Q->front = = q->rear) return ERROR;
p = q->front->next;
*e = p->data;
Q->front->next = p->next;
if (q->rear = = p) {
Q->rear = q->front;
Q->front->next = NULL;
}
Free (p);
return OK;
}

//Empty queue
int Clearqueue (Linkqueue *q) {
Queueptr p,q;
if (! Emptyqueue (Q)) {
p = q->front->next;
while (P!=q->rear) {
Q = p;
Q->front->next = p->next;
p = p->next;
Free (q);
}
Free (p);
Q->rear = q->front;
return OK;
} else {
return ERROR;
}

}

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

//Get team head element
int GetHead (linkqueue *q,qelemtype *e) {
if (! Emptyqueue (Q)) {
*e = q->front->next->data;
return OK;
} else {
return ERROR;
}
}

//Custom visit function that represents the value of the output element
void visit (Qelemtype e) {
printf ("%d", e);
}

//From the team head to the end of the queue call the visit function for each element in the queues Q
void Queuetraverse (Linkqueue *q,void *visit (qelemtype)) {
Queueptr p;
p = q->front->next;
printf ("The elements in the queue are:");
while (p) {
Visit (P->data);
p= p->next;
}
printf ("\ n");
}

int main ()
{
Linkqueue *link;
int f1,f2,f3,f4,f5,f6,e,i,a,len,n;


//Queue initialization
F1 = initqueue (link);
if (F1) printf ("Queue initialization succeeded!") \ n ");
else printf ("Queue initialization failed!") \ n ");

//Queue
printf ("Please enter the number of elements in the queue:");
scanf ("%d", &n);
printf ("Please enter the value of the element in the queue:");
for (i=0; i<n; i++) {
scanf ("%d", &a);
EnQueue (Link,a);
}

//Get Queue Length
Len = queuelength (link);
printf ("Queue Length:%d\n", Len);

//Determine if the queue is empty
F2 = emptyqueue (link);
if (F2) printf ("Queue is empty!") \ n ");
else printf ("The queue is not empty!") \ n ");

//Traversal queue
Queuetraverse (Link,&visit);

//OUT Team
F3 = DeQueue (link,&e);
if (F3) printf ("Deleted team header element is:%d\n", e);
else printf (the queue is empty!) Unable to delete team header element! \ n ");

//Get team head element
F4 = GetHead (link,&e);
if (F4) printf ("Team head element:%d\n", e);
else printf (the queue is empty!) ");

//Empty queue
F6 = clearqueue (link);
if (f6) printf ("Empty queue succeeds!") \ n ");
else printf ("Empty queue failed!") \ n ");

//Destroy queue
F5 = destoryqueue (link);
if (F5) printf ("Destroy queue succeeded!") \ n ");
else printf ("Destroy queue failed!") \ n ");

return 0;
}

Data structure-a single-chain queue-related operation algorithm

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.