我链接的List -C中的分段错误错误

时间:2015-03-31 22:21:31

标签: c linked-list malloc

我把几段代码放在一起,制作一个链接列表,添加到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 memory\n");
        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 memory\n");
        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);




}

1 个答案:

答案 0 :(得分:0)

我建议如下。

#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 memory\n");
        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;
}