1 #include <iostream> 3 using namespace std; 5 const int N=6; //队列长度 7 int a[N]; //队列数组 9 int head; //头指针的位置 11 int tail; // 15 void init(); //初始
17 void add(int x); //队列入队 18 19 int out(); //队列出队 20 21 bool iffull(); //判断队列满 22 23 bool ifempty(); //判断队列为空 24 25 int alen(); //求当前队列的长度 26 27 int main() 28 29 { 30 31 init(); 33 add(11); 35 add(12); 37 add(13); 39 cout<<out()<<endl; 41 cout<<out()<<endl; 43 cout<<out()<<endl; 45 cout<<out()<<endl; 47 return 0; 49 } 50 53 int alen() 54 55 { 57 return (tail - head +N) % N; 59 } 60 61 bool ifempty() 63 { 65 return head == tail; 67 } 70 71 bool iffull() 73 { 75 return (tail+1) % N == front; 77 } 78 81 int out() 83 { 85 if (ifempty()) 87 { 89 cout<<"队列已空,不能出队"<<endl; 91 return 0; 93 } 94 95 int x = a[head]; 97 head = (head+1) % N; 99 return x; 101 }
105 void add(int x) 107 { 109 if (iffull()) 111 { 113 cout<<"队列已满,不能加入"<<endl; 115 return ; 117 } 119 a[tail] = x; 121 tail = (tail+1) % N; 123 } 127 void init() 128 129 { 131 head =0; 133 tail =0; 135 }
摘自https://blog.csdn.net/noipBar/article/details/84346958
原文:https://www.cnblogs.com/2020cs/p/12250842.html