Search code examples
clinked-listmallocnodesunhandled-exception

C linked list and nodes program, why am I getting a unhanded exception?


How do I fix error I keep getting when I run the program? Im trying to make head point to the address of first and then be able to pass *first through a function where a new node will be created, the user can give it data at run-time, and then first will point to the new node! Am I doing this right?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void addToStart (struct node** head);
void Menu();
void DisplayList(struct node* head);

struct node{

    int data;
    struct node *next;

};


void main(){

    int option = 0;

    struct node *head;
    struct node *first;
    head = (struct node*)malloc(sizeof(struct node));
    first = (struct node*)malloc(sizeof(struct node));

    head->data= 0;
    head->next = first;
    first->data = 1;
    first->next = NULL;

    Menu();
    scanf(" %d", option);

    while(option != 6){
        switch(option){
        case 1:
            addToStart(&first);
            break;
        case 3:
            DisplayList(head);
        break;
    case 6:
        exit(0);
        break;

    default:
        printf("\nTry Again");
        break;
        }//switch end
    }//while end

}

void addToStart (struct node** first)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node");
scanf("%d", &newNode->data);
newNode->next = *first;
*first = newNode; // transfer the address of newNode' to 'head'
}

void Menu(){

    printf("1) Add a node.\n");
    printf("3) Display all nodes.\n");
    printf("6) Exit.\n");


}

void DisplayList(struct node* head){

    struct node *temp;
    temp =(struct node*)malloc(sizeof(struct node));
    temp = head;
    while( temp!= NULL )
    {
        printf("Data: %d", temp->data); // show the data
        temp = temp->next;
    }
}

Solution

  • #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node{
    
        int data;
        struct node *next;
    
    };
    
    void addToStart (struct node** head);
    void Menu();
    void DisplayList(struct node* head);
    
    
    
    
    int main(){
    
        int option = 0;
    
        struct node *head;
        //struct node *first;
        //head = (struct node*)malloc(sizeof(struct node));
        //first = (struct node*)malloc(sizeof(struct node));
    
        head = NULL;
    
        option = 0;
        while(option != 6){
            Menu();
            scanf(" %d", &option);
            switch(option){
    
                case 1:
                    addToStart(&head);
                                    break;
                case 3:
                    DisplayList(head);
                                    break;
                case 6:
                    exit(0);
    
                    break;
    
                default:
                    printf("\nTry Again");
                    break;
            }//switch end
        }//while end
    
    }
    
    void addToStart (struct node** head)
    {
        struct node *newNode;
        newNode = (struct node*)malloc(sizeof(struct node));
        printf("\nEnter data for this node");
        scanf("%d", &newNode->data);
        newNode->next = NULL;
        if (*head==NULL)
        {
            *head = newNode;
        }
        else
        {
            struct node *lastNode = *head;
            while(lastNode->next!=NULL)
                lastNode = lastNode->next;
            lastNode->next = newNode;
    
    
        }
    
       // *first = newNode; // transfer the address of newNode' to 'head'
    }
    
    void Menu(){
    
        printf("\n1) Add a node.\n");
        printf("3) Display all nodes.\n");
        printf("6) Exit.\n");
    
    
    }
    
    void DisplayList(struct node* head){
    
        struct node *temp;
        temp =(struct node*)malloc(sizeof(struct node));
        temp = head;
        while( temp!= NULL )
        {
            printf("Data: %d", temp->data); // show the data
            temp = temp->next;
        }
    }
    

    This works.

    Many points you should consider:

    1) it is int main, not void main()

    2) give address of variable you want to set value for when you do scanf. it should be scanf(" %d", &option); not scanf(" %d", option);

    3) When you made a new node, you were not setting its next to NULL. This was missing. newNode->next = NULL;

    4) You were in infinite loop. option was never updated. I added that. Also, Menu should be shown after user has given his choice.

    5) There is no check for memory allocation. What if malloc returns NULL?

    6) You were getting confused with first and head. Convention is to use head.

    7) I would use typedef , instead of struct node everytime. This is left as an exercise :)

    OP, this list is not exhaustive.

    If you want to display numbers in reverse order, change the else part in addToStart like this

    else
        {
            newNode->next = *head;
            *head = newNode;
        }