0

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;
} 
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
Maheshi
  • 1
  • 2
  • 2
    `int main(struct node*head)` That is not a valid `main` signature. See [What are the valid signatures for C's main() function?](https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function) – kaylum Nov 29 '21 at 02:08
  • 2
    You are not initialising head's next pointer – which will result in error for requested size of 1, on the other hand you later on initialise by NULL but overwrite anyway by all but last element. Simpler: Just set `temp->next` to NULL *after* the loop (last element...). – Aconcagua Nov 29 '21 at 02:10
  • `char name;... scanf("%s",&temp->name);` Here `name` is single character, `scanf` will try to write a string to, causes buffer overflow. Your `case` statements are missing `break;` Please work on this a bit more. Your compiler should issue warnings to help you. A good debugger can pick out a lot of bugs. Also, don't try to write the whole program at once. Break it in to smaller parts, first get the input right. Then write a linked list and put it together. – Barmak Shemirani Nov 29 '21 at 02:13
  • Do *not* call `selectMenu` recursively – this *will* result in stackoverflow if only the programme runs long enough. Instead write a loop. – Aconcagua Nov 29 '21 at 02:13
  • `createList` returns a pointer to the new head – you need to store that somewhere. And you have a memory leak: If you create a new list while one is existing already, you need to first free all the existing nodes before. – Aconcagua Nov 29 '21 at 02:17
  • Proper indentation and fewer empty lines make the code more readable! – Aconcagua Nov 29 '21 at 02:20
  • `searchemp` ends up in and endless loop *if* you find the employee you are looking for as you won't modify the current pointer any more. And please don't use globals for such purposes, just have another local one. – Aconcagua Nov 29 '21 at 02:23
  • @kaylum Oh i see. I will check it out. Thank you so much for the help – Maheshi Nov 29 '21 at 04:03
  • @Aconcagua Thanks a heap for the help!!! I will try to correct those. – Maheshi Nov 29 '21 at 04:06
  • If *head* is a pointer to a node: node * head, then to pass a pointer to head in C the caller uses | function(...&head) | and the calle uses | function(... node **head) | . – rcgldr Nov 29 '21 at 20:44
  • @rcgldr Thank you so much for the help!! – Maheshi Dec 03 '21 at 17:44

0 Answers0