【问题标题】:Double linked list prepend element by ref in C双链表在C中通过ref添加元素
【发布时间】:2011-08-14 17:23:23
【问题描述】:

我尝试用 C 编写双链表。

这是我的实现:

   typedef struct
    {
        void* value;
        struct Dlist* prev;
        struct Dlist* next;
    } Dlist;

    Dlist* createDlist()
    {
      Dlist* newList = (Dlist*)malloc (sizeof(Dlist));
      newList->value = NULL;
      newList->next = NULL;
      newList->prev = NULL;
      return newList;
    }

    /*
     * Get last element from Dlist
     */
    Dlist* getLast(Dlist* list)
    {
      if (list)
      {
          while(list->next)
            list = (Dlist*)list->next;
      }
      return list;
    }

    /*
     * add element to list at start
     */
    Dlist* addItemAtStart(Dlist* list, Pair* value)
    {
      Dlist* newList = NULL;
      Dlist* last = NULL;

      newList = createDlist ();
      newList->value = value;

      if (list)
      {
         last = getLast(list);
         last->next = newList;
         newList->prev = last;

         return list;
      }
      else
        return newList;
    }

现在,当我尝试向列表中添加元素时,我每次都需要分配一个新值:

list = addItemAtStart(list, "Hello");

但我只想要

addItemAtStart(list, "Hello");

如果没有list =,我怎样才能使列表在没有分配的情况下发生变化?

p.s.我得到了Dlist* addItemAtStart(Dlist **list, void* value)的segfaut

我尝试这样插入:

  Dlist **list = NULL;
  addItemAtStart(&list, "Hello");

谢谢。

【问题讨论】:

  • 您必须考虑您的全局“列表”对象是什么。您定义的实际上更像是一个“列表节点”,而不是列表本身。您如何处理列表本身?
  • 另外,如果您有一个列表对象,您可以存储一个指向最后一个列表节点的指针,这样您就不必反复查找它。

标签: c list data-structures linked-list


【解决方案1】:

如果你通过指向它的第一个元素来处理列表,也许你应该使用双重间接:

void addItemAtStart(Dlist** plist, Pair* value)
{
    // replace all list with *plist
}

addItemAtStart(&list, "Hello");

【讨论】:

    【解决方案2】:

    您可以编写函数来接受指向列表指针的指针:

     Dlist* addItemAtStart(Dlist** list, Pair* value)
    

    在使用list 时,请确保在addItemAtStart 中添加另一个间接级别。

    函数可以调用

    addItemAtStart(&list, "Hello");
    

    【讨论】:

      【解决方案3】:

      在开始时提供头节点的引用以插入节点。

      Dlist* addItemAtStart(Dlist** list, Pair* value)
          {
            Dlist* newList = NULL;
            Dlist* last = NULL;
      
            newList = createDlist();
            newList->value = value;
      
            if (list)
        {
           last = getLast(*list);
           last->next = newList;
           newList->prev = last;
      
      
        }
        else
          *list = newList
      

      【讨论】:

        【解决方案4】:

        为此,您需要创建一个静态链表节点指针。 确保 atitematstart 方法更新该节点。

        【讨论】:

        • 这将您一次限制在一个列表中,这有点不必要。
        【解决方案5】:

        使用全局列表将解决您的问题。在函数中,将值分配给列表对象。我试过了。

        【讨论】:

          【解决方案6】:

          您的代码有一些更正和包含:

          #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));
          
              if (newNode==NULL) return NULL;
          
              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){
              if (node==NULL) return NULL;
          
                  while (node->next!=NULL) node=node->next;
              
                  return node; 
          }
          */
          /*
          *Description: add element to list at start
          *
          *Return Values:
          *0:Failed
          *1:Succeeded
          */
          int addItemAtStart(struct node **head, void *value){
              struct node *newNode=malloc(sizeof(struct node));
              /*Sometimes malloc can't allocate memory and returns NULL so you have to check it and if malloc couldn't allocate memory you have to exit the function*/
              if (newNode==NULL) return 0;
              
              newNode->value=value;
              newNode->prev=NULL;
              newNode->next=*head;
          
              if (*head!=NULL) (*head)->prev=newNode;
              *head=newNode;
          
              return 1;
          }
          
          int main(){
              struct node *list=NULL;
              
              addItemAtStart(&list, "apple");
              addItemAtStart(&list, "lemon");
              addItemAtStart(&list, "orange");
              addItemAtStart(&list, "peach");
              
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 2017-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-16
            • 2013-12-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多