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