Self-built algorithm library of data structure--sequential ring queue

Source: Internet
Author: User

This paper is aimed at the basic series of data Structure network course (3): Stack and queue 9th class The storage and operation of the ring queue.

Build your own professional infrastructure algorithm library according to the method suggested in the "0207 Algorithm Change Program" [VIDEO] section.

The sequential ring Queue algorithm library uses the program's multi-file organization, including two files:
  
1. header file: Sqqueue.h, contains the code that defines the sequential ring queue data structure, the macro definition, the declaration of the function to implement the algorithm;

#ifndef sqqueue_h_included#define Sqqueue_h_included#define MAXSIZE 5typedefCharElemtype;typedefstruct{Elemtype data[maxsize];intFront,rear;/* Team head and tail hands */} Sqqueue;voidInitqueue (Sqqueue *&q);//Initialize sequential ring queuevoidDestroyqueue (Sqqueue *&q);//Destroy sequential ring queueBOOLQueueempty (Sqqueue *q);//Determine if the order ring queue is emptyintQueuelength (Sqqueue *q);//Returns the number of elements in the queue, also known as the queue lengthBOOLEnQueue (Sqqueue *&q,elemtype e);//Incoming teamBOOLDeQueue (Sqqueue *&q,elemtype &e);//OUT Team#endif//sqqueue_h_included

2. source file: Sqqueue.cpp, which contains definitions of functions that implement various algorithms

#include <stdio.h>#include <malloc.h>#include "sqqueue.h"voidInitqueue (Sqqueue *&q)//Initialize sequential ring queue{q= (Sqqueue *)malloc(sizeof(Sqqueue)); Q->front=q->rear=0;}voidDestroyqueue (Sqqueue *&q)//Destroy sequential ring queue{ Free(q);}BOOLQueueempty (Sqqueue *q)//Determine if the order ring queue is empty{return(q->front==q->rear);}intQueuelength (Sqqueue *q)//Returns the number of elements in the queue, also known as the queue length{return(q->rear-q->front+maxsize)%maxsize;}BOOLEnQueue (Sqqueue *&q,elemtype e)//Incoming team{if((q->rear+1)%maxsize==q->front)//The team is full of overflow        return false; Q->rear= (q->rear+1)%maxsize; q->data[q->rear]=e;return true;}BOOLDeQueue (sqqueue *&q,elemtype &e)//OUT Team{if(q->front==q->rear)//Team empty down overflow        return false; Q->front= (q->front+1)%maxsize; e=q->data[q->front];return true;}

3. Create a source file (such as main.cpp) in the same project, and compile the main function to complete the relevant testing work. Cases:

#include <stdio.h>#include "sqqueue.h"intMain () {Elemtype E; Sqqueue*q;printf("(1) Initialize queue q\n"); Initqueue (Q);printf("(2) sequentially into the queue element a,b,c\n");if(EnQueue (Q,' A ')==0)printf("The team is full, can't enter the team.");if(EnQueue (Q,' B ')==0)printf("The team is full, can't enter the team.");if(EnQueue (Q,' C ')==0)printf("The team is full, can't enter the team.");printf("(3) queue is %s\ n", (Queueempty (Q)?"Empty":"Non-empty"));if(DeQueue (Q, e) = =0)printf("The team is empty, can't be out of the team \");Else        printf("(4) out of the team an element %c\ n", e);printf("(5) Number of elements in queue Q:%d\ n", Queuelength (Q));printf("(6) sequentially into the queue element d,e,f\n");if(EnQueue (Q,' d ')==0)printf("The team is full, can't enter the team.");if(EnQueue (Q,' E ')==0)printf("The team is full, can't enter the team.");if(EnQueue (Q,' F ')==0)printf("The team is full, can't enter the team.");printf("(7) Number of elements in queue Q:%d\ n", Queuelength (Q));printf("(8) Out queue sequence:"); while(! Queueempty (Q) {DeQueue (Q, e);printf("%c ", e); }printf("\ n");printf("(9) Release queue \ n"); Destroyqueue (Q);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Self-built algorithm library of data structure--sequential ring 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.