Data structure---C-language implementation loop queue

Source: Internet
Author: User

Loop queue//Xin Yang # include <stdio.h> #include <stdlib.h> #define MAXSIZE typedef int QELEMTYPE;TYPEDEF struct  queue{Qelemtype Elem[maxsize];  int Front;int rear; }sqqueue;  Define Team Header int Init_queue (sqqueue **q)//Initialize {(*q)->front= 0;  (*q)->rear = 0;  } int Is_full (Sqqueue *q) {if (Q->front = = (q->rear + 1)% MAXSIZE)//Full Liu Miangang return 1;  else return 0;  } int Insert_queue (Sqqueue **q, Qelemtype elem) {if (Is_full (*q)) return-1; (*q)->elem[(*q)->rear] = Elem; (*q)->rear = ((*q)->rear + 1)% MAXSIZE;  Insert return 0;  } int Is_empty (Sqqueue *q) {if (Q->front = = q->rear)//empty return 1;  else return 0;  } int Delete_queue (sqqueue * * q, Qelemtype *pelem) {if (Is_empty (*q)) return 0;  *pelem = (*q)->elem[(*q)->front];  (*q)->front = ((*q)->front + 1)%maxsize;  return 0;  } int main () {int i=0, elem, count = 0;  Sqqueue *q= (Sqqueue *) malloc (sizeof (sqqueue));  Init_queue (&AMP;Q); printf ("========================= splitLine =====================\n ");p rintf (" Inserting elements into the loop queue!  \ n "); for (i = 0; i < i++) {Insert_queue (&q, i);  count++;} printf ("The team is finished!" \ n ");p rintf (" ========================= Split Line =====================\n ");p rintf (" Total elements in circular queue:%d \ n ", count);p rintf (" The elements in the loop queue are: \ n "); for (i = 0; i < count; i++) {printf ("%d\t ", I);} printf ("\ n");p rintf ("========================= Split Line =====================\n");p rintf ("Delete the first 5 elements in the queue: \ n");p rintf ("  deleting please later: \ n "); for (i = 0; i < 4; i++) {Delete_queue (&q, &elem);  printf ("will:%d delete \ n", elem);   count--;} printf ("========================= Split Line =====================\n");p rintf ("Total elements in circular queue:%d \ n", count);p rintf (" The element in the loop queue after the delete operation has: \ n "); for (i = 1; I <= count; i++) {printf ("%d\t ", Elem+i);}  printf ("\ n");p rintf ("========================= Split Line =====================\n"); return 0;   }



Results:



Data structure---C-language implementation 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.