c中的链表插入:插入错误的值

时间:2012-02-21 04:57:33

标签: c algorithm linked-list

我无法弄清楚我的代码有什么问题,当我运行代码时我有一些垃圾输出!似乎我插入了一些奇怪的值。谢谢你的帮助:

我的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

struct node {
    int number;
    struct node *next;
    struct node *prev;
};

struct node *head = NULL;

int number_of_cycles = 0;

int arr[] = {
    82, 13, 22, 61, 12, 52, 41, 75, 98, 20, 6, 9, 7, 1, 5, 4, 2, 8, 3
};


/* insert a node directly at the right place in the linked list */
void insert_node(int value);

int main(void) {
    struct node *current = NULL;
    struct node *next = NULL;

    int i = 0;

    struct timeval tbegin,tend; 
    double texec=0.; 

    // Start timer 
    gettimeofday(&tbegin,NULL); 

    /* insert some numbers into the linked list */
    for(i = 0; i < 19; i++)
        //insert_node(rand());
        insert_node(arr[i]);

    /* print the list */
    printf(" before  after\n"), i = 0;
    while(head->next != NULL) {
        printf("%4d\t%4d\n", arr[i++], head->number);
        head = head->next;
    }

    /* free the list */
    for(current = head; current != NULL; current = next)
        next = current->next, free(current);
    // End timer 
    gettimeofday(&tend,NULL); 

    // Compute execution time 
    texec=((double)(1000*(tend.tv_sec-tbegin.tv_sec)+((tend.tv_usec-tbegin.tv_usec)/1000)))/1000.; 

    printf("Elapsed time = %f\n", texec);
    printf("Number Of Cycles = %d\n", number_of_cycles);

    return 0;
}

void insert_node(int value) {
    struct node *new_node = NULL;
    struct node *cur_node = NULL;
    struct node *last_node = NULL;
    int found;


    new_node = (struct node *)malloc(sizeof(struct node *));
    if(new_node == NULL) {
        printf("memory problem\n");

    }
    new_node->number = value;
    /* If the first element */
    if (head == NULL) {
        new_node->next = NULL;
        new_node->prev = NULL;
        head = new_node;
    } 

    else if (new_node->number < head->number) {
        new_node->next = head;
        head->prev = new_node;
        head = new_node;    
        new_node->prev = NULL;
    } 

    else {
        cur_node = head;
        found = 0;
        while (( cur_node != NULL ) && ( found == 0 )) {
            if( new_node->number < cur_node->number )
            {
                found = 1;
            }
            else
            {
                last_node = cur_node;
                cur_node = cur_node->next;
            }
            number_of_cycles++;
        }
        if( found == 1 )
        {
            new_node->prev = cur_node->prev;
            new_node->next = cur_node; 
            cur_node->prev->next = new_node;
            cur_node->prev = new_node;
        }
        else
        {
            last_node->next = new_node;
            new_node->next = NULL;
            new_node->prev = last_node;
        }
        number_of_cycles++;         
    }

}

此代码的输出为:

before  after
  82    1916799424
  13    1916799408
  22    1916799392
  61    1916799376
  12    1916799360
  52    1916799344
  41    1916799328
  75    1916799312
  98    1916799296
  20    1916799280
   6    1916799264
   9    1916799248
   7    1916799232
   1    1916799216
   5    1916799200
   4    1916799184
   2    1916799168
   8    1916799152
Elapsed time = 0.000000
Number Of Cycles = 0

1 个答案:

答案 0 :(得分:7)

您的问题出在insert_node:

new_node =(struct node *)malloc(sizeof(struct node *));

你想要的是:

new_node =(struct node *)malloc(sizeof(struct node));

原因是您希望在堆上为结构节点分配足够的空间,而不是结构节点指针。