---c language of data structure to implement chain-queue

Source: Internet
Author: User

Chained queue storage//Xin Yang # include <stdio.h> #include <stdlib.h>typedef int qelemtype;//definition node typedef struct qnode{ Qelemtype data;struct qnode *next;} Qnode, *queueptr;//defines the pointer typedef struct{queueptr Front; QUEUEPTR Rear;} linkqueue;//Insert element e enters queue void En_queue (Linkqueue *q, Qelemtype e) {queueptr temp = (queueptr) malloc (sizeof (Qnode)); if (temp {temp->data = E;temp->next = Null;q->rear->next = Temp;q->rear = temp;}} Initialize queue void Init_queue (Linkqueue *q) {Q->front = Q->rear = (Qnode *) malloc (sizeof (Qnode)); Q->front->next = NULL;} Create queue void Create_queue (Linkqueue *q) {int n = 0;init_queue (q);p rintf ("Enter the element to enter the queue, end with 0!\n"), while (scanf ("%d", &n ) {if (n = = 0) break;en_queue (q, N);}} e element out of team void De_queue (Linkqueue *q, Qelemtype *e) {if (Q->front = = q->rear) return; Queueptr temp = q->front->next;if (Q->front->next = = q->rear) Q->rear = Q->front;*e = temp->data; Q->front->next = Temp->next;free (temp);} Determines whether the queue is empty int is_empty (Linkqueue *q) {if (Q->front= = q->rear) return 1;return 0;} return queue int getlength_queue (Linkqueue *q) {Queueptr temp = Q->front;int i = 0;while (temp! = q->rear) {++i;temp = TEMP-&G T;next;} return i;} Empty queue void Clear (Linkqueue *q) {Queueptr temp = q->front->next;while (temp) {queueptr TP = Temp;temp = temp->next; FREE (TP);} temp = Q->front;q->front = Q->rear = Null;free (temp);} print queue element void Print_queue (Linkqueue *q) {if (Q->front = = q->rear) return; Queueptr temp = q->front->next;while (temp! = q->rear) {printf ("%d", temp->data); temp = Temp->next;} printf ("%d", Temp->data);p rintf ("\ n");} First data out of team void Top_queue (Linkqueue *q, Qelemtype *e) {if (Q->front = = q->rear) Return;*e = q->front->next-> data;} int main () {int i = 0, k = 0, top = 0;int len; Linkqueue Q;create_queue (&q); Top_queue (&q, &top);p rintf ("element of Team head:%d\n", top); len = Getlength_queue (& Q);p rintf ("Traverse all elements in the team: \ n"); for (i = 0; i < len; i++) {De_queue (&q, &k);p rintf ("%d", k);} printf ("\ n"); Clear (&amP;Q); return 0;} 




---c language of data structure to implement chain-queue

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.