编写函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh” 求一个答案,万谢!
答案:1 悬赏:80 手机版
解决时间 2021-08-23 20:04
- 提问者网友:暮烟疏雨之际
- 2021-08-22 21:32
编写函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh”
求一个答案,万谢!
最佳答案
- 五星知识达人网友:醉吻情书
- 2021-08-22 21:57
#include<iostream>
using namespace std;
struct node //a struct of type node
{
char item;
node *next;
};
node *head;
node *ptr = head;
void creatList()
{
cout << "Enter a sequence of charactors and end with &:"<<endl;
node *head=NULL;
node *tail;
char i;
cin >> i;
if (i != '&')
{
head = new node;
head->item = i;
head->next = head;
tail = head; //first node
cin >> i; //cin next integer
while (i != '&')
{
tail->next = new node;
tail = tail->next;
tail->item = i;
tail->next = head;
cin >> i;
}
}
cout << "The charactor in the list are: "<<endl;
node *c = head;
do {
cout<<c->item<<" ";
c = c->next;
}while(c != head);
cout<<endl;
}
void move()
{
ptr = ptr->next;
}
int main()
{
int n;
creatList( );
cout<<"Please enter the number to determine how many positions you want to move: ";
cin>>n; cout<<endl;
for (int j = 0 ; j < n; j++)
{
move();
}
cout<<"The charactor string after traverse: ";
node *cu = ptr;
do {
cout<<cu->item<<" ";
cu = cu->next;
}while(cu != ptr);
cout<<endl;
return 0;
}
using namespace std;
struct node //a struct of type node
{
char item;
node *next;
};
node *head;
node *ptr = head;
void creatList()
{
cout << "Enter a sequence of charactors and end with &:"<<endl;
node *head=NULL;
node *tail;
char i;
cin >> i;
if (i != '&')
{
head = new node;
head->item = i;
head->next = head;
tail = head; //first node
cin >> i; //cin next integer
while (i != '&')
{
tail->next = new node;
tail = tail->next;
tail->item = i;
tail->next = head;
cin >> i;
}
}
cout << "The charactor in the list are: "<<endl;
node *c = head;
do {
cout<<c->item<<" ";
c = c->next;
}while(c != head);
cout<<endl;
}
void move()
{
ptr = ptr->next;
}
int main()
{
int n;
creatList( );
cout<<"Please enter the number to determine how many positions you want to move: ";
cin>>n; cout<<endl;
for (int j = 0 ; j < n; j++)
{
move();
}
cout<<"The charactor string after traverse: ";
node *cu = ptr;
do {
cout<<cu->item<<" ";
cu = cu->next;
}while(cu != ptr);
cout<<endl;
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