Home > front end >  Why is my print linked list function not working? (C language)
Why is my print linked list function not working? (C language)

Time:08-09

I have written a code to enter the name, age, department id, company name, and salary respectively, of employees from a text file into a linked list. I have created 3 functions to insert, display and update the list. I'm having trouble with the updateFile function, the inserting values part is working but when I call the display function again (to print updated list) it's not working. Can someone help me out with this? thanks!

employee.txt (text file):

Peter 30 1001 Apple 8000
Joseph 50 1002 Oracle 4000
Mary 40 1003 Samsung 6000
Lilly 40 1203 Samsung 7000
Tony 50 1002 Oracle 3000
Jake 30 1005 Apple 3000
Sam 40 1007 Samsung 4000
Lisa 30 1300 Oracle 5000
Kate 50 1200 Apple 6000
Rick 50 1313 Apple 4000

My Code:

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

struct personTag {
   char name[20];
    int age;
};

struct officialTag {
    int deptId;
    char cmpName[20];
    double salary;
};

struct employeeTag {
    struct personTag personalInfo;
    struct officialTag officialInfo;
    struct employeeTag *next;
};

typedef struct employeeTag EmpTag;
typedef EmpTag *EmpTagPtr;

typedef struct personTag person;
typedef struct officialTag official;


void insert(EmpTagPtr *s, char E_name[], int E_age, int E_deptid, char E_cmpname[], double 
E_salary);
void displayEmployees(EmpTagPtr s);
void updateFile(EmpTagPtr s, char E_name[], int E_age, int E_deptid, char E_cmpname[], 
double E_salary);


int main() {
    EmpTagPtr start = NULL;

    char E_name[20];
    int E_age;
    int E_deptid;
    char E_cmpname[20];
    double E_salary;

    // reading employee.txt file
    FILE *fp;
    fp = fopen("employee.txt", "r");
    //fp = stdin;

    while (fscanf(fp, "s %d %d s %lf", E_name, &E_age, &E_deptid, E_cmpname, 
&E_salary) == 5) 
    {
        insert(&start, E_name, E_age, E_deptid, E_cmpname, E_salary);
    }

    fclose(fp);

        int option;

    printf("(1) Display employee details\n");
    printf("(2) Add new employee to the record\n");
    printf("\n");

    printf("Enter an option : ");
    scanf("%d",&option);
    printf("\n");

    switch (option)
    {
        case 1:
        displayEmployees(start);
        break;

        case 2:
        updateFile(start, E_name, E_age, E_deptid, E_cmpname, E_salary);
        displayEmployees(start);
        break;
    }

    return 0;
}

void insert(EmpTagPtr *s, char E_name[], int E_age, int E_deptid, char E_cmpname[], double 
E_salary) 
{

    // create an empty node
    EmpTagPtr newNode = malloc(sizeof *newNode);

    // filling in the values
    strcpy(newNode->personalInfo.name, E_name);
    newNode->personalInfo.age = E_age;

    newNode->officialInfo.deptId = E_deptid;
    strcpy(newNode->officialInfo.cmpName, E_cmpname);
    newNode->officialInfo.salary = E_salary;

    while (*s && strcmp(newNode->personalInfo.name, (*s)->personalInfo.name) > 0) 
    {
        s = &(*s)->next;
    }

    // `s` now points at the `EmpTagPtr` pointer where the new node should be inserted:
    newNode->next = *s;
    *s = newNode;
}

void displayEmployees(EmpTagPtr s) 
{
    EmpTagPtr current = s;

    while (current != NULL) {
        // printing the data part
        printf("Employee name:    %s\n", current->personalInfo.name);
        printf("Company name:     %s\n", current->officialInfo.cmpName);
        printf("Employee Age:     %d\n", current->personalInfo.age);
        printf("Department ID:    %d\n", current->officialInfo.deptId);
        printf("Employee Salary:  %.2lf\n", current->officialInfo.salary);
        printf("---------------------------------------------------------\n");
        current = current->next;  // move foward the current pointer
    }

    printf("NULL\n");
    fflush(stdout);
}

