Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

list.c #include list.h void add(LIST **head, LIST **tail, void *data) { // TODO implement the function } void clearIteratively(LIST **head, LIST **tail) { if (*head

list.c
#include "list.h"
void add(LIST **head, LIST **tail, void *data)
{
// TODO implement the function
}
void clearIteratively(LIST **head, LIST **tail)
{
if (*head == NULL)
return;
LIST *currNode = *head;
LIST *nextNode = NULL;
do
{
nextNode = currNode->next;
if (currNode->data != NULL)
free(currNode->data);
free(currNode);
currNode = nextNode;
} while (currNode != NULL);
*head = NULL;
*tail = NULL;
}
void clearRecursively(LIST **currNode, LIST **tail)
{
// TODO implement the function
}
void delete(LIST **head, LIST **tail, void *data)
{
// TODO implement the function
}
person.c
#include "person.h"
LIST *head = NULL, *tail = NULL;
void inputPersonalData(PERSON *person)
{
// TODO implement the function
// or check out part 9 of the c tutorial ;)
}
void addPersonalDataToDatabase(PERSON *person)
{
// TODO Implement the function
}
void displayDatabase()
{
// TODO Implement the function
}
void displayPerson(PERSON *person)
{
// TODO Implement the function
// hmmmm seems familiar....
}
PERSON *findPersonInDatabase(char *name)
{
// TODO Implement the function
return NULL; // if not found
}
void removePersonFromDatabase(char *name)
{
// TODO Implement the function
}
void clearDatabase()
{
// TODO Implement the function
}
person.h
#ifndef PERSON_H_
#define PERSON_H_
#include
#include
#include
#include "list.h"
typedef char NAME[41];
typedef struct date
{
int month;
int day;
int year;
} DATE;
typedef struct person
{
NAME name;
int age;
float height;
DATE bday;
} PERSON;
void inputPersonalData(PERSON *person);
void addPersonalDataToDatabase(PERSON *person);
void displayDatabase();
void clearDatabase();
void displayPerson(PERSON *person);
PERSON *findPersonInDatabase(char *name);
void removePersonFromDatabase(char *name);
#endif /* PERSON_H_ */
#include
#include
#include
#include "person.h"
#define DEF_NUM 1
#include
#include
#include
#include "person.h"
#define DEF_NUM 1
int main(void)
{
// TODO replace stdin with the file input from ../in.txt
PERSON *person;
int num;
puts("Enter the initial number of records:");
if (scanf("%d", &num)
num = DEF_NUM;
printf(" --> Reading Personnel Records... ");
while (num-- > 0)
{
person = (PERSON *) malloc(sizeof(PERSON));
inputPersonalData(person);
addPersonalDataToDatabase(person);
}
printf(" --> Displaying Database... ");
displayDatabase();
puts(" --> Searching database for Maya... ");
PERSON *maya = findPersonInDatabase("Maya");
if (maya == NULL)
puts(" Maya not found... ");
else
{
displayPerson(maya);
puts(" --> Removing Maya from database... ");
removePersonFromDatabase("Maya");
}
displayDatabase();
// added to test finding and removing the last element
puts(" --> Searching database for Frank... ");
PERSON *frank = findPersonInDatabase("Frank");
if (frank == NULL)
puts(" Frank not found... ");
else
{
displayPerson(frank);
puts(" --> Removing Frank from database");
removePersonFromDatabase("Frank");
}
displayDatabase();
puts(" --> Removing Miro from database... ");
removePersonFromDatabase("Miro");
displayDatabase();
puts(" --> Adding new record to database... ");
person = (PERSON *) malloc(sizeof(PERSON));
inputPersonalData(person);
addPersonalDataToDatabase(person);
displayDatabase();
puts(" --> Clearing database... ");
clearDatabase();
displayDatabase();
puts(" --> Adding new record to database... ");
person = (PERSON *) malloc(sizeof(PERSON));
inputPersonalData(person);
addPersonalDataToDatabase(person);
displayDatabase();
clearDatabase();
}
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
C PROGRAM HELP
create an application which:
reads a specified number of personnel records from the standard input.
creates a database (in linked list form) of the data.
implemenets utilities to add to, delete from, search, display and clear the database.
Linked Lists
The skeleton of a linked list implementation is provided in list.h and list.c. Check out the struct definition of a LIST node in list.h to get an idea how data will be stored.
Your task is to complete and test this implementation. This will require you to fill out the definitions for the following functions in list.c:
void add(LIST **head, LIST **tail, void *data);
void clearRecursively(LIST **currNode, LIST **tail);
void delete(LIST **head, LIST **tail, void *data);
You will also need to make an additional main function to test the LIST utilities.
add :
The goal of the add function is to create a new linked list node, populate it with the data provided in the void *data, and append it to the linked list with head (i.e. the start of the list) referenced by LIST **head and tail (i.e. the end of the list) referenced by LIST **tail.
The following cases must be handled:
The list is empty (so head and tail are NULL).
The list has at least one element (so neiter head nor tail is NULL).
delete :
The goal of the delete function is to free the node containing the specified data pointer (and its contents), and then remove said node from the list.
The following cases must be handled:
The list is empty.
The list is not empty, but the specified data is not in the list.
The specified data is in the list.
The node being deleted is the head.
It is the only node (there is only 1 node).
It is not the only node (there are multiple nodes).
The node being deleted is neither the nead nor the tail.
The node being deleted is the tail (but you don't need to worry about it being the head too, because that case was handled above).
You may wish to draw pictures of each of the cases above to determine what needs to be done in each case.
Note that whenever the list is empty, the head and tail should both be NULL; otherwise, the head is the start of the list and the tail is the end of the list.
clearRecursively:
The goal of this function is to free every node in the list (and its content) and set the head and tail to NULL.
This is the recursive version of clearIteratively, which is provided.
Personnel Database
Once the linked list utilities are complete and tested, you are ready to move on to person.c, which contains skeletons for the following functions:
void inputPersonalData(PERSON *person);
void addPersonalDataToDatabase(PERSON *person);
void displayDatabase();
void displayPerson(PERSON *person);
PERSON *findPersonInDatabase(char *name);
void removePersonFromDatabase(char *name);
void clearDatabase();
These functions should work as follows:
inputPersonalData:
takes as input a pointer to allocated space for a PERSON.
populates the space referenced by that pointer with data gotten from stdin (which you've overwritten in the main with freopen to stream from ../in.txt.
addPersonalDataToDatabase:
takes as input a pointer to a populated instance of the PERSON struct.
adds this pointer to the linked list referenced by the LIST *s head and tail in person.c.
displayDatabase:
goes through the linked list referenced by head and tail, calling displayPerson on each data pointer in the list.
note that this will require casting each list node's data field as a PERSON * (it is stored as a generic void *).
displayPerson
takes as input a PERSON *.
displays the referenced data.
findPersonInDatabase:
takes as input a char *, the name of the person to be deleted.
finds the first occurence of a PERSON * stored in the linked list whose referenced name is the same as the input name and returns that PERSON *.
if no such person is in the database, returns NULL.
deletePersonFromDatabase:
takes as input a char *, the name of the person to be deleted.
finds the first person in the list with that name (if one exists) and deletes them.
clearDatabase:
deletes the linked list referenced by head and tail.
1-liner, use a utility defined in list.c.
Linked Lists The skeleton of a linked list implementation is provided in list.h and list.c. Check out the struct definition of a LIST node in list.h to get an idea how data will be stored. Your task is to complete and test this implementation. This will require you to fill out the definitions for the following functions in list.c: void add(LIST *head, LIST **tail, void *data); void clearRecursively (LIST **curr Node, LIST **tail); void delete(LIST **head, LIST **tail, void *data); You will also need to make an additional main function to test the LIST utilities. add : The goal of the add function is to create a new linked list node, populate it with the data provided in the void data, and prepend or append it to the linked list with head (i.e. the start of the list) referenced by LIST head and tail (i.e. the end of the list) referenced by LIST **tail. The following cases must be handled: The list is empty (so head and tail are NULL). The list has at least one element (so neiter head nor tail is NULL). delete: The goal of the delete function is to free the node containing the specified data pointer (and its contents), and then remove said node from the list. The following cases must be handled: The list is empty. The list is not empty, but the specified data is not in the list. The specified data is in the list. The node being deleted is the head. . It is the only node (there is only 1 node). . It is not the only node (there are multiple nodes). The node being deleted is neither the nead nor the tail. . The node being deleted is the tail (but you don't need to worry about it being the head too, because that case was handled above). You may wish to draw pictures of each of the cases above to determine what needs to be done in each case. Note that whenever the list is empty, the head and tail should both be NULL; otherwise, the head is the start of the list and the tail is the end of the list. clearRecursively: The goal of this function is to free every node in the list (and its content) and set the head and tail to NULL This is the recursive version of clearIteratively, which is provided. Personnel Database person.c contains skeletons for the following functions: void input Personal Data (PERSON *person); void addPersonalDataToDatabase (PERSON *person); void displayDatabase(); void displayPerson (PERSON *person); PERSON *findPerson InDatabase(char *name); void removePersonFromDatabase(char *name); void clearDatabase(); These functions should work as follows: input PersonalData: takes as input a pointer to allocated space for a PERSON populates the space referenced by that pointer with data gotten from with freopen to stream from ../in.txt. stdin (which you've overwritten in the main addPersonal DataToDatabase: takes as input a pointer to a populated instance of the PERSON struct. adds this pointer to the linked list referenced by the LIST *S head and tail in person.c. displayDatabase : goes through the linked list referenced by head and tail, calling displayPerson on each data pointer in the list. note that this will require casting each list node's data field as a PERSON * it is stored as a generic void *). >displayPerson takes as input a PERSON *. displays the referenced data. findPersonInDatabase : takes as input a char * , the name of the person to be deleted. finds the first occurence of a PERSON * stored in the linked list whose referenced name is the same as the input name and returns that PERSON *. if no such person is in the database, returns NULL deletePersonFromDatabase : takes as input a char * , the name of the person to be deleted. finds the first person in the list with that name (if one exists) and deletes them. clearDatabase : deletes the linked list referenced by head and tail. 1-liner, use a utility defined in list.c. Enter the initial number of records: -- Reading Personnel Records... --> Displaying Database... Zach : age 22, height 5.8, birthday 2/20/1989 Maya : age 21, height 5.9, birthday 8/2/1990 Ange : age 23, height 5.6, birthday 7/3/1987 Greg : age 22, height 6.2, birthday 4/15/1989 Matt : age 24, height 6.5, birthday 6/5/1986 Bob : age 50, height 8.0, birthday 3/23/1980 Frank : age 100, height 9.@, birthday 5/4/1923 --> Searching database for Maya... Maya : age 21, height 5.9, birthday 8/2/1990 --> Removing Maya from database... Zach : age 22, height 5.8, birthday 2/20/1989 Ange : age 23, height 5.6, birthday 7/3/1987 Greg ! age 22, height 6.2, birthday 4/15/1989 Matt ! age 24, height 6.5, birthday 6/5/1986 Bob : age 50, height 8.0, birthday 3/23/1980 Frank : age 100,height 9., birthday 5/4/1923 --> Searching database for Frank... Frank : age 100, height 9.8, birthday 5/4/1923 --> Removing Frank from database Zach : age 22, height 5.8, birthday 2/28/1989 Ange : age 23, height 5.6, birthday 7/3/1987 Greg : age 22, height 6.2, birthday 4/15/1989 Matt : age 24, height 6.5. birthday 6/5/1986 Bob : age 50, height 8.0, birthday 3/23/1980 --> Removing Miro from database... Zach : age 22, height 5.8, birthday 2/20/1989 Ange age 23, height 5.6, birthday 7/3/1987 Greg : age 22, height 6.2. birthday 4/15/1989 Matt : age 24, height 6.5, birthday 6/5/1986 Bob : age 50, height 8.0, birthday 3/23/1980 --> Adding new record to database... Zach: age 22, height 5.8, birthday 2/20/1989 Ange : age 23, height 5.6, birthday 7/3/1987 Greg : age 22, height 6.2, birthday 4/15/1989 Matt : age 24, height 6.5, birthday 6/5/1986 Bob : age 50, height 8.0, birthday 3/23/1900 AJ : age 100, height 88.0, birthday 1/1/1920 --> Clearing database... The database is empty. --> Adding new record to database... OtherAJ : age 1000, height 88.0, birthday 1/1/1920 #ifndef LIST_H_ #define LIST_H_ #include #include 7 8 typedef struct list { void *data; struct list *next; } LIST; 11 void add(LIST **head, LIST **tail, void *data); void clearIteratively (LIST **head, LIST **tail); void clearRecursively (LIST **head, LIST **tail); void delete (LIST **head, LIST **tail, void *data); 16 18 #endif /* LIST_H_ */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions