试图理解链接列表和结构

时间:2014-11-18 22:55:15

标签: c struct linked-list nodes

我只使用演讲中的笔记汇总了以下代码,所以如果遗漏了明显的东西,我会道歉。我在“Node * newNode(int item,Node * h)”的参数下得到红色下划线,表示无法解析标识符'。基本上代码尝试做的是在链表的开头添加一个新节点。你能告诉我我做错了什么吗?

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

struct Node;
Node *newNode(int item, Node *h);
/*
 * 
 */
int main(int argc, char** argv) {

    typedef struct node{
        int info;
        struct node *link;
    }Node;

    Node *head = NULL;

    Node *newNode(int item, Node *h){

        Node *p;
        *p = (Node *) malloc(sizeof(Node));
        p -> info = item;
        p -> link = h;
        return p;

    }

    head = newNode(1, head);                   //add a new head to start of list




    return (EXIT_SUCCESS);
}

3 个答案:

答案 0 :(得分:1)

结构的前向声明和typedef之间存在细微差别。 它变得混乱,因为忽略结构符号并且只生成typedef是很常见的。

看看这个:

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

struct node; //We're forward declaring a struct called node (small n).

//We're declaring a function that accepts node structs (small n).
struct node *newNode(int item, struct node *h);

//We are doing two things here. 
//First, we're defining the structure of node (which we forward declared).
//Second, we're aliasing struct node (small n) as Node (Big N) in a typedef declaration.
typedef struct node{
    int info;
    struct node *link;
}Node;

//From now on (and only now on) we can refer to Node and it will be seen as 
//the same as struct node (small n).

//Now we define that function we declared above.
//Notice the declaration used struct node (small n) but this just uses Node (big N).
//The typedef tells the compiler they mean the same thing!
Node *newNode(int item, Node *h){

    Node *p;
    p = (Node *) malloc(sizeof(Node));
    p -> info = item;
    p -> link = h;
    return p;
}

//No matter how toy your example we need to clean up after ourselves! 
//It's just good practice.
//This only frees a single node in isolation but it's enough for your example.
void deleteNode(Node* n){
    free(n);
}

int main(int argc, char** argv) {


    Node *head = NULL;


    head = newNode(1, head);                 //add a new head to start of list

    //Do something with your lovely new node here.
    printf("head node value = %d\n",head->info) //The head node value is 1.   

    deleteNode(head);

    return (EXIT_SUCCESS);
}

答案 1 :(得分:0)

您正在尝试在另一个函数内定义一个函数。标准C不支持。

您可以通过将node typedef和newNode()函数定义移到main()之外来解决该问题。

答案 2 :(得分:0)

您可以在主程序中定义Node的结构,该结构在其自己的范围内未全局定义。您需要将struct Node;替换为main:

中定义的结构

typedef struct node{ int info; struct node *link; }Node;

您还在main中定义Node函数,然后尝试使用它来定义头部或启动指针。您的程序应如下所示:

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

typedef struct node{
    int info;
    struct node *link;
}Node;

Node *newNode(int item, Node *h); // Function Prototype to new node.
/*
*
*/
int main(int argc, char** argv) {

 Node *head = NULL;

 head = newNode(1, &head);   //add a new head to start of list // You want to send the reference of the pointer
                                                              // to the function newNode.

 return (EXIT_SUCCESS);
}

Node *newNode(int item, Node *h){

    Node *p;
    p = (Node *)malloc(sizeof(Node)); // Do not use *p that deferences the pointer that you are trying to allocate memory for.
    p->info = item;
    p->link = h;
    return p;
}

一句警告!我请您参考您的教科书或其他学术网站,这些网站基本上会向您展示如何格式化程序。您的代码中导致的错误是最基本的,如果您希望完全理解动态内存分配,我会要求您在继续之前从那里开始。