双链表在C中用ref前缀元素

时间:2011-08-14 17:23:24

标签: c list data-structures linked-list

我尝试在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");

谢谢。

6 个答案:

答案 0 :(得分:2)

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

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

addItemAtStart(&list, "Hello");

答案 1 :(得分:2)

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

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

使用addItemAtStart时,请确保在list内添加其他级别的间接。

可以使用

调用该函数
addItemAtStart(&list, "Hello");

答案 2 :(得分:2)

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

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

您的代码经过更正和包含:

#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;
}