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. Right now, In my code, I have created an insert function and a display function. When I ran the code on the command prompt there were no errors found. However, my list did not get printed. Can someone help me out with this if possible? thankyou.
Here are the details of the file, it is called employee.txt:
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;
void insert(EmpTagPtr *s, char E_name[], int E_age, int E_deptid,
char E_cmpname[], double E_salary);
void displayEmployees(EmpTagPtr s);
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");
fscanf(fp, "%s,%d,%d,%s,%lf", E_name, &E_age, &E_deptid, E_cmpname,
&E_salary);
while (!feof(fp)) {
insert(&start, E_name, E_age, E_deptid, E_cmpname,
E_salary); // inserting data to the new node
fscanf(fp, "%s,%d,%d,%s,%lf", E_name, &E_age, &E_deptid, E_cmpname,
&E_salary);
}
fclose(fp);
displayEmployees(start);
return 0;
}
void insert(EmpTagPtr *s, char E_name[], int E_age, int E_deptid,
char E_cmpname[], double E_salary) {
EmpTagPtr current = *s;
EmpTagPtr prev = NULL;
// create an empty node
EmpTagPtr newNode;
newNode = (EmpTag *)malloc(sizeof(EmpTag));
// 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;
newNode->next = NULL;
while (current != NULL &&
strcmp(current->personalInfo.name, prev->personalInfo.name) > 0) {
prev = current;
current = current->next;
}
if (prev == NULL) {
// add as the first node
newNode->next = *s;
*s = newNode;
} else {
prev->next = newNode;
newNode->next = current;
}
}
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: %lf\n", current->officialInfo.salary);
printf("---------------------------------------------------------");
current = current->next; // move foward the current pointer
}
printf("NULL\n");
}