Data structure--Queue and loop queue

Source: Internet
Author: User

Description: Min's "Data structure" (C language Version) study notes, record, for later viewing.


#include <stdio.h> #include <stdlib.h> #define OK 1; #define ERROR-1;TYPEDEF int qelemtype;typedef int Status    ;//define Queue node typedef struct qnode{Qelemtype data; struct Qnode *next;}  Qnode, *queueptr;//queue typedef struct{QUEUEPTR Front;   Team head pointer queueptr rear;    Team tail pointer}linkqueue;//Initialize queue status Initqueue (Linkqueue *q) {Q->front = Q->rear = (queueptr) malloc (sizeof (Qnode));    if (!q->front) return ERROR;    Q->front->next = NULL; return OK;}        Destroy queue status Destroyqueue (Linkqueue *q) {while (Q->front) {q->rear = q->front->next;        Free (Q->front);    Q->front = q->rear; } return OK;    Insert element Status EnQueue (Linkqueue *q, Qelemtype e) {queueptr p = (queueptr) malloc (sizeof (Qnode));    if (!p) return ERROR;    P->data = e;    P->next = NULL; Q->rear->next = p; The end of the queue points to P q->rear = p; P is the tail pointer return OK;}  Out Queue status DeQueue (Linkqueue *q, Qelemtype *e) {if (Q->front = = q->rear) return ERROR;  Queueptr p = q->front->next;    Point to the head of the queue first element *e = p->data;    Q->front->next = p->next;    if (q->rear = = p) q->rear = q->front;    Free (p); return OK;}

When initializing, let front and rear are equal to 0, at this time the number of queue elements is 0, when the 4 elements queued as


The number of queue elements at this time is Rear-front = 4, if the queue becomes the following, rear < front


The reason the queue is hollow is to distinguish whether the queue is empty or full, and the queue is full when the next position of the tail pointer is the head pointer.

#include <stdio.h> #include <stdlib.h> #define OK 1#define error-1#define maxqsize 100//MAX Queue Length typedef int QEL    emtype;typedef int status;//cyclic queue typedef struct{QELEMTYPE *base;    int front; int rear;}    sqqueue;//Initialize Loop queue status Initqueue (Sqqueue *q) {q->base = (Qelemtype *) malloc (maxqsize * sizeof (Qelemtype));    if (!q) return ERROR;    Q->front = q->rear = 0; return OK;} Gets the queue Length int queuelength (Sqqueue *q) {return (Q->rear-q->front + maxqsize)% Maxqsize;}    Queue status EnQueue (Sqqueue *q, Qelemtype e) {if ((q->rear + 1)% Maxqsize = = Q->front) {return ERROR;    } Q->base[q->rear] = e;    Q->rear = (q->rear + 1)% Maxqsize; return OK;}    Out of Team status DeQueue (Sqqueue *q, Qelemtype *e) {if (Q->front = = q->rear) return ERROR;    *e = q->base[q->front];    Q->front = (q->front + 1)% Maxqsize; return OK;}

Data structure--Queue and loop 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.