C++在链表中插入和删除字符
C++ insert and delete characters in a linked list
我的问题是我正在尝试根据用户在 int h 给出的特定位置输入将一个字母插入链表中......但是,每次我运行程序时,无论用户放入程序的数字如何,只有列表中的第二个字符会更改。
Example:
./h
Name: koala
a<-l<-a<-o<-k
Change the position: 2
To the character: 3
a<-l<-3<-o<-k
Insert the Character: F
To the Postion: 3
a<-F<-l<-3<-o<-k
我希望它看起来像。
./h
Name: koala
a<-l<-a<-o<-k
Change the position: 2
To the character: 3
a<-l<-3<-o<-k
Insert the Character: F
To the Postion: 3
a<-l<-3<-F<-o<-k
我知道我的问题出在我的列表中的 insert_char() 函数中.cpp但就是无法弄清楚我做错了什么......
列表.h
#include <iostream>
using namespace std;
struct Node;
Node* new_list();
void insert_front(Node** plist,char x);
void insert_char(Node* plist, char x, int p);
void change_char(Node* plist, char x, int p);
void print_list(Node* list);
void delete_front(Node** plist);
void delete_list(Node** plist);
//void delete_char(Node* plist,int p);
struct Node {
char x;
Node *next;
};
主.cpp
struct Node {
char x;
Node *next;
};
int main(){
Node *list;
list = new_list(); //new empty list
cout<<"Name: ";
string name;
cin>> name;
for (int i =0; i < name.length(); i++)
insert_front(&list, name[i]);
//---------print list-------------------------
print_list(list);
cout <<"Change the position: ";
int z;
cin>> z;
cout<< "To the character: " ;
char x;
cin>> x;
change_char(list, x, z);
print_list(list);
cout <<"Insert the Character: ";
char y;
cin>> y;
cout<< "To the Postion: ";
int h;
cin>> h;
insert_char(list, y, h);
print_list(list);
return 0;
}
列表.cpp
Node* new_list()
{
Node* list = 0; //in C++ it is better to use 0 than NULL
return list;
}
void insert_front(Node** plist,char x){
Node* t;
t = new Node;
t->x = x;
t->next = *plist;
*plist = t;
return;
}
void change_char(Node* plist, char x, int p)
{
Node* s = plist;
for (int i=1; i<p && 0!=s;i++)
s = s->next;
if (0 != s)
s->x = x;
return;
}
void insert_char(Node* plist, char x, int p){
Node* s = plist;
Node* a = new Node();
for (int i=1; i<p && s; i++)
a->next=s->next;
s->next=a;
if (0 !=s)
a->x=x;
return;
}
//void delete_char(Node* plist,int p)
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( *plist != 0){ // list is not empty
t = (*plist);
*plist = (*plist)->next;
delete t;
}
}
void delete_list(Node** plist){
while(*plist != 0) //while list not empty
delete_front(plist);
}
bool is_empty(Node* list){
return (list == 0);
}
insert_char
中的 for 循环只是一遍又一遍地"插入"字符。 我认为您的意思是将s
推进到插入的起点(由h
确定)。
更新:
这部分是错误的,原因如下:
for (int i=1; i<p && s; i++)
a->next=s->next;
s->next=a;
请注意,缩进具有误导性。 由于您没有大括号,因此只有中间线是循环的一部分。 实际上,您已经编写了:
for (int i=1; i<p && s; i++) {
a->next=s->next;
}
s->next=a;
就个人而言,我总是在块上使用大括号,即使它们只包含一个语句。
因此,您设置了很多次a->next
,而不是前进到您想要的列表中的位置。
您需要前进到您希望新元素在循环中的位置,然后执行实际插入。
// Advance s to index p.
for (int i = 1; i < p && s->next; i++) {
s = s->next;
}
// Insert a at s.
a->next = s->next;
s->next = a;
相关文章:
- 正在从C++中的字符数组中删除字符
- 从字符串中删除字符
- 如果不是十六进制,则QT从qlineedit中删除字符
- 从长字符串中删除字符
- 从文件对象的当前位置删除字符
- C++ 删除字符**
- 从字符串中删除字符的第一个和最后一个实例
- 如何在C++中删除字符*数组
- 如何从C 中的字符串中删除字符
- 用“ strcpy”在2D数组中删除(字符)的一行(字符)时是否存在任何错误
- 删除字符并在字符串(C )的末端附加它们
- C++ 字符串,用于删除字符'b'并将字符'a'替换为两个'd'的构建函数
- 在字符串中的定界符之间删除字符
- 从 std::字符串中删除字符从 "(" 到 ")" 用擦除?
- 使用擦除和删除从字符串中删除字符
- C++ 如何删除字符中的双引号
- C++ 中的密码 |打印字符后删除字符
- C++在链表中插入和删除字符
- 字符串流的获取函数不删除字符
- 删除字符C++最后一次出现时字符数组的开头