具有仅显示偶数的显示功能的循环队列
Circular Queue That has a display function that displays even numbers only
嗨,所以我有这个循环队列 C++ 程序,我需要它的显示功能,只显示甚至插入的数字,这里有人可以帮忙,请提供代码
我需要一种方法让这个程序只显示偶数 我一直在尝试在某些可能有意义的位置使用 %2==0 但大多数时候我弄错了或为空
.
.
.
.
.
.
#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;
void insertCQ(int val) {
if ((front == 0 && rear == n-1) || (front == rear+1)) {
cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"1)Insert\n";
cout<<"2)Delete\n";
cout<<"3)Display\n";
cout<<"4)Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}
这是一个使用 std::queue
的简单示例:
#include <iostream>
#include <queue>
int main()
{
// This deque is declared just to more easily instantiate the queue
std::deque<int> deck{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::queue<int> q{deck};
while (!q.empty()) {
if (q.front() % 2 == 0) {
std::cout << q.front() << ' ';
}
q.pop();
}
std::cout << '\n';
}
您使用 mod 2 的方法是正确的,但是关于您的 "queue" 的所有内容都不正确。简单的说,你没有写队列,肯定不是循环队列。
队列是先进先出的数据结构;先进先出。把它想象成一条隧道。我只会在一端添加元素,而只会从另一端移除元素。中间的一切都无关紧要,除了知道队列的大小。这意味着 only 可见元素是第一个和最后一个。如果我想看到队列的第二个元素,我必须删除第一个元素。
您编写的代码将您的数组视为一个列表,而不是您可以自由访问所有元素的列表。
最好写一个真正的队列class,我希望如果你在写数据结构,你可以写一个class。
这是一个非常基本的队列 class,它展示了您所询问的行为。它没有任何循环。循环这个词意味着你的队列应该用循环链表而不是数组来实现。值得注意的是,它至少需要 C++11,但在今天这应该不是问题。
重要
这段代码省去了很多必要的错误检查。例如,如果在空队列上调用,back()
可能会导致未定义的行为,等等。这是有意避免 copy/paste 被雇用的,因为作弊是不好的。
#include <array>
#include <iostream>
namespace Q {
class queue {
public:
int &front() { return m_arr[0]; }
int &back() { return m_arr[m_size - 1]; }
void push(int val) {
if (m_size < 5) {
m_arr[m_size] = val;
++m_size;
}
}
// Shifts entire array one to the left
void pop() {
if (m_size > 0) {
for (int i = 1; i < m_size; ++i) {
m_arr[i - 1] = m_arr[i];
}
--m_size;
}
}
bool empty() const { return m_size == 0; }
private:
std::array<int, 5> m_arr = {0};
int m_size = 0;
};
}; // namespace Q
int main() {
Q::queue q;
for (int i = 1; i <= 5; ++i) {
q.push(i);
}
while (!q.empty()) {
if (q.front() % 2 == 0) {
std::cout << q.front() << ' ';
}
q.pop();
}
std::cout << '\n';
}
嗨,所以我有这个循环队列 C++ 程序,我需要它的显示功能,只显示甚至插入的数字,这里有人可以帮忙,请提供代码
我需要一种方法让这个程序只显示偶数 我一直在尝试在某些可能有意义的位置使用 %2==0 但大多数时候我弄错了或为空 . . . . . .
#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;
void insertCQ(int val) {
if ((front == 0 && rear == n-1) || (front == rear+1)) {
cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"1)Insert\n";
cout<<"2)Delete\n";
cout<<"3)Display\n";
cout<<"4)Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}
这是一个使用 std::queue
的简单示例:
#include <iostream>
#include <queue>
int main()
{
// This deque is declared just to more easily instantiate the queue
std::deque<int> deck{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::queue<int> q{deck};
while (!q.empty()) {
if (q.front() % 2 == 0) {
std::cout << q.front() << ' ';
}
q.pop();
}
std::cout << '\n';
}
您使用 mod 2 的方法是正确的,但是关于您的 "queue" 的所有内容都不正确。简单的说,你没有写队列,肯定不是循环队列。
队列是先进先出的数据结构;先进先出。把它想象成一条隧道。我只会在一端添加元素,而只会从另一端移除元素。中间的一切都无关紧要,除了知道队列的大小。这意味着 only 可见元素是第一个和最后一个。如果我想看到队列的第二个元素,我必须删除第一个元素。
您编写的代码将您的数组视为一个列表,而不是您可以自由访问所有元素的列表。
最好写一个真正的队列class,我希望如果你在写数据结构,你可以写一个class。
这是一个非常基本的队列 class,它展示了您所询问的行为。它没有任何循环。循环这个词意味着你的队列应该用循环链表而不是数组来实现。值得注意的是,它至少需要 C++11,但在今天这应该不是问题。
重要
这段代码省去了很多必要的错误检查。例如,如果在空队列上调用,back()
可能会导致未定义的行为,等等。这是有意避免 copy/paste 被雇用的,因为作弊是不好的。
#include <array>
#include <iostream>
namespace Q {
class queue {
public:
int &front() { return m_arr[0]; }
int &back() { return m_arr[m_size - 1]; }
void push(int val) {
if (m_size < 5) {
m_arr[m_size] = val;
++m_size;
}
}
// Shifts entire array one to the left
void pop() {
if (m_size > 0) {
for (int i = 1; i < m_size; ++i) {
m_arr[i - 1] = m_arr[i];
}
--m_size;
}
}
bool empty() const { return m_size == 0; }
private:
std::array<int, 5> m_arr = {0};
int m_size = 0;
};
}; // namespace Q
int main() {
Q::queue q;
for (int i = 1; i <= 5; ++i) {
q.push(i);
}
while (!q.empty()) {
if (q.front() % 2 == 0) {
std::cout << q.front() << ' ';
}
q.pop();
}
std::cout << '\n';
}