简体   繁体   English

我应该在此代码中进行哪些更改以使其打印某些内容? 插入 function 是否需要是 struct 类型才能插入节点?

[英]What should I change in this code to make it print something? Does the insert function need to be of type struct to insert a node?

I honestly don't really understand how this code works.老实说,我真的不明白这段代码是如何工作的。 To make it work would I just need to change the type of some of the functions or would I need to change more than that?为了使其工作,我只需要更改某些功能的类型,还是需要更改更多?

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

struct LinkedList{
    int data;
    struct LinkedList* next;
};
int main(){
    struct LinkedList *A = NULL;
    insert(A, 3);
    printList(A);
    return 0;
}
void insert(struct LinkedList *root, int item){
   root=malloc(sizeof(struct LinkedList));
   root->data= item;
   root->next = NULL;
}
void printList(struct LinkedList *head){
    struct LinkedList *temp=head;
    while (temp != NULL){
        printf("%d", temp->data);
        temp = temp->next;
    }
}

It is necessary to pass as a parameter to "insert" function a pointer to the "struct LinkedList" pointer... not the "struct LinkedList" pointer itself.有必要将一个指向“struct LinkedList”指针的指针作为参数传递给“插入”function……而不是“struct LinkedList”指针本身。

Additionally, you are only calling "insert()" once in this version so you don't see and your "insert()" is not creating the linked list.此外,您在此版本中只调用了一次“insert()”,因此您看不到并且您的“insert()”没有创建链接列表。 Instead it is overwriting the "head" (your "A" pointer) every time it is called.相反,它每次被调用时都会覆盖“head”(你的“A”指针)。

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

struct LinkedList{
    int data;
    struct LinkedList* next;
};
/* prototypes */
void insert(struct LinkedList **root, int item);
void printList(struct LinkedList *head);

int main(){
    struct LinkedList *A = NULL;
    insert(&A, 3);
    insert(&A, 4);
    printList(A);
    return 0;
}
void insert(struct LinkedList **root, int item){

   struct LinkedList *newptr;

   newptr=malloc(sizeof(struct LinkedList));
   newptr->data= item;
   // ORIGINAL: root->next = NULL;
   newptr->next = *root;

   *root = newptr;
}
void printList(struct LinkedList *head){
    struct LinkedList *temp=head;
    while (temp != NULL){
        printf("%d\n", temp->data);
        temp = temp->next;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 声明结构类型,将值插入该类型的数组,然后打印出数组 - Declare struct type, insert values into array of the type, and print out array 在BST中插入项目时,为什么根节点会更改? - Why does my root node change when I insert item in BST? 警告-预期的“结构节点**”但参数类型为“结构节点**”是什么意思? - What does the warning - expected ‘struct node **’ but argument is of type ‘struct node **’ mean? 在链表的第 n 个位置插入一个节点; 这段代码有什么问题? - Insert a node at nth position in a linked list; what's wrong in this code? 为什么我的代码未在此链接列表中插入新节点? - Why does my code not insert a new node into this linked list? 我的算法在函数树中插入节点有哪些缺陷? - What are the flaws in my algorithm to insert a node in a function tree? 当我收到错误“运行时错误:&#39;node&#39;(又名&#39;struct node&#39;)类型的空指针内的成员访问”时,我应该寻找什么? - What should I look for when when i get the error "runtime error: member access within null pointer of type 'node' (aka 'struct node')"? 为什么此功能无法成功将节点插入链表? - Why does this function not successfully insert a node into a linked list? 我应该从这段代码中改变什么 - what thing i should change from this code 如何将节点从结构 B 插入到结构 A(二叉树) - How to Insert a node from a struct B to struct A (binary trees)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM