C++ 如何创建字符数组的LLL
似乎每个指南都是如何使用int创建LLL,但我在使用char指针时遇到了麻烦。当我运行这段代码时,它会立即出现故障 这是到目前为止我的代码C++ 如何创建字符数组的LLL,c++,C++,似乎每个指南都是如何使用int创建LLL,但我在使用char指针时遇到了麻烦。当我运行这段代码时,它会立即出现故障 这是到目前为止我的代码 struct node { char * data; node * next; }; void build(node * head);//create list void manipulate(node * & head);//manipulate list void display(node * head);
struct node
{
char * data;
node * next;
};
void build(node * head);//create list
void manipulate(node * & head);//manipulate list
void display(node * head);//display all
void delete_list(node * head);//delete all nodes in linked list
bool again();//asks user if they'd like to continue
int main()
{
node * head = NULL;
//create list from user1 input
while(again)
build(head);
//displays list
display(head);
//manipulate list as user2 reads through it
manipulate(head);
return 0;
}
void build(node * head)
{
head->next = new node;
char * data = new char;
cout << "where to visit? ";
cin.get(head->data,strlen(data)+1,'\n');
head = head->next;
}
struct节点
{
字符*数据;
节点*下一步;
};
无效构建(节点*头部)//创建列表
无效操作(节点*&头部)//操纵列表
无效显示(节点*头部)//全部显示
作废删除列表(节点*头)//删除链接列表中的所有节点
再次bool()//询问用户是否要继续
int main()
{
node*head=NULL;
//从user1输入创建列表
同时(再次)
建立(头);
//显示列表
显示器(头部);
//在user2读取列表时对其进行操作
操纵(头部);
返回0;
}
无效构建(节点*头部)
{
head->next=新节点;
字符*数据=新字符;
cout数据,strlen(数据)+1',\n');
头部=头部->下一步;
}
我假设这个代码:
while(again)
build(head);
应该是(再次调用而不是将其与零进行比较):
无论哪种方式,第一次通过循环head
的时间都为空。但是build
仍然继续使用它:
head->next = new node;
这里,使用next
将产生段故障,因为head
为空。您正在访问内存中的无效位置。调试器说什么是LLL??请解释…您在循环中再次计算,而不是调用它。它的计算结果总是true
。如果我不得不猜测,我会假设LLL是线性链表,对吗?是的,线性链表。对不起,这是我的老师使用的术语,我想这是常用的
head->next = new node;