我的linkedList-C中出现分段错误



我把几段代码放在一起,形成一个链表,添加到head(有一个特殊函数)和中间(也有特殊函数)。我的问题是,我需要为程序提供数字,并将它们作为节点插入我的LINKEDLIST中。然而,我的显示函数(显示节点树)会返回分段错误,因此在没有任何显示函数的情况下只取值也是如此。我是malloc的新手,所以我怀疑问题出在哪里?感谢

#include<stdio.h>
#include<stdlib.h>
/*LINKEDLIST STRUCT*/
struct node {
        int data;
        struct node *next;
        };
    /*Inserting head-Node*/
struct node *insert_head(struct node *head, int number)
{
    struct node *temp;
    temp = malloc(sizeof(struct node));
    if(temp == NULL)
    {
        printf("Not enough memoryn");
        exit(1);
    }
    temp->data = number;
    temp->next = head;
    head = temp;
    return head;
}
    /*Inserting inside a list*/
void after_me(struct node *me, int number)
{
    struct node *temp;
    temp = malloc(sizeof(struct node));
    if(temp == NULL)
    {
        printf("Not enough memoryn");
        exit(1);
    }
    temp->data = number;
    temp->next = me->next;
    me->next = temp;
}
    /*PRINTING LIST*/
void display(struct node *head)
{
    struct node *moving_ptr = head;
    while(moving_ptr != NULL)
    {
        printf("%d-->",moving_ptr->data);
        moving_ptr = moving_ptr->next; 
    }
}   
int main()
{
    int index;
    struct node *head;
    struct node *previous_node;
    scanf("%d", &index);
    while(index > 0)
    {

        /*allocating in List */
        if(head == NULL)
            head = insert_head(head,index);
        else
        if((head != NULL) && (index <= (head->data)))
            {
                struct node *temp;
                head->next = temp;
                temp->next = head;/*TRY INSERT HEAD FUNC.*/
            }
        else
        if((head != NULL) && (index > (head->data)))
        {
            previous_node->data = index-1;
            after_me(previous_node,index);
        }
        scanf("%d", &index);
    }
        display(head);


}

我建议如下。

#include <stdio.h>
#include <stdlib.h>
struct node {
    int data;
    struct node *next;
};
//aggregated into one place    
struct node *new_node(int number){
    struct node *temp;
    if(NULL == (temp = malloc(sizeof(*temp)))){
        printf("nNot enough memoryn");
        exit(1);
    }
    temp->data = number;
    temp->next = NULL;
    return temp;
}
struct node *insert_head(struct node *head, int number) {
    struct node *temp = new_node(number);
    temp->next = head;
    return temp;
}
void after_me(struct node *me, int number){
    struct node *temp = new_node(number);
    temp->next = me->next;
    me->next = temp;
}
void display(struct node *head){
    struct node *moving_ptr = head;
    while(moving_ptr != NULL){
        printf("%d", moving_ptr->data);
        if(moving_ptr = moving_ptr->next)
            printf("-->");
    }
    putchar('n');
}
struct node *insert(struct node *me, int number){
    if(me){
        if(number <= me->data){
            me = insert_head(me, number);
        } else {
            me->next = insert(me->next, number);
        }
    } else {
        me = new_node(number);
    }
    return me;
}
void release(struct node *list){//Of course, you will be able to replace a simple loop(e.g while-loop).
    if(list){
        release(list->next);
        free(list);
    }
}
int main(void){
    struct node *head = NULL;
    int index;
    while(1==scanf("%d", &index) && index > 0){
        head = insert(head, index);
    }
    display(head);
    release(head);
    return 0;
}

相关内容

  • 没有找到相关文章