在 C++ 的链表中添加新节点时程序崩溃
Program crashes while adding a new node in linked list in c++
我正在尝试实现链接的插入功能,但一旦我添加第三个元素,程序就会崩溃并停止执行,即使相同的代码在 hackerrank 的编译器上工作也是如此。
这是我的代码。
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node * next;
Node(int data){
this -> data = data;
this -> next = nullptr;
}
};
Node * insert_tail(Node * head, int data){
Node * node = new Node(data);
if(head == nullptr) return node;
Node * current = head;
while(head -> next != nullptr) current = current -> next;
current -> next = node;
return head;
}
void print_linkedlist(Node * head){
while(head -> next != nullptr){
cout << head -> data << " -> ";
head = head -> next;
}
cout << head -> data << " -> nullptr";
}
int main(){
Node * head = nullptr;
head = insert_tail(head, 1);
head = insert_tail(head, 5);
head = insert_tail(head, 3);
head = insert_tail(head, 5);
head = insert_tail(head, 8);
head = insert_tail(head, 17);
print_linkedlist(head);
return 0;
}
行
while(head -> next != nullptr) current = current -> next;
函数中的insert_tail
是错误的。当 head->next
不是 nullptr
.
时,它将无休止地 运行
应该是
while(current -> next != nullptr) current = current -> next;
这里有错字
while(head -> next != nullptr) current = current -> next;
^^^^^^^^^^^^
写
while(current -> next != nullptr) current = current -> next;
^^^^^^^^^^^^
函数的另一种定义如下所示,
void insert_tail( Node * &head, int data )
{
Node **current = &head;
while ( *current ) current = &( *current )->next;
*current = new Node( data );
}
并且可以像
一样简单地调用该函数
insert_tail(head, 1);
函数print_linkedlist
也可以写成
std::ostream & print_linkedlist( const Node * head, std::ostream &os = std::cout )
{
for ( ; head; head = head->next )
{
os << head -> data << " -> ";
}
return os << "nullptr";
}
可以这样称呼
print_linkedlist(head) << '\n';
我正在尝试实现链接的插入功能,但一旦我添加第三个元素,程序就会崩溃并停止执行,即使相同的代码在 hackerrank 的编译器上工作也是如此。
这是我的代码。
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node * next;
Node(int data){
this -> data = data;
this -> next = nullptr;
}
};
Node * insert_tail(Node * head, int data){
Node * node = new Node(data);
if(head == nullptr) return node;
Node * current = head;
while(head -> next != nullptr) current = current -> next;
current -> next = node;
return head;
}
void print_linkedlist(Node * head){
while(head -> next != nullptr){
cout << head -> data << " -> ";
head = head -> next;
}
cout << head -> data << " -> nullptr";
}
int main(){
Node * head = nullptr;
head = insert_tail(head, 1);
head = insert_tail(head, 5);
head = insert_tail(head, 3);
head = insert_tail(head, 5);
head = insert_tail(head, 8);
head = insert_tail(head, 17);
print_linkedlist(head);
return 0;
}
行
while(head -> next != nullptr) current = current -> next;
函数中的insert_tail
是错误的。当 head->next
不是 nullptr
.
应该是
while(current -> next != nullptr) current = current -> next;
这里有错字
while(head -> next != nullptr) current = current -> next;
^^^^^^^^^^^^
写
while(current -> next != nullptr) current = current -> next;
^^^^^^^^^^^^
函数的另一种定义如下所示,
void insert_tail( Node * &head, int data )
{
Node **current = &head;
while ( *current ) current = &( *current )->next;
*current = new Node( data );
}
并且可以像
一样简单地调用该函数insert_tail(head, 1);
函数print_linkedlist
也可以写成
std::ostream & print_linkedlist( const Node * head, std::ostream &os = std::cout )
{
for ( ; head; head = head->next )
{
os << head -> data << " -> ";
}
return os << "nullptr";
}
可以这样称呼
print_linkedlist(head) << '\n';