gpt4 book ai didi

c - 为什么我在 C 中的链表失败了?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:52:09 26 4
gpt4 key购买 nike

我打算编写一个简单的 SDL 应用程序,所以我认为我应该实现一个简单的链表,以便稍后扩展。现在,经过一番挣扎之后,我决定在这里寻求帮助。

我知道链表,我读过它。我看到了几个实现,不是那么难,容易掌握。所以我想我应该能够自己实现一个。我猜我严重忽略了这里的某些东西。 :(

首先这段代码编译不好,段错误。其次,不幸的是,如果我能够克服使用指针所犯的基本错误,我认为它不会产生我想要的结果。

你能告诉我我的代码中的错误吗?我哪里做错了?

这是一个用于编辑的实时工作区链接 http://liveworkspace.org/code/1qu1RN $1

这是我在 Cygwin gcc 上遇到的错误

temp [v 0] -> 0.460241 0.000000 0.050356
temp [v 1] -> 0.072079 0.153807 0.864573
temp [v 2] -> 0.683917 0.709301 0.080479
temp [v 3] -> 0.295755 0.264795 0.894696
address of ptemp @ 1629101750

[v 0] -> linked vertex is 7731159828445146316135105405324534341657178000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 578395405356271.125000 1310047834864691498231654014730274851713450000.000000
Segmentation fault (core dumped)

这是代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NEWline printf("\n")

typedef struct position {
double x;
double y;
double z;
} position;

struct datalist {
position v;
struct datalist *pnext;

};
typedef struct datalist datalist;

void initializeList (datalist *head);
void addList_II (datalist *head , double data[3] );

void initializeList (datalist *head){
position pv= {0,0,0};
head = (datalist *) malloc ( sizeof (head) );
if (head == NULL){ printf("cannot allocate memory"); exit(1); }
head->v=pv;
head->pnext=NULL;
}

void addList_II ( datalist *head , double data[3] ) {

datalist *adddata;
adddata = (datalist *)malloc ( sizeof (datalist) );
if (adddata == NULL) { printf("cannot allocate memory for insertion data"); exit(1); }
adddata->v.x = data[0];
adddata->v.y = data[1];
adddata->v.z = data[2];

adddata->pnext = head->pnext;
head=adddata;
}

int main() {

datalist *ptemp;
datalist temp;
int i,c; //counters
datalist *head;

initializeList(head);

for ( i = 0; i < 4; i++ ) {
double t[3];
srand ( time ( NULL )*i+234 );
temp.v.x = ((double ) rand() )/ RAND_MAX;
srand ( time ( NULL )*i*546 );
temp.v.y = ((double ) rand() )/ RAND_MAX;
srand ( time ( NULL )*i*567+345 );
temp.v.z = ((double ) rand() )/ RAND_MAX;
t[0] = temp.v.x; t[1] = temp.v.y; t[2] = temp.v.z;

printf ( "temp [v %d] -> %F %F %F \n",i, temp.v.x, temp.v.y, temp.v.z );
addList_II (head, t );
}

c=0;
ptemp=head;
while (ptemp!=NULL) {
printf("[v %d] -> linked vertex is %F %F %F \n", c, ptemp->v.x,ptemp->v.y, ptemp->v.z);
ptemp=ptemp->pnext;

}
}

最佳答案

在您的addList_II 中,您设置了错误的指针。您正在获取新节点并将其下一个指针指向头的下一个指针,该指针为空。

   void addList_II ( datalist *head , double data[3] ) {
datalist *adddata;
adddata = (datalist *)malloc ( sizeof (datalist) );
if (adddata == NULL) { printf("cannot allocate memory for insertion data"); exit(1); }
adddata->v.x = data[0];
adddata->v.y = data[1];
adddata->v.z = data[2];

// adddata->pnext = head->pnext; //not THIS
adddata->pnext = head; //DO THIS
head=adddata;
}

此外,您的指针在您的函数调用中超出了范围。您需要做的是传入 ** 变量。这意味着您传递一个指向指针的指针,该指针指向您的内存块。您通过取消对本地 * 的引用来修改原始指针,返回时一切正常。

如果您需要更多解释,尽管询问,但这是解决方案

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NEWline printf("\n")

typedef struct position {
double x;
double y;
double z;
} position;

struct datalist {
position v;
struct datalist *pnext;

};
typedef struct datalist datalist;

void initializeList (datalist **head);
void addList_II (datalist **head , double data[3] );

void initializeList (datalist **head){
position pv= {0,0,0};
*head = (datalist *) malloc ( sizeof (datalist) );
if (head == NULL){ printf("cannot allocate memory"); exit(1); }
(*head)->v=pv;
(*head)->pnext=NULL;
}

void addList_II ( datalist **head , double data[3] ) {
datalist *adddata;
adddata = (datalist *)malloc ( sizeof (datalist) );
if (adddata == NULL) { printf("cannot allocate memory for insertion data"); exit(1); }
adddata->v.x = data[0];
adddata->v.y = data[1];
adddata->v.z = data[2];

adddata->pnext = *head; //DO THIS
*head=adddata;
}

int main() {

datalist *ptemp;
datalist temp;
int i,c; //counters
datalist *head;

initializeList(&head);


for ( i = 0; i < 3; i++ ) {
double t[3];
srand ( time ( NULL )*i+234 );
temp.v.x = ((double ) rand() )/ RAND_MAX;
srand ( time ( NULL )*i*546 );
temp.v.y = ((double ) rand() )/ RAND_MAX;
srand ( time ( NULL )*i*567+345 );
temp.v.z = ((double ) rand() )/ RAND_MAX;
t[0] = temp.v.x; t[1] = temp.v.y; t[2] = temp.v.z;

printf ( "temp [v %d] -> %F %F %F \n",i, temp.v.x, temp.v.y, temp.v.z );
addList_II (&head, t );
}

ptemp=head;

while (ptemp!=NULL) {
NEWline;
printf("[v ] -> linked vertex is %F %F %F \n", ptemp->v.x,ptemp->v.y, ptemp->v.z);
ptemp=ptemp->pnext;

}
}

关于c - 为什么我在 C 中的链表失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com