//循环队列
#include<iostream>
using namespace std;
typedef struct Qq{
int *base;
int front;
int rear;
}Qq;
void chushihua(Qq &q) {
q.base = new int[15];
if (!q.base) {
cout << "分配地址失败" << endl;
return;
}
cout << "分配地址成功" << endl;
q.front = q.rear = 0;
}
void changdu(Qq l) {
int count = 0;
while (l.front != l.rear) {
count++;
l.front = (l.front + 1) % 15;
}
cout << count << endl;
// count=(l.rear=l.front+15)%15;
}
bool panduanduiman(Qq l) {
if (l.front == (l.rear + 1) % 15) {
cout << "循环对列已满" << endl;
return false;
}
else
return true;
}
void rudui(Qq &l,int e) {
if (!panduanduiman(l)) {
cout << "不可以入队了" << endl;
return;
}
l.base[l.rear] = e;
l.rear = (l.rear + 1) % 15;
}
void chudui(Qq &l) {
if (l.front == l.rear) {
cout << "队列为空" << endl;
return;
}
cout << l.base[l.front] << endl;
l.front = (l.front + 1) % 15;
}
void bianli(Qq l) {
int m = l.front;
while (m!=l.rear){
cout << l.base[m] << '\t';
m = (m + 1) % 15;
}
cout << endl;
}
int main() {
Qq l;
chushihua(l);
int c[10] = { 0,1,2,3,4,5,6,7,8,9 };
for (int i = 0; i < 10; i++) {
rudui(l, c[i]);
}
for (int i = 0; i < 10; i++) {
chudui(l);
}
for (int i = 0; i < 10; i++) {
rudui(l, c[i]);
}
bianli(l); changdu(l);
return 0;
}

posted on 2020-11-09 18:28  柠檬tea的味道  阅读(53)  评论(0)    收藏  举报