0

OK, this is a simple single linked list program in c

struct node
{
    int id;
    char name[20];
    int sem;

    struct node *link;
};

typedef struct node* Node;

Node getnode()
{
    Node temp=(Node)(malloc(sizeof(Node)));
    if(temp==NULL)
        printf("\n Out of memory");

    return temp;
}

Node ins_pos(Node first)
{
    Node temp=getnode();
    printf("\n Enter id ");
    scanf("%d",&temp->id);
    printf("\n Enter name ");
    scanf("%s",temp->name);
    printf("\n Enter semester ");
    scanf("%d",&temp->sem);

    if(first == NULL)
    {
        temp->link=NULL;
        return temp;
    }

    else
    {
        int pos,i;
        printf("\n Enter position: ");
        scanf("%d",&pos);

        if(pos == 1)
        {
            temp->link=first;
            return temp;
        }

        else
        {
            Node prev=NULL,cur=first;
            for(i=1; i<pos; i++)
            {
                 if(cur==NULL)
                    break;

                prev=cur;
                cur=cur->link;


            }

            if(cur==NULL && i < pos)
                printf("\n Position invalid");
            else
            {
                prev->link=temp;
                temp->link=cur;
            }

            return first;
        }
    }
}

Node del(Node first)
{
    if(first==NULL)
        printf("\n List is Empty ");
    else
    {
        Node temp=first;
        printf("\n ID: %d was deleted",temp->id);
        first=first->link;
        free(temp);
    }
    return first;
}

void disply(Node first)
{
    if(first==NULL)
        printf("\n List is empty");
    else
    {
        Node cur=first;
        while(cur!=NULL)
        {
            printf("\n ID : ");
            printf("%d",cur->id);
            printf("\n Name : ");
            printf("%s",cur->name);
            printf("\n Semester : ");
            printf("%d",cur->sem);
            printf("\n\n\n");

            cur=cur->link;
        }
    }
}
int main()
{
    Node first=NULL;
    int opt;

    do
    {
            printf("\n QUEUE MENU\n 1.Insert at position  \n 2.delete front\n 3.display\n 4.Exit \n\n Enter your choice : ");
            scanf("%d",&opt);

            switch(opt)
            {
                case 1 :first = ins_pos(first);
                        break;

                case 2 :first = del(first);
                        break;

                case 3 :disply(first);
                        break;

            }


    }while(opt!=4);


    return 0;
}  

When inserting a new node, Code Blocks Crashes at the malloc statement. How do I know? well, it crashes before asking "Enter ID". So, am I doing something wrong?

Another point here is, it works fine with only an integer field in node, the problem here maybe the character array.

0

3 Answers 3

1

In this function Node getnode() -

Node temp=(Node)(malloc(sizeof(Node)));

With the above malloc you allocate memory equal to size of Node which is of type struct pointer , and is not enough .Therefore ,you get a segmentation fault.

instead of this ,write like this -

Node temp=malloc(sizeof(*temp));        //also there is no need of cast

This will allocate memory equal to size of type to which temp points to i.e size equal to that of structure. Which is appropriate .

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

2 Comments

I noticed this answer doesn't explain why this fixes the problem.
@immibis I have added the explanation . If you see any problem ,please feel free to point it out . Thanks !! :-)
1

Some errors here.

 malloc( sizeof( struct node ) );

Otherwise too little memory is allocated.

Do you include stdlib.h for malloc definition - that would cause this issue (no definition, defaults to int).

Comments

0

You need to use Node temp=(Node)(malloc(sizeof(*temp)));

1 Comment

Sorry, oversight. Corrected.

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.