make a program in c that will read a number of personal records (for example, us
ID: 3898179 • Letter: M
Question
make a program in c that will read a number of personal records (for example, using PERSON struct),
creates a database of personal records using a linked list,
allows for deleting and adding new entries to the database, and
includes functions to acquire a record of personal data and a function to display (print) all records in the database.
Keep all functions that implement the linked list in list.c. The accompanying file list.h should include declarations of the data structures and functions necessary for operations on lists. All functions that relate to the database should be in the file personnel.c. Create also accompanying personnel.h for keeping the data structures and function declarations related to the maintenance of the database. The main.c should implement a comprehensive test driver.
Memory management for the list nodes should be in list.c, while memory management for the data records should be in personnel.c
Explanation / Answer
list.h
#include<stdio.h>
#include<string.h>
struct person{
char firstname[30];
char lastname[30];
};
struct node{
struct person data;
struct node *next;
};
struct node *insertItem(struct node *list, struct person p);
struct node *removeItem(struct node *list, struct person p);
void print(struct node *list);
list.c
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include "list2.h"
struct node *insertitem(struct node *list, struct person p){
struct node *q = (struct node *)malloc(sizeof(struct node));
q->next = NULL;
strcpy(q->data.firstname, p.firstname);
strcpy(q->data.lastname, p.lastname);
if (list == NULL)
list = q;
else {
struct node *p = list;
while(p->next != NULL)
p = p->next;
p->next = q;
}
return list;
}
struct node *removeitem(struct node *list, struct person s){
int found = 0;
if (list == NULL){
printf("List is empty ");
return list;
}
else {
struct node *p = list;
if (strcmp(p->data.firstname, s.firstname) == 0 && strcmp(p->data.lastname, s.lastname) == 0){
found = 1;
list = list->next;
free(p);
}
else {
struct node *q = p;
p = p->next;
while (p!= NULL && strcmp(p->data.firstname, s.firstname) != 0 && strcmp(p->data.lastname, s.lastname) != 0){
p= p->next;
q=q->next;
}
if (p!= NULL){
q->next = p->next;
free(p);
found = 1;
}
}
}
if (found == 0)
printf("Not found ");
return list;
}
void print(struct node *list){
struct node *p = list;
//printf("0000000000000000 ");
while (p!= NULL){
//printf("11111111111111111 ");
printf("%s%s ",p->data.firstname, p->data.lastname);
p = p->next;
}
}
personal.h
#include<stdio.h>
#include "list2.h"
struct personaldb{
struct node *list;
};
void addperson(struct personaldb *pdb, struct person p);
void removeperson(struct personaldb *pdb,struct person p);
void printdb(struct personaldb *pdb);
personal.c
#include "personal.h"
void addperson(struct personaldb *pdb, struct person p){
pdb->list = (struct node *)insertitem(pdb->list,p);
}
void removeperson(struct personaldb *pdb, struct person p){
pdb->list = (struct node *)removeitem(pdb->list,p);
}
void printdb(struct personaldb *pdb){
print(pdb->list);
}
main.c
#include<stdio.h>
#include "personal.h"
int main(){
FILE *fp;
struct personaldb pdb;
struct person p;
pdb.list = NULL;
fp = fopen("personal.txt","r");
if (fp == NULL){
printf("Error opening file ");
return 0;
}
while (!feof(fp)){
//printf(" ---------------------------- ");
fscanf(fp,"%s%s",p.firstname, p.lastname);
addperson(&pdb,p);
}
fclose(fp);
printdb(&pdb);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.