【问题标题】:i am unable to print the linked list我无法打印链接列表
【发布时间】:2023-03-16 02:16:01
【问题描述】:
#include<stdio.h>
#include<stdlib.h>

main()
{  
    struct node
    {   
         int data;
         struct node *next;
    };

    struct node *first=(struct node*)malloc(sizeof(struct node));
    struct node *second=(struct node*)malloc(sizeof(struct node));
    struct node *third=(struct node*)malloc(sizeof(struct node));

    scanf("%d %d %d",&(first->data),&(second->data),&(third->data));
    first->next=second;
    second->next=third;
    third->next=NULL;
    struct node *t=(struct node *)first;
    f(t);
 }

f(struct node *a)
{
    while(a!=NULL)
    {
        printf("%d",a->data);
        a= a->next;
    }
}

上面的代码给出了警告和错误“在参数列表中声明的结构节点”和“取消引用指向不完整类型的指针”

请帮助我运行代码并解决错误。

【问题讨论】:

  • 在哪里定义了结构?它只会在那个范围内定义。
  • 你是什么意思..?
  • 也许您应该花一些时间来了解一下作用域?任何初学者书籍或教程或课程都应该提到它,并且在一个范围内定义的内容仅在该(和嵌套)范围内可用。

标签: c struct


【解决方案1】:

有几个问题。

  • 您的函数没有返回类型
  • 您的struct node 仅在main 的范围内声明。
  • 您在声明之前使用了f 函数。
  • 最后但并非最不重要的一点是:代码的格式很糟糕。格式化对编译器来说并不重要,但只对包括你在内的人类读者来说很重要

你的程序应该是这样的:

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

struct node               // structure declared at global scope
{
  int data;
  struct node *next;
};

void f(struct node *a);   // declare function

int main()                // function has now return type int
{
  struct node *first = (struct node*)malloc(sizeof(struct node));
  struct node *second = (struct node*)malloc(sizeof(struct node));
  struct node *third = (struct node*)malloc(sizeof(struct node));

  scanf("%d %d %d", &(first->data), &(second->data), &(third->data));
  first->next = second;
  second->next = third;
  third->next = NULL;
  struct node *t = (struct node *)first;
  f(t);
}

void f(struct node *a)   // function has now return type void
{
  while (a != NULL)
  {
    printf("%d", a->data);
    a = a->next;
  }
}

免责声明:这个程序只是在没有警告的情况下正确编译,但我没有检查它是否真的有意义。

【讨论】:

  • f函数的定义实际上main函数之外。而就是为什么f没有看到struct node声明。
  • @CiaPan 哦,你是对的。已编辑。这就是为什么我的清单的最后一点很重要;-)
  • 我同意。我也打算回答这个问题,但你的速度更快。我的回答基本上和你的一样,除了我把最后一点放在列表的顶部。程序是给计算机的,但源代码是给人类的。所以它应该总是尽可能的可读。
  • @Jabberwocky 它编译并且也有意义;-) 它是一个递归打印链表的程序。
  • @ShivamSingh 是的,这似乎是正确的,但没有递归。
【解决方案2】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ===== Define structure outside function ===== */
/*
    Use `typedef` to rename the data type.
    `node_t` is equal with `struct node_t`, and
    `node` is equal with `struct node_t *`.
 */
typedef struct node_t
{
    int data;
    struct node_t *next;
} node_t, *node;

/*  Define link list
 */
typedef struct list_t
{
    node head;
    node rear;
    int size;
} list_t, *list;

list list_create()
{
    list l = NULL;

    if ((l = malloc(sizeof(list_t))) == NULL)
    {
        exit(-1);
    }
    memset(l, 0, sizeof(list_t));

    return l;
}

int list_destroy(list *pl)
{
    node cur;
    node prev;

    if (pl == NULL || *pl == NULL)
        return -1;

    cur = (*pl)->head;
    while (cur != NULL)
    {
        prev = cur;
        cur = cur->next;
        free(prev);
    }

    free(*pl);
    *pl = NULL;

    return 0;
}

int list_push(list l, int data)
{
    node n;

    if (l == NULL)
        return -1;

    if ((n = malloc(sizeof(node_t))) == NULL)
    {
        exit(-1);
    }

    n->data = data;
    n->next = NULL;

    if (l->head == NULL)
    {
        l->head = n;
    }
    else
    {
        l->rear->next = n;
    }

    l->rear = n;
    l->size++;

    return 0;
}

int list_pop(list l, int *pdata)
{
    if (l == NULL || l->head == NULL)
    {
        return -1;
    }

    *pdata = l->head->data;
    if (l->head == l->rear)
    {
        l->rear = NULL;
    }
    l->head = l->head->next;

    l->size--;

    return 0;
}

void list_foreach(list l, void (*func)(int data, void *arg), void *arg)
{
    node cur = l->head;

    while (cur != NULL)
    {
        func(cur->data, arg);
        cur = cur->next;
    }
}


static void print_node(int data, void *arg)
{
    printf("%d\n", data);
}

int main(int argc, char *argv[])
{
    int i;
    int d;
    list l;

    l = list_create();

    for (i = 0; i < 5; i++)
    {
        scanf("%d", &d);
        getchar();
        list_push(l, d);
    }

    list_foreach(l, print_node, NULL);

    printf("Pop: [status %d], ", list_pop(l, &d));
    printf("[value %d]\n", d);

    printf("Pop: [status %d], ", list_pop(l, &d));
    printf("[value %d]\n", d);

    list_foreach(l, print_node, NULL);

    list_destroy(&l);

    return 0;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 2020-12-15
    • 2023-03-28
    相关资源
    最近更新 更多