【问题标题】:Get the last element error in a C double linked list获取 C 双链表中的最后一个元素错误
【发布时间】:2011-08-16 17:42:25
【问题描述】:

我尝试用 C 写一个双链表。现在我写一个getLast 元素函数:

Dlist* getLast(Dlist **list)
{
    if (list != NULL)
    {
        while((*list) != NULL)
            (*list) = (*list)->next;
    }
    return (*list);
}

我收到segmentation fault

程序收到信号SIGSEGV,分段错误。 src/dlist.c:29 处的 getLast (list=0x804a008) 中的 0x080485ce 29 (*list) = (*list)->下一个;

我添加一个元素,就可以了。当我尝试添加第二个元素时,出现分段错误。

我这样称呼这个函数:

Dlist* addItemAtStart(Dlist** list, Pair* value)
{
    Dlist* last = NULL;
    last = getLast (*list);
    ...
}

怎么了?

【问题讨论】:

    标签: c list pointers linked-list


    【解决方案1】:

    您的代码返回一个 NULL 指针。

    while(*list->next != NULL)
    

    【讨论】:

      【解决方案2】:

      您正在破坏所有列表指针。您的许多问题都源于不接受基本列表结构。列表它的第一个元素 - 你不需要将它表示为指向第一个元素的指针。

      一些代码来说明?

      DIList * myList ; // This is your list, add elements to it
      
      DIList * lastElement = getLast(myList); // Last element in your list, also a list
      
      DIList * getLast(DIList * aList) {
          if(aList == NULL) return NULL;
      
          DIList * aNode = aList;
          while(aNode->next != NULL) aNode = aNode->next;
      
          return aNode;
      }
      

      【讨论】:

        【解决方案3】:

        您需要将列表指针存储在一个临时变量中,这样您就不会破坏您的列表(或其他内存):

        Dlist* getLast(Dlist **list)
        {
          if (list != NULL)
          {
              Dlist *ptr = *list;
              if (ptr == NULL)
                  return NULL;
        
              while(ptr->next != NULL)
                  ptr = ptr->next;
        
              return ptr;
          }
          return NULL;
        }
        

        【讨论】:

        • 感谢您的回复。但我得到 segfault - 程序收到信号 SIGSEGV,分段错误。 0x0804858c 在 getLast (list=0x804a008) at src/dlist.c:29 29 ptr = ptr->next;
        • Dlist* getLast(Dlist **list) { return NULL; }
        • 是的,是的,已修复。我昨晚睡了大约三个小时。
        【解决方案4】:

        在 addItemAtStart 中,你为什么使用:

            last = getLast (*list);
        

        代替:

            last = getLast(list);
        

        另外,在 getLast 中,您不应该使用:

            while((*list)->next != NULL)
        

        代替:

            while((*list) != NULL)
        

        【讨论】:

          【解决方案5】:

          下次您的代码出现问题时,请提供您的所有代码,而不仅仅是其中的一部分!

          #include <stdio.h>
          #include <stdlib.h>
          
          struct node{
              void* value;
              struct node *prev, *next;
          };
          /*This Function is Unnecessary
          struct node * createList(){
              struct node *newNode=malloc(sizeof(struct node));
              newNode->value = NULL;
              newNode->next = NULL;
              newNode->prev = NULL;
                    
              return newNode;
          }
          */
          /*
           *This Function is also Unnecessary
           *Get last element from Dlist
          struct node* getLast(struct node* node){
              while(node){
                  if (node->next==NULL) break;
                  node=node->next;
              }
              
              return node; 
          }
          */
          /*add element to list at start*/
          void addItemAtStart(struct node **head, void *value){
              struct node *newNode=malloc(sizeof(struct node));
              
              if (newNode==NULL) return;
              
              newNode->value=value;
              newNode->prev=NULL;
              newNode->next=*head;
              
              if (*head!=NULL) (*head)->prev=newNode;
              *head=newNode;
          }
          
          int main(){
              struct node *list=NULL;
              
              addItemAtStart(&list, "apple");
              addItemAtStart(&list, "lemon");
              addItemAtStart(&list, "orange");
              addItemAtStart(&list, "peach");
              
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多