2014-10-16 56 views
0
struct x{ 
    ...; 
    ...; 
    struct x * next; 
}; 

struct x create() { 
    struct x new = malloc... 
    new->... = .; 
    new->... = ..; 
    new->next = NULL 
}; 

当我创建一个新的struct x节点时,它如何在使用struct x时创建多次。我感到奇怪,你可以多次使用它,因为它将内存分配给每次都有新同名的结构x?结构中的每个节点都不需要单独的名称。或者每次新的内存分配完成时,这只是重要的。创建指向彼此的新节点

主要问题:我将创建第一个节点,然后创建第二个节点。第一个节点应该指向第二个节点,依此类推。但是当我创建第一个节点时,第二个节点不存在,所以我不能设置first-> next = second。

我已经看过链接列表的例子,但它并没有改善我此刻的想法。代码并不像我自己的理解和想法那么重要。请帮助我思考和理解这个概念。

//我试图遵循Degustaf的提示(除了下一个指针,基本上与创建一个新节点相同),但是实现错误。因此,我在这段代码中发现了什么错误:

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

struct x{ 

    int a; 
    int b; 
    struct x * next; 
} 

    struct x *create(int a , int b){ 
    struct x *new = malloc(sizeof(struct x)); 
    new->a = a;//namn skitsamma allokering relevant 
    new->b = b; 
    new->next = NULL; 
    return new; 
}; 


    int main() { 

    struct x *x1 = struct x *create(12,13); 


    return 0; 

} 
+0

'struct x new = malloc ...'应该是'struct x * new = malloc ...'。你还需要返回'* new'。然而,我诚实地说,只是使用指针(返回指针等)。 – 2014-10-16 17:27:10

回答

0

或者它只是每个重要的一个新的内存分配完成时间。

正确。

但是,您的代码还有其他问题。特别是,你不会从你的create函数返回任何东西。我看到了两种方法来解决这个问题。第一,你可以直接返回结构,这意味着你不需要的malloc:使用

struct x x1 = create(); 
struct x x2 = create(); 
x1.next = &x2; 

另一种可能是返回一个指针

struct x create() 
{ 
    struct x new; 
    new.member1 = .; 
    new.member2 = ..; 
    new.next = NULL; 
    return new; 
}; 

然后你可以填充它一个struct,在这种情况下,这将成为

struct x *create() 
{ 
    struct x *new = malloc...; 
    new->member1 = .; 
    new->member2 = ..; 
    new->next = NULL; 
    return new; 
}; 

然后你可以使用填充它

struct x *x1 = create(); 
x1->next = create(); 

我的意见是,第二个选项更清洁,因为您不需要担心链表中的单个元素超出范围,但它在释放内存时需要小心(需要遍历列出并一次释放一个元素。

+0

如果我调用struct x * create()我应该用struct x * create()而不是struct x * x1 = create()来调用它吗?你的提示看起来很棒,我试着用代码创建一个结构体,你现在可以在我的问题相同的文章中看到。我实现了错误的代码,但不知道什么是错的。 – user3216616 2014-10-16 23:05:11

+0

将'main'函数的主体改为'struct x * x1 = * create(12,13)' – Degustaf 2014-10-16 23:53:35

1

您可以简单地在创建两者之后分配指针的值。

struct x x1 = create(); 
struct x x2 = create(); 
x1.next = &x2; 
x2.next = &x1; 
+0

我不认为你真的想让你的链表包含一个循环。 – Degustaf 2014-10-16 17:36:38

0

我的事,这是你想要的,但是这是在列表中intenger数也同比如你所愿可以改变代码的例子。

#include <stdio.h> 
#include <string.h> 
#include <malloc.h> 
#include <iostream> 
struct cell { 
    float info; 
    struct cell * next; 
}; 
int more (float * k) 
{ 
char ans[4]; 

printf("Continue Yes/No: "); 
scanf("%s",ans); 
if (ans[0]=='Y') { 
    printf("insert value: "); 
    scanf("%f",k); 
    return(1); 
} 
else 
    return(0); 
} 

struct cell * crelist() 
{ 
struct cell * last = (struct cell *)NULL; 
struct cell * ptr = (struct cell *)NULL; 
struct cell * list = (struct cell *)NULL; 
float k; 

ptr = (struct cell *)malloc(sizeof(struct cell)); 
if (ptr != (struct cell *)NULL) { 
    printf("insert value: "); 
    scanf("%f",&k); 
    ptr->info = k; 
    ptr->next = (struct cell *)NULL; 
    list = ptr; 
    last = ptr; 
} 
else 
    return((struct cell *)NULL); 
while (more(&k)) { 
     ptr = (struct cell *)malloc(sizeof(struct cell)); 
     if (ptr != (struct cell *)NULL) { 
      ptr->info = k; 
      ptr->next = (struct cell *)NULL; 
      last->next = ptr; 
      last = ptr; 
     } 
     else 
      break; 
} 
     return(list); 
} 

void printlist(struct cell * list) 
{ 
    struct cell * p; 

    p = list; 
    while (p != (struct cell *)NULL) { 
     printf("->%f\n",(*p).info); 
     p=(*p).next; 
    } 
    return; 
} 

int main() 
{ 
    struct cell * list; 
    int i; 

    list = crelist(); 
    printlist(list); 
    scanf("%d",&i); 
    system("pause"); 
    return 0; 
}