错误:创建链接列表显示函数时

发布于 2025-01-17 02:04:49 字数 1218 浏览 3 评论 0原文

我正在尝试创建一个包含 display() 函数的链接列表,但我不断收到错误。无法在输出中显示元素的函数。

我尝试过各种在线代码解决方案,但没有对我的代码起作用。

如果有人有任何想法,下面是我的代码。让我告诉解决它

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

struct node{
    int data;
    struct node *next;
};


int create(int n){
    int d1,d2,i;
    struct node *head, *temp, *newnode;
    head = (struct node *) malloc(sizeof(struct node));
    printf("enter the data for node 1");
    scanf("%d",&d1);
    
    head->data = d1;
    head->next = NULL;
    temp = head;
    
    for(i=2; i<=n; i++){
        
        newnode = (struct node *)malloc(sizeof(struct node));
        printf("enter a data for %d node",i);
        scanf("%d",&d2);
        
        newnode->data = d2;
        newnode->next = NULL;
        temp->next = newnode;
        temp = temp->next; // newnode can also use instead of temp->next 
    }


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

int main()
{
     struct node* head = NULL;
     
    int n;
   printf("Enter number of node to create");
   scanf("%d",&n);
   
  create(n);
  printList(head);
    

    return 0;
}

I am trying to create a linked list with display() function in it but i continuously getting error in it. a function not able to display element in output.

I have try various online solution of code but not worked on my code.

Below is my code if anyone have any idea. let me tell to solve it

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

struct node{
    int data;
    struct node *next;
};


int create(int n){
    int d1,d2,i;
    struct node *head, *temp, *newnode;
    head = (struct node *) malloc(sizeof(struct node));
    printf("enter the data for node 1");
    scanf("%d",&d1);
    
    head->data = d1;
    head->next = NULL;
    temp = head;
    
    for(i=2; i<=n; i++){
        
        newnode = (struct node *)malloc(sizeof(struct node));
        printf("enter a data for %d node",i);
        scanf("%d",&d2);
        
        newnode->data = d2;
        newnode->next = NULL;
        temp->next = newnode;
        temp = temp->next; // newnode can also use instead of temp->next 
    }


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

int main()
{
     struct node* head = NULL;
     
    int n;
   printf("Enter number of node to create");
   scanf("%d",&n);
   
  create(n);
  printList(head);
    

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深陷 2025-01-24 02:04:49

更改 create 函数的返回类型以返回节点的地址,如下所示:struct node * create(int n) 并将返回值更改为:return head;

最后,在 main() 中将返回的地址保存在 head 指针中: head = create(n);

Change return type of the create function to return the address of the node like this: struct node * create(int n) and change return value to: return head;.

Finally, in main() save the returned address in head pointer: head = create(n);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文