1 //顺序队列 2 #define MAXSIZE 10//假设队列最多容纳10个数,其实因为是循环队列所以其实只最多能容纳9个数 3 #include<stdio.h> 4 #include<stdlib.h> 5 typedef int Status;//用Status来当作bool类型使用 6 typedef struct SqQueue { 7 int front;//定义队列头 8 int rear;//定义队列尾 9 char data[MAXSIZE];//假设队列里的数据是char类型的数据 10 }SqQueue; 11 SqQueue SqQueue_test; //初始化队列 12 void InitQueue() //初始化队列 13 { 14 SqQueue_test.front = 0; 15 SqQueue_test.rear = 0; 16 //出栈用rear=front来判断是否空了 17 //满栈用front+1=rear来表示是否满了,所以其实浪费了一个空间 18 } 19 Status EnQueue(char a) 20 { 21 //判断队列是否满了 22 if ((SqQueue_test.front + 1) % MAXSIZE == SqQueue_test.rear) 23 { 24 return 0; 25 } 26 else 27 { 28 SqQueue_test.data[SqQueue_test.front] = a; 29 SqQueue_test.front = (SqQueue_test.front + 1) % MAXSIZE; 30 } 31 } 32 Status DeQueue() 33 { 34 if ((SqQueue_test.rear == SqQueue_test.front)) 35 { 36 //栈为空 37 return 0; 38 } 39 else 40 { 41 char temp = SqQueue_test.data[SqQueue_test.rear]; 42 printf("%c已出队", temp); 43 SqQueue_test.rear = (SqQueue_test.rear + 1) % MAXSIZE; 44 } 45 } 46 int GetLenQueue()//得到栈的长度 47 { 48 return (MAXSIZE - SqQueue_test.rear % MAXSIZE + SqQueue_test.rear % MAXSIZE); 49 } 50 51 52 53 54 void test() 55 { 56 char a[] = "sadad"; 57 //入栈 58 for (int i = 0; i < 5; i++) 59 { 60 EnQueue(a[i]); 61 } 62 //出栈 63 for (int i = 0; i < 5; i++) 64 { 65 DeQueue(); 66 } 67 } 68 int main() 69 { 70 test(); 71 return 0; 72 }