通用右移
Generic Right Shift
我被布置了以下作业问题:
给定一组 'n' 个元素和 'r',编写一个通用函数将元素集右移 'r' 个位置。如果要将元素移动到大于 'n' 的位置,则将移位过程包装到集合的开头。例如,如果五个元素的集合是 1,7,8,9,12 并且 'r' 的值为 3,则元素集合将是 8, 9, 12, 1, 7.
对于这个问题,我有点喜欢循环队列,所以我用它写了一个代码。现在它不包含任何通用函数,但这不是问题,我的问题是我的移位函数没有正确移动元素。请帮助我。
#include<iostream>
#include<string>
using namespace std;
#define max 10
int q[max];
int front,rear;
void init() {
front = -1;
rear = -1;
}
void enqueue(int x) {
if(front == (rear+1)%max) {
cout<<"Queue overflow";
}
if(front == -1) {
front = 0;
rear = 0;
}
else {
rear = (rear+1)%max;
}
q[rear]=x;
//cout<<"Front"<<front<<endl;
//cout<<"Rear"<<rear<<endl;
}
int dequeue() {
int x;
if(front == -1) {
cout<<"Queue Underflow";
}
else {
x = q[rear];
rear = (rear+1)%max;
}
return x;
}
void display() {
int i;
if(front == -1) {
cout<<"Queue Underflow";
}
else {
for(i = front; i != rear; i =( i+1)%max) {
cout<<q[i]<<'\t';
}
cout<<q[rear]<<endl;
}
}
void shift(int num) {
//do the shifting business here
int i,x,r;
cout<<"Enter by how many positions should the elements be shifted towards the right"<<endl;
cin>>r;
for(i = 0; i < (num-r); ++i) {
x = dequeue();
enqueue(x);
}
}
int main() {
int ch,n,i,x,r;
init();
//cout<<"Enter your choice"<<endl;
//cin>>ch;
cout<<"Number of elements in the collection"<<endl;
cin>>n;
for(i = 0; i < n; ++i) {
cout<<"Enter the value to be added"<<endl;
cin>>x;
enqueue(x);
}
cout<<"The elements to be shifted"<<endl;
display();
shift(n);
display();
}
编辑
我给出的输入是:
元素数量:
5
要移动的元素:
1个
7
8个
9
12
元素移动的值:
3
预期输出:
8 9 12 1 7
我得到的输出:
1 7 8 9 12 0 12 0 12
您的错误在 dequeue()
中。你需要让前面的指针前进,而不是让后面的指针前进:
int dequeue() {
int x;
if(front == -1) {
cout<<"Queue Underflow";
}
else {
x = q[front];
front = (front+1)%max;
}
return x;
}
我被布置了以下作业问题:
给定一组 'n' 个元素和 'r',编写一个通用函数将元素集右移 'r' 个位置。如果要将元素移动到大于 'n' 的位置,则将移位过程包装到集合的开头。例如,如果五个元素的集合是 1,7,8,9,12 并且 'r' 的值为 3,则元素集合将是 8, 9, 12, 1, 7.
对于这个问题,我有点喜欢循环队列,所以我用它写了一个代码。现在它不包含任何通用函数,但这不是问题,我的问题是我的移位函数没有正确移动元素。请帮助我。
#include<iostream>
#include<string>
using namespace std;
#define max 10
int q[max];
int front,rear;
void init() {
front = -1;
rear = -1;
}
void enqueue(int x) {
if(front == (rear+1)%max) {
cout<<"Queue overflow";
}
if(front == -1) {
front = 0;
rear = 0;
}
else {
rear = (rear+1)%max;
}
q[rear]=x;
//cout<<"Front"<<front<<endl;
//cout<<"Rear"<<rear<<endl;
}
int dequeue() {
int x;
if(front == -1) {
cout<<"Queue Underflow";
}
else {
x = q[rear];
rear = (rear+1)%max;
}
return x;
}
void display() {
int i;
if(front == -1) {
cout<<"Queue Underflow";
}
else {
for(i = front; i != rear; i =( i+1)%max) {
cout<<q[i]<<'\t';
}
cout<<q[rear]<<endl;
}
}
void shift(int num) {
//do the shifting business here
int i,x,r;
cout<<"Enter by how many positions should the elements be shifted towards the right"<<endl;
cin>>r;
for(i = 0; i < (num-r); ++i) {
x = dequeue();
enqueue(x);
}
}
int main() {
int ch,n,i,x,r;
init();
//cout<<"Enter your choice"<<endl;
//cin>>ch;
cout<<"Number of elements in the collection"<<endl;
cin>>n;
for(i = 0; i < n; ++i) {
cout<<"Enter the value to be added"<<endl;
cin>>x;
enqueue(x);
}
cout<<"The elements to be shifted"<<endl;
display();
shift(n);
display();
}
编辑
我给出的输入是:
元素数量: 5
要移动的元素: 1个 7 8个 9 12
元素移动的值: 3
预期输出:
8 9 12 1 7
我得到的输出:
1 7 8 9 12 0 12 0 12
您的错误在 dequeue()
中。你需要让前面的指针前进,而不是让后面的指针前进:
int dequeue() {
int x;
if(front == -1) {
cout<<"Queue Underflow";
}
else {
x = q[front];
front = (front+1)%max;
}
return x;
}