C-Language Data Structure: Use a flag to implement a cyclic queue and a data structure queue

Source: Internet
Author: User

C-Language Data Structure: Use a flag to implement a cyclic queue and a data structure queue

1 # include <stdio. h> 2 # include <stdlib. h> 3 4 # define MAXSIZE 10 // define the queue length 5 6 static int flag = 0; // define the flag position 7 8 typedef struct {9 int * base; 10 int front; 11 int rear; 12} SqQueue; // create struct 13 14 int InitQueue (SqQueue & Q) {15 Q. base = (int *) malloc (MAXSIZE * sizeof (int); 16 if (! Q. base) 17 return 0; 18 else {19 Q. front = Q. rear = 0; 20 return 1; 21} 22} // create an empty linked list 23 24 25 int IsEmpety (SqQueue & Q) {26 if (flag = 0 & Q. front = Q. rear) {27 printf ("the queue is empty \ n"); 28 return 0; 29} 30 else 31 return 1; 32 33} // judge whether it is null 34 35 int IsFull (SqQueue & Q) {36 if (flag = 1 & Q. front = Q. rear) {37 printf ("queue full \ n"); 38 return 0; 39} 40 else 41 return 1; 42} // determine whether it is full 43 44 void AddQueue (SqQueue & Q) {45 if (I SFull (Q) = 1) {46 int e; 47 printf ("Please input element: \ n"); 48 scanf ("% d", & e ); 49 Q. base [Q. rear] = e; 50 Q. rear = (Q. rear + 1) % MAXSIZE; 51 flag = 1; 52} 53} // Add element 54 55 void DeleteQueue (SqQueue & Q) {56 if (IsEmpety (Q) = 1) {57 int e; 58 e = Q. base [Q. front]; 59 Q. front = (Q. front + 1) % MAXSIZE; 60 flag = 0; 61 printf ("% d \ n", e ); 62} 63} // Delete element 64 65 void Action (SqQueue & Q) {66 printf ("1. join \ n "); 67 printf (" 2. enable the team Header element to exit and return its value \ n "); 68 printf ("3. exit \ n "); 69 int a; 70 scanf (" % d ", & a); 71 switch (a) 72 {73 case 1: 74 75 AddQueue (Q ); 76 break; 77 case 2: 78 79 DeleteQueue (Q); 80 break; 81 case 3: 82 exit (0); 83 default: 84 printf ("the input is invalid, enter \ n ") again; 85 86} 87 Action (Q); 88} // Operation Option 89 90 int main () {91 SqQueue Q; 92 if (InitQueue (Q) 93 printf ("created successfully. \ N "); 94 else {95 printf (" creation failed. "); 96 system (" pause "); 97 return 0; 98} 99 100 Action (Q); 101 102 system (" pause "); 103 free (Q. base); 104 return 0; 105 106 107} // Main Function

 

 

 

Although it is implemented with a flag, an error occurs when you replace the int type array with a char type array. In this case, we will keep you in doubt and wait for you to solve the problem.

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.