我想做的是修改简单的链表,到目前为止我有一个输入名称的功能,然后以相反的顺序输出,但是我在特定位置更改字符时遇到问题。
主要代码:
#include "list.h"
#include "string"
using namespace std;
int main(){
cout<<"What is your name \n";
string name;
getline(cin,name);
Node *list;
list = new_list();
for (int i=0; i<name.length();i++){
insert_front(&list,name[i]);
}
print_list(list);
int p;
char x;
cout<<"Change the position: ";
cin>>p;
cout<<"\n to the character " ;
cin>>x;
change_char(list, x, p);
print_list(list);
return 0;
}
和我的 list.cpp
#include "list.h"
Node* new_list(){
Node* list = 0;
return list;
}
void insert_front(Node** plist,char x){
Node* t;
t = new Node;
t->x = x;
t->next = *plist;
*plist = t;
return;
}
void print_list(Node* list){
Node* p;
p = list;
if(p == 0)
cout << "--- empty list ---" << endl;
while(p !=0){
cout << p->x<<" -- ";
p = p->next;
}
cout << endl;
}
void delete_front(Node** plist){
Node* t;
if( !is_empty(*plist) ){
t = (*plist)->next;
*plist = (*plist)->next;
delete t;
}
}
void delete_list(Node** plist){
while( !is_empty(*plist) )
delete_front(plist);
}
bool is_empty(Node* list){
return (list == 0);
}
void change_char(Node* plist, char x, int p){
Node* s;
s=(plist->next);
cout<<s<<endl;
cout<<plist<<endl;
return;
我很想知道如何在特定位置改变角色。change_char 给了我一个地址,但不是一个列表,我不确定出了什么问题。
另外,字符的变化与字符的插入和删除有何不同。谢谢你的帮助