void updateFile(EmpTagPtr s, char E_name[], int E_age, int E_deptid, char E_cmpname[], 
double E_salary)
{

    EmpTagPtr start = NULL;
    EmpTagPtr current = s;

    // writing to employee.txt file
    FILE *fp;
    fp = fopen("employee.txt", "a");
    //fp = stdin;

    printf("Enter Employee name :");
    scanf("%s",E_name);
    printf("\n");

    printf("Enter Company name  :");
    scanf("%s",E_cmpname);
    printf("\n");

    printf("Enter Employee Age  :");
    scanf("%d",&E_age);
    printf("\n");

    printf("Enter Department ID :");
    scanf("%d",E_deptid);
    printf("\n");

    printf("Enter Employee Salary :");
    scanf("%lf",E_salary);

    fprintf(fp, "s %d %d s %lf", E_name, &E_age, &E_deptid, E_cmpname, &E_salary);
    fclose(fp);
}

CodePudding user response:

You have type issue on updateFile, just activate compiler warnings. Your updateFile function is not updating s, you should add a node with the values:

insert(&s, E_name, E_age, E_deptid, E_cmpname, E_salary);

void updateFile(EmpTagPtr s, char E_name[], int E_age, int E_deptid, char E_cmpname[],-
double E_salary)
{

    EmpTagPtr start = NULL;
    EmpTagPtr current = s;

    // writing to employee.txt file
    FILE *fp;
    fp = fopen("employee.txt", "a");
    //fp = stdin;

    printf("Enter Employee name :");
    scanf("%s", E_name);
    printf("\n");

    printf("Enter Company name  :");
    scanf("%s", E_cmpname);
    printf("\n");

    printf("Enter Employee Age  :");
    scanf("%d", &E_age);
    printf("\n");

    printf("Enter Department ID :");
    scanf("%d", &E_deptid);
    printf("\n");

    printf("Enter Employee Salary :");
    scanf("%lf", &E_salary);

    fprintf(fp, "s %d %d s %lf", E_name, E_age, E_deptid, E_cmpname, E_salary);
    fclose(fp);
    insert(&s, E_name, E_age, E_deptid, E_cmpname, E_salary);
}

you can simplify the function signature using local variables


void updateFile(EmpTagPtr *s) {
    char E_name[20];
    int E_age;
    int E_deptid;
    char E_cmpname[20];
    double E_salary;


    // writing to employee.txt file
    FILE *fp;
    fp = fopen("employee.txt", "a");
    //fp = stdin;

    printf("Enter Employee name :");
    scanf("%s", E_name);
    printf("\n");

    printf("Enter Company name  :");
    scanf("%s", E_cmpname);
    printf("\n");

    printf("Enter Employee Age  :");
    scanf("%d", &E_age);
    printf("\n");

    printf("Enter Department ID :");
    scanf("%d", &E_deptid);
    printf("\n");

    printf("Enter Employee Salary :");
    scanf("%lf", &E_salary);

    fprintf(fp, "s %d %d s %lf", E_name, E_age, E_deptid, E_cmpname, E_salary);
    fclose(fp);
    insert(s, E_name, E_age, E_deptid, E_cmpname, E_salary);
}

CodePudding user response:

There is a problem in insert.
In using s to locate the position for newNode, that location becomes the new start of the list and prior nodes are lost. s should be modified when the list is empty or a node is inserted at the start of the list.

update only appends to the file. Nothing is inserted into the list. That isn't a problem with the current design as after appending to the file, the program returns. When the program is executed the next time, the appended record will be inserted. If the design is changed to allow multiple updates, then the list will need to be inserted as well.

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

struct personTag {
    char name[20];
    int age;
};

struct officialTag {
    int deptId;
    char cmpName[20];
    double salary;
};

struct employeeTag {
    struct personTag personalInfo;
    struct officialTag officialInfo;
    struct employeeTag *next;
};

typedef struct employeeTag EmpTag;
typedef EmpTag *EmpTagPtr;

typedef struct personTag person;
typedef struct officialTag official;


