为什么在会出现这种情况啊,在里面就是正常字符串,后面输出就是烫
#include
#include
#include
using namespace std;
struct Student {
const char* name;
Student* next;
};
Student* head;
Student* getNode() {
char nam[20];
cin >> nam;
const char* na = nam;
const char* b = "0";
if (strcmp(na, b) == 0)
return NULL;
Student* p = new Student;
p->name = na;
cout << p->name;
p->next = 0;
return p;
}
void Create()
{
cout << "请输入姓名(以0结束):" << endl;
if ((head=getNode()) == 0)
return;
for (Student* pe = head, ps; ps = getNode(); pe = ps)
pe->next = ps;
}
void ShowList() {
cout << "链表中的元素依次为:" << endl;
for (Student* p = head; p; p = p->next)
cout << p->name << "";
cout << endl;
}
void Insert(Student& stud)
{
Student* ps = new Student;
*ps = stud;
if (!head || head->name == "jone")
{
ps->next = head;
head = ps;
return;
}
Student pg = head;
for (Student* p = pg->next; p; pg = p, p = p->next)
if (p->name == "jone") {
ps->next = p;
pg->next = ps;
return;
}
pg->next = ps;
ps->next = NULL;
}
int task55() {
Create();
Student ps = { "Marit" };
Insert(ps);
ShowList();
return 0;
}
检查一下输入的字符串是不是超出了字符数组长度,把\0覆盖了
或者只是指向了数组名没有加[]访问数组元素
很简单,空指针问题