[英]Why am I getting segmentation fault while implementing linked list?
在此功能中,我遇到了分割錯誤。 我認為這與內存分配有關。 我犯什么錯誤?
現在,如果我初始化Node * a = NULL,那么最后我的頭指針將變為NULL。
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
Node* addTwoLists(Node* first, Node* second) {
// Code here
Node *a;
Node *head = a;
int bor = 0;
while(first->next && second->next) {
int ans = first->data + second->data;
a = new Node((ans%10)+bor);
bor = ans/10;
a=a->next;
first = first->next;
second = second->next;
}
return head;
}
a
未初始化。 你不能使用a
,直到你分配一個值 head
,所以再也沒有其他東西了。 不是分配,而是錯誤的指針使用。
這是它的外觀。 此代碼維護一個變量last
,這是添加到列表中的最后一個節點。 您需要此變量,以便可以在列表的末尾。 您顯然試圖自己進行此操作,但邏輯錯誤。
Node* addTwoLists(Node* first, Node* second) {
Node *last = NULL;
Node *head = NULL;
int bor = 0;
while(first->next && second->next) {
int ans = first->data + second->data;
Node* a = new Node((ans%10)+bor);
if (head == NULL) {
head = last = a; // first node, update head and end of list
}
else {
last->next = a; // add a to the end of the list
last = a; // update the end of the list
}
bor = ans/10;
first = first->next;
second = second->next;
}
return head;
}
未經測試的代碼。
對於起動器,可變head
值不確定,並且在功能中不會更改。
Node *a;
Node *head = a;
更改變量a
並不意味着更改表達式a->next
的值。
// ...
a = new Node((ans%10)+bor);
//...
a=a->next;
該函數可以通過以下方式編寫(無需測試)
Node * addTwoLists( const Node *first, const Node *second )
{
const int Base = 10;
Node *head = nullptr;
int bor = 0;
Node **current = &head;
for ( ; first != nullptr && second != nullptr; first = first->next, second = second->next )
{
int sum = first->data + second->data + bor;
*current = new Node( sum % Base );
bor = sum / Base;
current = &( *current )->next;
}
if ( bor )
{
*current = new Node( bor );
}
return head;
}
這是一個示范節目
#include <iostream>
struct Node
{
explicit Node( int data, Node *next = nullptr ) : data( data ), next( next )
{
}
int data;
Node *next;
};
void push_front( Node **head, int x )
{
*head = new Node( x, *head );
}
Node * addTwoLists( const Node *first, const Node *second )
{
const int Base = 10;
Node *head = nullptr;
int bor = 0;
Node **current = &head;
for ( ; first != nullptr && second != nullptr; first = first->next, second = second->next )
{
int sum = first->data + second->data + bor;
*current = new Node( sum % Base );
bor = sum / Base;
current = &( *current )->next;
}
if ( bor )
{
*current = new Node( bor );
}
return head;
}
std::ostream & display_list( const Node *head, std::ostream &os = std::cout )
{
for ( ; head != nullptr; head = head->next )
{
os << head->data << ' ';
}
return os;
}
int main()
{
const int N = 10;
Node *list1 = nullptr;
Node *list2 = nullptr;
for ( int i = 1; i < N; i++ ) push_front( &list1, i );
for ( int i = N; --i != 0; ) push_front( &list2, i );
display_list( list1 ) << '\n';
display_list( list2 ) << '\n';
Node *list3 = addTwoLists( list1, list2 );
display_list( list3 ) << '\n';
}
它的輸出是
9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9
0 1 1 1 1 1 1 1 1 1
您可能會因為各種原因而遇到細分錯誤。
first
或second
為NULL ,則將出現分段錯誤。 因此,請確保如果這兩個節點都不為NULL 。 而且,根據需要, head
變量應包含答案列表的起始節點,因此,只要列表開始,就需要分配節點。
只需在a = new Node((ans%10)+bor);
之后添加此行即可a = new Node((ans%10)+bor);
if(head == NULL) head = a;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.