void insert(EmpTagPtr *s, char E_name[], int E_age, int E_deptid, char E_cmpname[], double E_salary);
void displayEmployees(EmpTagPtr s);
void updateFile(EmpTagPtr s, char E_name[], int E_age, int E_deptid, char E_cmpname[], double E_salary);

int main() {
    EmpTagPtr start = NULL;

    char E_name[20];
    int E_age;
    int E_deptid;
    char E_cmpname[20];
    double E_salary;

    // reading employee.txt file
    FILE *fp;
    fp = fopen("employee.txt", "r");
    //fp = stdin;

    while (fscanf(fp, "s %d %d s %lf", E_name, &E_age, &E_deptid, E_cmpname, &E_salary) == 5) {
        insert(&start, E_name, E_age, E_deptid, E_cmpname, E_salary);
    }

    fclose(fp);

    int option;

    printf("(1) Display employee details\n");
    printf("(2) Add new employee to the record\n");
    printf("\n");

    printf("Enter an option : ");
    scanf("%d",&option);
    printf("\n");

    switch (option) {
        case 1:
            displayEmployees(start);
            break;

        case 2:
            updateFile(start, E_name, E_age, E_deptid, E_cmpname, E_salary);
            displayEmployees(start);
            break;
    }

    return 0;
}

void insert(EmpTagPtr *s, char E_name[], int E_age, int E_deptid, char E_cmpname[], double E_salary) {
    // create an empty node
    EmpTagPtr newNode = malloc(sizeof *newNode);

    newNode->next = NULL;

    // filling in the values
    strcpy(newNode->personalInfo.name, E_name);
    newNode->personalInfo.age = E_age;

    newNode->officialInfo.deptId = E_deptid;
    strcpy(newNode->officialInfo.cmpName, E_cmpname);
    newNode->officialInfo.salary = E_salary;

    if ( ! *s) { // no other nodes
        *s = newNode;
    }
    else {
        EmpTagPtr temp = *s; // to iterate through the nodes
        EmpTagPtr last = *s; // to store the last node processed

        while ( temp) {
            if ( strcmp(newNode->personalInfo.name, temp->personalInfo.name) < 0) {
                if ( temp == *s) { // new first node
                    newNode->next = *s;
                    *s = newNode; // modify *s for new starting node
                }
                else {
                    last->next = newNode; // insert after last node processed
                    newNode->next = temp;
                }
                return;
            }
            last = temp; // keep last node processed
            temp = temp->next; // advance to next node
        }
        last->next = newNode; // did not return so append to end
    }
}

void displayEmployees(EmpTagPtr s) {
    EmpTagPtr current = s;

    while (current != NULL) {
        // printing the data part
        printf("Employee name:    %s\n", current->personalInfo.name);
        printf("Company name:     %s\n", current->officialInfo.cmpName);
        printf("Employee Age:     %d\n", current->personalInfo.age);
        printf("Department ID:    %d\n", current->officialInfo.deptId);
        printf("Employee Salary:  %.2lf\n", current->officialInfo.salary);
        printf("---------------------------------------------------------\n");
        current = current->next;  // move foward the current pointer
    }

    printf("NULL\n");
    fflush(stdout);
}

void updateFile(EmpTagPtr s, char E_name[], int E_age, int E_deptid, char E_cmpname[], double E_salary) {
    // writing to employee.txt file
    FILE *fp;
    fp = fopen("employee.txt", "a");
    //fp = stdin;

    printf("Enter Employee name :");
    scanf("%s",E_name);
    printf("\n");

    printf("Enter Company name  :");
    scanf("%s",E_cmpname);
    printf("\n");

    printf("Enter Employee Age  :");
    scanf("%d",&E_age);
    printf("\n");

    printf("Enter Department ID :");
    scanf("%d",&E_deptid);
    printf("\n");

    printf("Enter Employee Salary :");
    scanf("%lf",&E_salary);

    fprintf(fp, "s %d %d s %lf", E_name, E_age, E_deptid, E_cmpname, E_salary);
    fclose(fp);
}
  • Related