C Language Data structure: Implementing circular queues with flag bits

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3 4 #defineMAXSIZE 10//Define Queue Length5 6 Static intflag=0;//Defining flag bits7 8typedefstruct {9     int*Base;Ten     intFront; One     intRear; A}sqqueue;//Create a struct body -  - intInitqueue (Sqqueue &Q) { theQ.Base=(int*)malloc(maxsize*sizeof(int)); -     if(! Q.Base) -         return 0; -     Else{ +Q.front=q.rear=0; -         return 1; +     } A}//Create an empty list at  -  - intIsempety (Sqqueue &2) { -     if(flag==0&&q.front==q.rear) { -printf"queue is empty \ n"); -         return 0; in     } -     Else  to         return 1; +  -}//determines whether the empty the  * intIsfull (Sqqueue &p) { $     if(flag==1&&q.front==q.rear) {Panax Notoginsengprintf"queue is full \ n"); -         return 0; the     } +     Else A         return 1; the}//determine if it is full +  - voidAddqueue (Sqqueue &Q) { $     if(Isfull (Q) = =1){ $         inte; -printf"Please enter an element: \ n"); -scanf"%d",&e); theQ.Base[q.rear]=e; -Q.rear= (q.rear+1)%MAXSIZE;Wuyiflag=1; the     } -}//adding elements Wu  - voidDeletequeue (Sqqueue &Q) { About     if(Isempety (Q) = =1){ $         inte; -E=q.Base[Q.front]; -Q.front= (q.front+1)%MAXSIZE; -flag=0; Aprintf"%d\n", e); +     } the}//Subtract elements -  $ voidAction (Sqqueue &Q) { theprintf"1. queue \ n"); theprintf"2. Bring the team head element out of the team and return its value \ n"); theprintf"3. Exit \ n"); the      intA; -scanf"%d",&a); in       Switch(a) the       { the        Case 1: About       the Addqueue (Q); the            Break; the        Case 2: +            - Deletequeue (Q); the            Break;Bayi        Case 3: theExit0); the       default: -printf"input is not valid, please re-enter \ n"); -  the       } the Action (Q); the}//Action Options the  - intMain () { the sqqueue Q; the     if(Initqueue (Q)) theprintf"created successfully. \ n");94     Else{ theprintf"creation failed. "); theSystem"Pause"); the         return 0;98     } About      - Action (Q);101     102System"Pause");103      Free(Q.Base);104     return 0; the 106 107}//Main function

Although implemented with a flag bit, an error occurs when swapping an int array to a char array, where doubts remain, pending resolution.

C Language Data structure: Implementing circular queues with flag bits

Related Article

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.