双链表在C中通过引用预先结束元素
我试着用C写双链表 这是我的实现:双链表在C中通过引用预先结束元素,c,list,data-structures,linked-list,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;
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");
谢谢。如果您通过指向列表的第一个元素来处理列表,也许您应该使用双间接寻址:
void addItemAtStart(Dlist** plist, Pair* value)
{
// replace all list with *plist
}
addItemAtStart(&list, "Hello");
如果通过指向列表的第一个元素来处理列表,可能应该使用双间接寻址:
void addItemAtStart(Dlist** plist, Pair* value)
{
// replace all list with *plist
}
addItemAtStart(&list, "Hello");
您可以编写函数来接受指向列表指针的指针:
Dlist* addItemAtStart(Dlist** list, Pair* value)
使用列表
时,只需确保在addItemAtStart
中添加另一级间接寻址即可
可以使用
addItemAtStart(&list, "Hello");
您可以编写函数来接受指向列表指针的指针:
Dlist* addItemAtStart(Dlist** list, Pair* value)
使用列表
时,只需确保在addItemAtStart
中添加另一级间接寻址即可
可以使用
addItemAtStart(&list, "Hello");
为了实现这一点,您需要创建一个静态链表节点指针。
确保atitematstart方法更新该节点。要实现这一点,您需要创建一个静态链表节点指针。
确保atitematstart方法更新该节点。在开始时提供头节点的引用以插入节点
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
在开始时为插入节点提供头节点的引用
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
使用全局列表将解决您的问题。在函数中,将值指定给列表对象。我已经试过了。使用全局列表可以解决您的问题。在函数中,将值指定给列表对象。我已经试过了。您的代码中包含了一些更正和内容:
#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;
}
#包括
#包括
结构节点{
无效*值;
结构节点*prev,*next;
};
/*这个功能是不必要的
结构节点*createList(){
结构节点*newNode=malloc(sizeof(结构节点));
if(newNode==NULL)返回NULL;
newNode->value=NULL;
newNode->next=NULL;
newNode->prev=NULL;
返回newNode;
}
*/
/*
*这个功能也是不必要的
*从Dlist获取最后一个元素
结构节点*getLast(结构节点*node){
如果(node==NULL)返回NULL;
而(node->next!=NULL)node=node->next;
返回节点;
}
*/
/*
*描述:在开始时将元素添加到列表中
*
*返回值:
*0:失败
*1:成功了
*/
int addItemAtStart(结构节点**头,空*值){
结构节点*newNode=malloc(sizeof(结构节点));
/*有时malloc无法分配内存并返回NULL,因此您必须检查它,如果malloc无法分配内存,则必须退出该函数*/
if(newNode==NULL)返回0;
新建节点->值=值;
newNode->prev=NULL;
新建节点->下一步=*头部;
如果(*head!=NULL)(*head)->prev=newNode;
*头=新节点;
返回1;
}
int main(){
结构节点*list=NULL;
addItemAtStart(&list,“apple”);
addItemAtStart(&list,“lemon”);
addItemAtStart(&list,“橙色”);
addItemAtStart(&list,“peach”);
返回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;
}
#包括
#包括
结构节点{
无效*值;
结构节点*prev,*next;
};
/*这个功能是不必要的
结构节点*createList(){
结构节点*newNode=malloc(sizeof(结构节点));
if(newNode==NULL)返回NULL;
newNode->value=NULL;
newNode->next=NULL;
newNode->prev=NULL;
返回newNode;
}
*/
/*
*这个功能也是不必要的
*从Dlist获取最后一个元素
结构节点*getLast(结构节点*node){
如果(node==NULL)返回NULL;
而(node->next!=NULL)node=node->next;
返回节点;
}
*/
/*
*描述:在开始时将元素添加到列表中
*
*返回值:
*0:失败
*1:成功了
*/
int addItemAtStart(结构节点**头,空*值){
结构节点*newNode=malloc(sizeof(结构节点));
/*有时malloc无法分配内存并返回NULL,因此您必须检查它,如果malloc无法分配内存,则必须退出该函数*/
if(newNode==NULL)返回0;
新建节点->值=值;
newNode->prev=NULL;
新建节点->下一步=*头部;
如果(*head!=NULL)(*head)->prev=newNode;
*头=新节点;
返回1;
}
int main(){
结构节点*list=NULL;
addItemAtStart(&list,“apple”);
addItemAtStart(&list,“lemon”);
addItemAtStart(&list,“橙色”);
addItemAtStart(&list,“peach”);
返回0;
}
您必须考虑全局“列表”对象是什么。您定义的实际上更多的是“列表节点”,而不是列表本身。你如何处理列表本身?另外,如果你有一个列表对象,你可以存储一个指向最后一个列表节点的指针,这样你就不必迭代来找到它。你必须考虑你的全局“列表”对象是什么。您定义的实际上更多的是“列表节点”,而不是列表本身。您如何处理列表本身?此外,如果您有一个列表对象,您可以存储指向最后一个列表节点的指针,这样您就不必迭代查找它。这将限制您一次只能找到一个列表,这有些不必要。这将限制您一次只能找到一个列表,这有些不必要。