2013-04-24 46 views
0

我知道这对C++程序员来说可能是微不足道的,但我是一个努力弄清楚这一点的noobie。在我的主要中,如果我手动打印我的短列表(例如< < < head-> value等),但是当我使用我的打印功能时,我会遇到分段错误。我一直在尝试使用一个调试器,但是我对unix/C++不是很擅长,而且我正在试图解决这个问题时感到沮丧。当我试图通过我的函数打印我的列表时,出现了段错误

#include <iostream> 
using namespace std; 

class ListNode 
{ 
    public: 
    int value; 
    ListNode* next; 
}; 

void insertAtHead(ListNode** head, int value) 
{ 
    ListNode *newNode = new ListNode; 
    newNode->value = value; 
    if(head == NULL) 
    { 
     *head = newNode; 
     newNode->next = NULL; 
    } 
    else 
    { 
     newNode->next = *head; 
     *head = newNode; 
    } 
} 

void printList(ListNode* head) 
{ 
    while(head != NULL) 
    { 
     cout << head->value << "->"; 
     head = head->next; 
    } 
} 
//inserts after the node with given value 
void insertAfterNode(ListNode** head,ListNode** newNode, int value) 
{ 
    ListNode* current = *head; 
    while(current != NULL && (current->value != value)) 
    { 
      //cout << "Im Here"; 
      current = current->next; 
      cout << current->value; 
    } 
    (*newNode)->next = current->next; 
    current->next = *newNode; 
} 

int main() 
{ 
    ListNode *head; 
    insertAtHead(&head, 5); 
    insertAtHead(&head, 10); 
    ListNode* newNode = new ListNode; 
    newNode->value = 8; 
    newNode->next = NULL; 
    insertAfterNode(&head,&newNode, 5); 
printList(head); 
} 
+0

我不认为这是按原样编译的。 'head'是一个指向'ListNode'的双指针,你几乎不能使用'operator->'来访问它的成员。 – 2013-04-24 04:30:22

+0

对不起,我正在编辑我的代码。让我解决它。我最初使用本地副本进行打印,并认为这可能是问题所在,但忘记将其改回。 – user2285010 2013-04-24 04:32:16

+1

@ user2285010最好在'ListNode'中添加一个构造函数,将所有成员变量初始化为0. – 2013-04-24 04:49:16

回答

2

入住此修改你的功能

void insertAtHead(ListNode** head, int value) 
{ 
    ListNode *newNode = new ListNode; 
    newNode->value = value; 

    newNode->next = *head; 
    *head = newNode; 
} 

void printList(const ListNode* head) 
{ 
    while(head != NULL) 
    { 
     cout << head->value << "->"; 
     head = head->next; 
    } 
} 

insertAtHead你打一个双指针,所以比较应该是这样的。

在访问之前,检查*head是否为空。如果null增加新的节点作为head

void insertAfterNode(ListNode** head,ListNode** newNode, int value) 
{ 
    ListNode* current = *head; 
    if (current != NULL) 
    { 
     while(current != NULL && (current->value != value)) 
     { 
      //cout << "Im Here"; 
      current = current->next; 
      cout << current->value; 
     } 
     (*newNode)->next = current->next; 
     current->next = *newNode; 
    } 
    else 
    { 
     *head = *newNode; 
    } 
} 

而且在主intialise head使用前

int main() 
{ 
    ListNode *head = NULL; 
    insertAtHead(&head, 5); 
    printList(head); // <== note: by-value, not by address or reference. 
+0

huzzah!谢谢!我很抱歉,我错过了,但是,即使我知道,如果我没有在我的主要设置头为NULL是错误的? – user2285010 2013-04-24 04:51:04

+1

为什么它的价值, 'insertAtHead()'中的'if(* head == NULL)'条件,以及包含'* head = newNode; newNode-> next = NULL;'的块是完全没有必要的,你可以简单地设置'newNode- > next = * head; * head = newNode;'检查这是否是一个dbl链表是非常重要的,但事实并非如此。另外,为了鲁棒性,两个*函数都应该确保基地指针head在初始化前是非NULL,然后用'head'进行解引用。 – WhozCraig 2013-04-24 05:08:31

+0

@WhozCraig感谢您的评论。代码编辑 – 999k 2013-04-24 05:17:14

0

你需要检查,如果你要访问的下一个值不为空这样的:

void printList(ListNode* head) 
    { 
     if (head != NULL) 
     { 
      while(head->next != NULL) 
      { 
       cout << head->value << "->"; 
       head = head->next; 
      } 
     } 
    } 
+0

嗯,这不适合我。仍然遇到分段错误。我也调整了参数以使用本地副本ie(ListNode * head) – user2285010 2013-04-24 04:36:37

+0

请检查我的新更新,同样,本地副本也是一个好主意;) – Pol0nium 2013-04-24 04:37:23

+0

这可能是我的insertAfterNode错了......看起来我可以'如果我使用此功能打印。 – user2285010 2013-04-24 04:38:21

0

哥们第一个答案是正确的

但我想再拍校正

在你的while循环中的函数中插入节点

current!=NULL不正确,因为那么你的条件为真当且仅当在列表中的最后一个节点5

条件的值相匹配应该只是while(current->value!=value)

这个你会到达具有节点值5

+0

它是当前!=空。这是你的意思吗? – user2285010 2013-04-24 05:17:07

+0

这完全是错误的。 while循环永远不会违反这个逻辑,并且如果在列表中搜索的值是* not *,则可以在进程中快速点击* undefined behavior *。 – WhozCraig 2013-04-24 05:27:42

+0

是的,但如果价值在那里,它会工作,我们可以把其他条件,如果价值不存在,那么用户必须再次输入 – 2013-04-24 10:22:21

相关问题