#include <stdio.h> #include <stdlib.h> #define elemtype inttypedef struct qnode{elemtype data;//define the elements in the queue struct Qnode *next;} qnode;typedef struct linkqueue{qnode *front; Qnode *rear;} linkqueue;//defines a queue that has only one head pointer and one tail pointer//queue initialization of void Initqueue (Linkqueue *q) {Q->front = (linkqueue*) malloc (sizeof ( Qnode)); if (!q->front) {exit (0); } q->rear = q->front; Q->front->next = NULL;} The queue's insert void Insertqueue (Linkqueue *p,elemtype *e)//Team column is P, the element to be inserted is E, and the rear pointer of the queue points to the last element of the queue. The front pointer of the queue points to the head node, and the head node has no element {Qnode *ptr; ptr = (linkqueue*) malloc (sizeof (Qnode)); Ptr->data = *e; Ptr->next = NULL; P->rear->next = ptr;//P->rear = p->rear->next; P->rear = ptr;} Deletion of the queue void Deletequeue (Linkqueue *p,elemtype *e) {Qnode *s; if (P->front = = p->rear) {return; } s = p->front->next; *e = s->data; P->front->next = s->next; if (s = = p->rear) if (p->front->next = = NULL)//With this can also be equivalent {P->front = p->rear; } free (s);} Destroys a queue void Destoryqueue (Linkqueue *p) {while (P->front) {p->rear = P->front->next;//p->front ->next = = NULL free (p->front);//release P's head node P->front = p->rear;//The front and rear of p are set to NULL}}int main ( ) {int i; Linkqueue p; Elemtype e; Initqueue (&P); for (i = 1;i <= 10;i++) {insertqueue (&p,&i); } for (i = 1;i <= 10;i++) {deletequeue (&p,&e); printf ("%2d", e); } return 0;}
Enter a string to end with the character ' # ' and print it out on the screen
#include <stdio.h> #include <stdlib.h> #define M 100#define elemtype chartypedef struct qnode{elemtype data; Defines the elements in the queue struct Qnode *next;} qnode;typedef struct linkqueue{qnode *front; Qnode *rear;} linkqueue;//defines a queue that has only one head pointer and one tail pointer//queue initialization of void Initqueue (Linkqueue *q) {Q->front = (linkqueue*) malloc (sizeof ( Qnode)); if (!q->front) {exit (0); } q->rear = q->front; Q->front->next = NULL;} The queue's insert void Insertqueue (Linkqueue *p,elemtype *e)//Team column is P, the element to be inserted is E, and the rear pointer of the queue points to the last element of the queue. The front pointer of the queue points to the head node, and the head node has no element {Qnode *ptr; ptr = (linkqueue*) malloc (sizeof (Qnode)); Ptr->data = *e; Ptr->next = NULL; P->rear->next = ptr;//P->rear = p->rear->next; P->rear = ptr;} Deletion of the queue void Deletequeue (Linkqueue *p,elemtype *e) {Qnode *s; if (P->front = = p->rear) {return; } s = p->front->next; *e = s->data; P->front->next = s->next; if (s = = p->rear) if (p-≫front->next = = NULL)//This can also be equivalent to {P->front = p->rear; } free (s);} Destroys a queue void Destoryqueue (Linkqueue *p) {while (P->front) {p->rear = P->front->next;//p->front ->next = = NULL free (p->front);//release P's head node P->front = p->rear;//The front and rear of p are set to NULL}}int main ( ) {int i,len = 0; Linkqueue p; Elemtype e; char c; Initqueue (&P); printf ("Input string: \ n"); scanf ("%c", &c); while (c! = ' # ') {insertqueue (&p,&c); len++; scanf ("%c", &c); } printf ("string: \ n"); for (i = 1;i <= len;i++) {deletequeue (&p,&e); printf ("%c", e); } return 0;}
Chained queue definition, insert, delete