Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7540777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:43:20+00:00 2026-05-30T07:43:20+00:00

i can’t figure out what’s wrong with my code, when i run the code

  • 0

i can’t figure out what’s wrong with my code, when i run the code i have some garbage output!it seems that i’m inserting some strange values. Thank’s for your help :

My code is below :

#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++;         
    }

}

The output of this code is :

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:43:22+00:00Added an answer on May 30, 2026 at 7:43 am

    Your problem is in insert_node:

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

    What you want is:

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

    The reason being that you want to allocate enough space on the heap for a struct node, not a struct node pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can a LINQ enabled app run on a machine that only has the .NET
Can somebody point me to a resource that explains how to go about having
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can you suggest some good MVC framework for perl -- one I am aware
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Can I run this in a Windows command prompt like I can run it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.