简体   繁体   English

在C段错误中附加链接列表

[英]Appending Linked List in C seg fault errors

I am having some trouble adding integers to the end of my linked list. 我在将整数添加到链接列表的末尾时遇到麻烦。 I am very new to C and had part of my program working properly (the push function). 我是C语言的新手,程序的一部分正常工作(推功能)。 I want to return a pointer to a struct node, and I am not quite sure where I am going wrong in my append function. 我想返回一个指向struct节点的指针,但我不确定在我的append函数中哪里出错了。

~Thanks. 谢谢

 enter code here


 //node.h

 #ifndef NODE_H
 #define NODE_H

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

 int length(struct node *);
 struct node* push(struct node *, int);     //adds integer to front of list.
 struct node* append(struct node *, int);   //adds integer to back of list.
 void print(struct node *, int);

 #endif


 //node.c

 #include "./node.h"
 #include<stdlib.h>
 #include<stdio.h>

 int length(struct node *current){
    if(current->next != NULL)
    return 1 + length(current->next);
 else
  return 1;
 }

 struct node* push(struct node *head, int num){

    struct node *temp = malloc(sizeof(struct node));
    temp->val = num;
    temp->next = head;
    head = temp;
    temp = NULL;
    return head;
    }

    struct node* append(struct node *current, int num){

       if(current != NULL){
       append(current->next, num);
       }

       else{
         struct node* temp = malloc(sizeof(struct node));
         temp->val = num;
       temp->next = NULL;
       current = temp;
       return current;

       }
       } 



void print(struct node* head, int size){

   printf("The list is %i", size);
   printf(" long \n");
   struct node* temp;
   temp = head;
   while(temp != NULL){
   printf("%d", temp->val);
   printf(" ");
   temp = temp->next;
   }
   printf(" \n");
   }


  //Main

  #include "./node.h"
  #include<stdlib.h>
  #include<stdio.h>

  int main(){

    char ans[2];
    int num;
    struct node* head = NULL;

    do{
     printf("Enter a integer for linked list: ");
     scanf("%d", &num);

     head = append(head, num);
     printf("Add another integer to linked list? (y or n) ");
     scanf("%1s", ans);
     }while(*ans == 'y');

     print(head, length(head));

     return 0;
     }

I think what is missing is that the recursive part of the function needs to set current->next. 我认为缺少的是该函数的递归部分需要设置current-> next。 This has the effect of setting every node's next pointer to what it was until you get to the end of the list, when it is set to the newly malloced node. 这样可以将每个节点的下一个指针设置为原来的状态,直到将其设置为新分配的节点时,您才能到达列表的末尾。

struct node* append(struct node *current, int num){

       if(current != NULL){
         current->next = append(current->next, num);
         return current;
       }
       else {
         struct node* temp = malloc(sizeof(struct node));
         if (temp == NULL) abort();
         temp->val = num;
         temp->next = NULL;
         return temp;
       }
} 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM