First of all I'm new to C language and recently started learning. The following code has been developed with my current knowledge. I was asked to develop a menu driven program involving linked lists to get details from the user and to print and search them using functions. Further, i was advised to create the pointer of the head pointer as a local variable. The problem I'm having is, it gives a runtime error when i try to print or search the data that are entered by the user. And I can't figure out where I have coded wrong. If someone can help me to fix this problem it would be really appreciated.
The code that I've developed so far is ,
#include<stdio.h>
#include<stdlib.h>
struct node {
int number;
char name;
int salary;
struct node*next;
};
int op,num,i=0,j;
struct node*temp;
//creating the list
struct node* createlist(){
printf("\nEnter the number of employees : ");
scanf("%d",&num);
struct node*head;
for(j=0;j<num;j++){
if(i==0){
head=(struct node*)malloc(sizeof(struct node));
temp=head;
}
else{
temp->next=(struct node*)malloc(sizeof (struct node));
temp=temp->next;
temp->next=NULL;
}
printf("\nEnter the employee number : ");
scanf("%d",&temp->number);
printf("Enter the name : ");
scanf("%s",&temp->name);
printf("Enter the salary : ");
scanf("%d",&temp->salary);
printf("\n");
}
return head;
}
//printing the list
void printlist(struct node*head)
{
if(temp==NULL){
printf("\n\nENTER THE DATA BEFORE PRINT!!!!\n\n");
}
else{
temp=head;
while(temp!=NULL){
printf("%d\t\t%s\t\t%d\n",temp->number,temp->name,temp->salary);
temp=temp->next; }
}
}
void searchemp(struct node*head) {
if(temp==NULL){
printf("\n\nENTER THE DATA BEFORE SEARCH!!!!\n\n");
}
else{
printf("Enter the employee number : ");
int nm;
scanf("%d",&nm);
temp=head;
while(temp!=NULL){
if(temp->number==nm){
printf("%d\t\t%s\t\t%d\n",temp->number,temp->name,temp->salary);
}
else{
temp=temp->next ;
}
}
selectmenu(head);
}
}
//creating the menu
int selectmenu (struct node*head){
printf("=========EMPLOYEE REGISTRATION SYSTEM=========\n");
printf("1.Enter Employee Details\n2.Output Employee Details\n3.Search Employee\n4.Exit\n");
printf("Enter the option : ");
scanf("%d",&op);
switch (op){
case 1:
createlist();
selectmenu(head);
case 2:
printlist(head);
selectmenu(head);
case 3:
searchemp(head);
selectmenu(head);
case 4:
break;
}
}
int main(struct node*head){
selectmenu(head);
return 0;
}