
Patrick B. answered 03/03/21
Math and computer tutor/teacher
//ContactNode.h
using namespace std;
#ifndef _CONTACT
#define _CONTACT
#include <string>
typedef
class ContactNode
{
private:
string contactName;
string contactPhoneNum;
ContactNode * next;
public:
ContactNode ( string nameStr, string phoneNumStr)
{
contactName = nameStr;
contactPhoneNum = phoneNumStr;
next = NULL;
}
string GetPhoneNumber() { return(contactPhoneNum); }
string GetName() { return(contactName); }
ContactNode* GetNext() { return(next); }
void InsertAfter( ContactNode * nodePtr) { next = nodePtr; }
void PrintContactNode(string * strMsg=NULL);
} * TContactNode;
#define CONTACT_NODE_SIZE ( sizeof(ContactNode))
typedef
class ContactList
{
private:
TContactNode head;
TContactNode tail;
TContactNode cur;
int count;
public:
ContactList()
{
head=tail=NULL;
count=0;
}
int GetCount() { return(count); }
void MoveFirst() { cur=head; }
void MoveLast() { cur=tail; }
void MoveNext()
{
if (cur==tail)
{
cur=head;
}
else
{
cur=cur->GetNext();
}
}
ContactNode& Get()
{
return(*cur);
}
void InsertAddNew( string nameStr, string phoneNum);
~ContactList();
} * TContactList;
#define CONTACT_LIST_SIZE (sizeof(ContactList))
#endif
// ContactNode.cpp --------------------------------------------
using namespace std;
#include <iostream>
#include <string>
#ifndef _CONTACT
#include "contactNode.h"
#endif
void ContactNode::PrintContactNode(string *strMsg)
{
if (strMsg!=NULL)
{
cout << "**********************************************" << endl;
cout << *strMsg << endl;
}
cout << "*****************************************" << endl;
cout << " contact Name :>" << contactName << "<" << endl;
cout << " contact Phone #>" << contactPhoneNum << "<" << endl;
}
void ContactList::InsertAddNew( string nameStr, string phoneNum)
{
TContactNode newNode = new ContactNode(nameStr,phoneNum);
if (count==0)
{
head=tail=newNode;
}
else
{
tail->InsertAfter(newNode);
tail = tail->GetNext();
}
count++;
}
ContactList::~ContactList()
{
TContactNode tempNodePtr = head;
for (int iLoop=0; iLoop<count; iLoop++)
{
head = head->GetNext();
delete(tempNodePtr);
tempNodePtr = head;
}
}
//-------------- Main.cpp -----------------------------------------//
using namespace std;
#include <iostream>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#ifndef _CONTACT
#include "contactNode.h"
#endif
int main(int argc, char** argv)
{
ContactList contactList;
string contactNameStr;
string contactPhoneNum;
int N=3;
//cout << "How many ??? :>"; cin >> N;
for (int iLoop=0; iLoop<N; iLoop++)
{
cout << "------- Person " << (iLoop+1) << " of " << N << "-----------" << endl;
cout << "Please input the name :>";
cin >> contactNameStr;
cout << "Please input the phone number :>";
cin >> contactPhoneNum;
cout << " You input: " << contactNameStr << "," << contactPhoneNum << endl<<endl;
contactList.InsertAddNew(contactNameStr,contactPhoneNum);
}
cout << "Contact List" << endl;
contactList.MoveFirst();
for (int iLoop=0; iLoop<N; iLoop++)
{
ContactNode curContact = contactList.Get();
curContact.PrintContactNode();
contactList.MoveNext();
}
return 0;
}