Chained queue definition, insert, delete

Source: Internet
Author: User

#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 (&AMP;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-&Gt;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 (&AMP;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

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.