Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, you will implement a dictionary to track the name and birth

ID: 3835544 • Letter: F

Question

For this assignment, you will implement a dictionary to track the name and birthday of your friends and relatives. You should be able to do the following operations:

- add an entry

- remove an entry

- search the dictionary for the birthday for a given name

- display the name and birthday of every entry in the dictionary

- list everyone in the dictionary who was born in a given month

The search key is the name, and you may assume that the names are unique.

Design and implement a C++ class to represent a person, which contains a name and a birthday at minimum. Additionally, design and implement a C++ class to represent an ADT dictionary of Person objects. Do not use an existing library class for the dictionary, you must implement your own. ( i dont really mind, but appriciate if able to hard code a class)

Searching for a person in your dictionary must be an average-case O(log n) operation; adding and removing can be O(n); and the "list everyone who was born in a given month" operation can be an O(n) operation.  With these requirements in mind, it is up to you which type of dictionary to use. You are free to leverage any work done in previous assignments.

You may create an interactive program which supports all of the required operations, or you may hard-code a sequence of operations that fully demonstrates your dictionary.

Explanation / Answer

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
# define MAX_DATA 10

typedef struct list
{
   string name;
   int day;
   int month;
   int year;
   struct list *next;
}struct_data;
struct_data *temp_data[MAX_DATA],*root[MAX_DATA],*tmp[MAX_DATA];

class Person
{
public:
int currIndex;

Person();
void addEntry(string,int,int,int);
void search(string);
void deleteEntry(string);
void display(void);
void displayByMonth(int);
};

Person::Person()
{
   currIndex=-1;
   for(int i=0;i<MAX_DATA;i++)
   {
       root[i]=NULL;
       temp_data[i]=NULL;
       tmp[i]=NULL;
   }
}

void Person::addEntry(string name, int day, int month, int year)
{
   currIndex=int(day%MAX_DATA);
   temp_data[currIndex]=(struct_data*)malloc(sizeof(struct_data));
   temp_data[currIndex]->name=name;
   temp_data[currIndex]->day=day;
   temp_data[currIndex]->month=month;
   temp_data[currIndex]->year=year;
   if(root[currIndex]==NULL)
   {
       root[currIndex]=temp_data[currIndex];
       root[currIndex]->next=NULL;
       tmp[currIndex]=temp_data[currIndex];
   }

   else
   {
       tmp[currIndex]=root[currIndex];
       while(tmp[currIndex]->next!=NULL)
           tmp[currIndex]=tmp[currIndex]->next;
       tmp[currIndex]->next=temp_data[currIndex];
   }
}

void Person::search(string name)
{
   int flag=0;
   currIndex=0;
   tmp[currIndex]=root[currIndex];
   while(tmp[currIndex]!=NULL)
   {
       if(tmp[currIndex]->name==name)
       {
           cout<<" Record wirh given name found..!!";
           cout<<"Name: "<<tmp[currIndex]->name<<" Day: "<<tmp[currIndex]->day<<" month: "<<tmp[currIndex]->month<<" year: "<<tmp[currIndex]->year<<endl;
           flag=1;
           break;
       }
       else tmp[currIndex]=tmp[currIndex]->next;
   }
   if (flag==0)
       cout<<" Record wirh given name was not found..!!";
}

void Person::deleteEntry(string name)
{
   currIndex=0;
   tmp[currIndex]=root[currIndex];
   while(tmp[currIndex]->name!=name && tmp[currIndex]!=NULL)
   {
       temp_data[currIndex]=tmp[currIndex];
       tmp[currIndex]=tmp[currIndex]->next;
   }
   temp_data[currIndex]->next=tmp[currIndex]->next;
   cout<<" "<<tmp[currIndex]->name<<" was deleted.";
   tmp[currIndex]=NULL;
   free(tmp[currIndex]);
}
//listing all records
void Person::display()
{
   currIndex=0;
   tmp[currIndex]=root[currIndex];
   while(tmp[currIndex]!=NULL)
   {
       cout<<"Name: "<<tmp[currIndex]->name<<" Day: "<<tmp[currIndex]->day<<" month: "<<tmp[currIndex]->month<<" year: "<<tmp[currIndex]->year<<endl;
       tmp[currIndex]=tmp[currIndex]->next;
   }
}
//listing by month
void Person::displayByMonth(int month)
{
   currIndex=0;
   tmp[currIndex]=root[currIndex];
   while(tmp[currIndex]->month==month && tmp[currIndex]!=NULL)
   {
       cout<<"Name: "<<tmp[currIndex]->name<<" Day: "<<tmp[currIndex]->day<<" month: "<<tmp[currIndex]->month<<" year: "<<tmp[currIndex]->year<<endl;
       tmp[currIndex]=tmp[currIndex]->next;
   }
}

int main()
{
   Person d;
   //adding records
   d.addEntry("johny",12,6,1992);
   d.addEntry("root",17,4,1986);
   //searching by name
   d.search("johny");
   //listing all records
   d.display();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote