Question

Implement a program that: reads a number of personal records (for example, using PERSON struct from...

Implement a program that:

  • reads a number of personal records (for example, using PERSON struct from the earlier lab) from the standard input,
  • creates a database of personal records,
  • allows for adding new entries to the database,
  • allows for deleting entries from the database,
  • includes functions to acquire a record of personal data, and
  • includes functions to display (print) a single selected record from the database, and
  • also allows for printing all records in the database.

NOTES:

  • if name is longer than 40 characters it should be truncated,
  • duplicates are allowed,
  • when removing entry, remove the first found.

The database application must be implemented using the linked list functions provided in list.c and the accompanying list.h.There are several functions that need to be completed in the list package.

All functions related to the operations on the database must be in the file person.c. There is the companion file person.h for keeping the data structures and function declarations.

A test driver is implemented in the main.c.

You must not modify any of the code. Instead, add the code that implements the missing functionality as indicated by the TODO comments.

#include "list.h"

void add(LIST **head, LIST **tail, void *data)
{
    if (*tail == NULL)
    {
        *head = *tail = (LIST *) malloc(sizeof(LIST));
        (*head)->data = data;
        (*head)->next = NULL;
    } else
    {
        (*tail)->next = (LIST *) malloc(sizeof(LIST));
        *tail = (*tail)->next;
        (*tail)->data = data;
        (*tail)->next = NULL;
    }
}

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)
{
    if (*currNode == NULL)
        return;

    LIST *nextNode;
    
    // TODO Complete this function

    clearRecursively(&nextNode, tail);

    *currNode = NULL;
    *tail = NULL;
}

void delete(LIST **head, LIST **tail, void *data)
{
    if (*head == NULL)
        return;

    if (data == NULL)
        return;

    if ((*head)->data == data)
    {
        LIST *newHead = (*head)->next;
        free((*head)->data);
        free(*head);
        *head = newHead;

        if (*head == NULL)
            *tail = NULL;

        return;
    }

    LIST *prevNode, *currNode;

    prevNode = (*head);
    currNode = (*head)->next;
    while (currNode != NULL)
    {
        // TODO Complete this function
    }
}
#ifndef LIST_H_
#define LIST_H_

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

typedef struct list
{
    void *data;
    struct list *next;
} LIST;

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);

#endif /* LIST_H_ */
#include "person.h"

LIST *head = NULL, *tail = NULL;

void inputPersonalData(PERSON *person)
{
    // TODO Implement the function
}

void addPersonalDataToDatabase(PERSON *person)
{
    // TODO Implement the function
}

void displayDatabase()
{
    // TODO Implement the function
}

void displayPerson(PERSON *person)
{
    // TODO Implement the function
}

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
}
#ifndef PERSON_H_
#define PERSON_H_

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

#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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "person.h"

#define DEF_NUM    1

int main(void)
{
    PERSON *person;

    int num;
    puts("Enter the initial number of records:");
    if (scanf("%d", &num) < 1)
        num = DEF_NUM;

    while (num-- > 0)
    {
        person = (PERSON *) malloc(sizeof(PERSON));
        inputPersonalData(person);
        addPersonalDataToDatabase(person);
    }

    displayDatabase();

    puts("\n--> Searching database for Maya");
    PERSON *maya = findPersonInDatabase("Maya");
    if (maya == NULL)
        puts("Maya not found");
    else
    {
        displayPerson(maya);
        puts("\n--> Removing Maya from database");
        removePersonFromDatabase("Maya");
    }
    displayDatabase();

// added to test finding and removing the last element
    puts("\n--> Searching database for Frank");
    PERSON *frank = findPersonInDatabase("Frank");
    if (frank == NULL)
        puts("Frank not found");
    else
    {
        displayPerson(frank);
        puts("\n--> Removing Frank from database");
        removePersonFromDatabase("Frank");
    }
    displayDatabase();

    puts("\n--> Removing Miro from database");
    removePersonFromDatabase("Miro");

    displayDatabase();


    puts("\n--> Adding new record to database");
    person = (PERSON *) malloc(sizeof(PERSON));
    inputPersonalData(person);
    addPersonalDataToDatabase(person);

    displayDatabase();

    puts("\n--> Clearing database");
    clearDatabase();

    displayDatabase();

    puts("\n--> Adding new record to database");
    person = (PERSON *) malloc(sizeof(PERSON));
    inputPersonalData(person);
    addPersonalDataToDatabase(person);

    displayDatabase();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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\n");
      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\n");
   return list;
      
}


void print(struct node *list){
     struct node *p = list;
     //printf("0000000000000000\n");
     while (p!= NULL){
         //printf("11111111111111111\n");
         printf("%s%s\n",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\n");
      return 0;
   }
   while (!feof(fp)){
       //printf("\n----------------------------\n");
       fscanf(fp,"%s%s",p.firstname, p.lastname);
       addperson(&pdb,p);
   }
   fclose(fp);
   printdb(&pdb);
  
}

Add a comment
Know the answer?
Add Answer to:
Implement a program that: reads a number of personal records (for example, using PERSON struct from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using struct.c, as a starting point, implement an application that creates a database of employee personal...

    Using struct.c, as a starting point, implement an application that creates a database of employee personal records. Your implementation should follow these guidelines: the definition of the structure PERSON should be provided in an h-file called person.h that you need to create in the src sub-directory, for the content, refer to the lecture notes, typdef should be used for referencing the PERSON structure, an array employees[] should be declared in the main C file (that is struct.c), a new C...

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • This program uses C++. This program reads in a line from the user and prints it...

    This program uses C++. This program reads in a line from the user and prints it out in a certain format. An example would be Input: 1 2 3 4 5 would result Output: [{1}, {2}, {3}, {4}, {5}]. When quotations marks are added into the input the format becomes different. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. When I ad multiple quotation marks into the input, it will only use...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a...

    Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a hashmap as the underlying data structure. Inspiration is ok, however the implementation must be yours. The hashmap must be able to grow to fit new elements, but it does not need to reduce its size. -- Linked lists? Positional arrays? All are fine. - Keys and values are strings (char *) - Operations are: get, put Provided files: - Implement the contract set in...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT