0
#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *next;
};
struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    printf("\n address = %u --- ",*h);
    t=(struct node *)malloc(sizeof(struct node));
    if(h==NULL)
    {
        t->data=x;
        t->next=NULL;
        h=t;
    }
    else
    {


        t->data=x;
        t->next=h;
        h=t;
    }
    return h;

}
void display(struct node *h1)
{
    struct node *t=h1;
    while(t->next!=NULL)
    {
        printf("%d->",t->data);
        t=t->next;
    }
}
int main()
{
    struct node *p=NULL;
    int a,ch=5;
    while(ch--)
    {

    printf("\n Enter data");
    scanf("%d",&a);
    p=insert_beg(p,a);
    display(p);
    }display(p);

}

The above is a code for inserting element in the begining of the single linked link list in c.

The code compiles successfuly but when i am trying to insert an element the system hangs up... Not to locate the error. Can anyone suggest the correction i need to done.

Is there any error in the expression mentioned below... Need help.

p=insert_beg(p,a);
6
  • What do you mean by "[i]s there any error in the expression mentioned below"? Do you mean that you get a build error on that line? Or do you mean you get a crash when you run the program? Or do you simply wonder if the line is correct (without actually building/running your program)? Please elaborate. If you get a crash at runtime, have you tried running a debug build in a debugger, to catch the crash in action and locate where it actually happens in your code? Commented Dec 19, 2016 at 15:26
  • 3
    delete printf("\n address = %u --- ",*h); Also at display : while(t->next!=NULL) --> while(t != NULL) Commented Dec 19, 2016 at 15:28
  • 1
    don't forget to free memory for the Node, I can see memory freak there in main() Commented Dec 19, 2016 at 15:32
  • If you want to print the address of the node you passed to your insert function, use the %p specifier instead of %u in your printf(), and don't dereference h (which is NULL for the first insert). In other words, print the value of the pointer (which is the address of the node if there is one), not the value of what it points to. Commented Dec 19, 2016 at 15:33
  • 1
    printf("\n address = %u --- ",*h); attempts to dereference a NULL pointer. Try printf("\n address = %p --- ", (void *) h); Commented Dec 19, 2016 at 15:33

3 Answers 3

1
#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *next;
};
struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    t=(struct node *)malloc(sizeof(struct node));
    if(h==NULL)
    {
        t->data=x;
        t->next=NULL;
        h=t;
    }
    else
    {
        t->data=x;
        t->next=h;
        h=t;
    }
    return h;
}
void display(struct node *h1)
{
     struct node *t=h1;
     while(t->next!=NULL)
     {
         printf("%d->",t->data);
         t=t->next;
     }
    printf("%d",t->data);
}
int main()
{
    struct node *p=NULL;
    int a,ch=5;
    while(ch>=0)
    {
        printf("\n Enter data:-");
        scanf("%d",&a);
        p=insert_beg(p,a);
        display(p);
        ch--;
    }
    display(p);

}

Sign up to request clarification or add additional context in comments.

2 Comments

I needed this code in my program. Helped me where I was stuck. Thanks for the help
where did you strucked @Pankti
0
    while(t->next!=NULL)

should be:

    while(t!=NULL)

also, your insert function can be simplified to:

struct node* insert_beg(struct node *h,int x)
{
    struct node *t;
    t=(struct node *)malloc(sizeof(struct node));
    t->data=x;
    t->next=h;
    return t;
}

Comments

0

You have an error in:

printf("\n address = %u --- ",*h);

Trying to print an entire structure.

In general use a debbuger for this as explained here

You can compile your program with debug information as follows:

gcc -o main -g main.c

And run it in gdb:

gdb main

Type 'run' command inside gdb, if it fails you can use a 'backtrace' to get loads of information why. Get comfortable with gdb tutorials as the manual might scare you off at first.

1 Comment

I had dereference h which was initially assigned NULL value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.