Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify this program by implementing linked list, instead of using array. Creat a linked list that consists of the nodes for every instances created from

Modify this program by implementing linked list, instead of using array. Creat a linked list that consists of the nodes for every instances created from class. Implement link list operation such as add, delete, and search, Provide menu for the user to choose the operation he likes to perform.

Show me the full program after modify pls!!!

#include

#include

#include

#include

using namespace std;

class Contact{

private:

string hpno; // handphone number

string cn; // contact name

string msg; // message

public:

Contact(){};

void set_hpno(string hpno_){ hpno = hpno_;}

void set_cn(string cn_){ cn = cn_;}

void set_msg(string msg_){ msg = msg_;}

string get_hpno(){ return hpno;}

string get_cn(){ return cn;}

string get_msg(){ return msg;}

void viewContact(){

ifstream contact_file;

contact_file.open("Contact.txt", ios::in);

if (contact_file.is_open()){

string d;

while (getline(contact_file, d)){

cout << d << endl;

}

contact_file.close();

}

}

void searchContact(){

ifstream contact_file;

contact_file.open("Contact.txt", ios::in);

int curLine = 0;

char search;

string line;

while(getline(contact_file, line)) {

curLine++;

if (line.find(search, 0) != string::npos) {

cout << "Search Result: " << search << "Line: " << curLine << endl;

}

}

contact_file.close();

}

bool compare(const pair& a, const pair& b) {

return a.first < b.first;

}

void sortContact(string arrhpno[], string arrcn[], string arrmsg[], int n)

{

for( int i = 1; i< n ;i++ )

{

for( int j=0; j

{

if(arrhpno[j] > arrhpno[j+1])

{

string temphpno;

string tempcn;

string tempmsg;

temphpno = arrhpno[j];

tempcn = arrcn[j];

tempmsg = arrmsg[j];

arrhpno[j] = arrhpno[j+1];

arrcn[j] = arrcn[j+1];

arrmsg[j] = arrmsg[j+1];

arrhpno[j+1] = temphpno;

arrcn[j+1] = tempcn;

arrmsg[j+1] = tempmsg;

}

}

}

}

void addContact(fstream & contact_file){

contact_file << hpno << endl;

contact_file << cn << endl;

}

void editContact(){

ifstream contact_file;

string replace1, replace2;

cout << "(1) Edit Handphone Number" << " " << "(2) Edit Contact Name" << "(3) Exit Edit Contact" << endl;

int s;

cin >> s;

contact_file.open("Contact.txt",ios::in);

while(s != 0){

switch(s){

case 1: // edit handphone number

cout << "New Handphone Number: ";

getline(cin,replace1);

break;

case 2: // edit contact name

cout << "New Contact Name: ";

getline(cin,replace2);

break;

case 3:

break;

}

}

}

void deleteContact(string phone){

ifstream contact_file;

contact_file.open("Contact.txt", ios::in);

ofstream temp_file;

temp_file.open("temp.txt", ios::out);

string hpno, cn;

while (contact_file >> hpno >> cn) {

if (hpno != phone){

temp_file << hpno << " " << cn << endl;

}

}

contact_file.close();

temp_file.close();

rename("temp.txt", "Contact.txt");

}

void sendMessage(){

ofstream msg_file;

string message;

msg_file.open("Log.txt", ios::out|ios::app);

cout << "Message: ";

getline(cin,message);

msg_file << message;

}

void retrieveMessage(){

ifstream log_file("log.txt");

if (log_file.is_open()){

string line;

while (getline(log_file, line))

{

size_t delimiterPos = line.find(" ");

string linePhoneNumber = line.substr(0, delimiterPos);

string lineMessage = line.substr(delimiterPos + 1);

string PhoneNumber;

if (linePhoneNumber == PhoneNumber){

cout << "Message from" << linePhoneNumber << ":" << lineMessage << endl;

}

}

log_file.close();

} else {

cout << "Error: Could not open log file." << endl;

}

}

void viewLog(){

ifstream msg_file;

msg_file.open("Log.txt", ios::in);

if (msg_file.is_open()){

string d;

while (getline(msg_file, d)){

cout << d << endl;

}

msg_file.close();

}

}

};

int main(){

int i, location;

int n = 5;

Contact c[n];

string arrhpno[n];

string arrcn[n];

string arrmsg[n];

for(i = 0; i < n; i++)

{

arrhpno[i] = c[i].get_hpno();

arrcn[i] = c[i].get_cn();

arrmsg[i] = c[i].get_msg();

}

system("pause");

return 0;

}

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

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions

Question

Describe Karens typical relationships with men.

Answered: 1 week ago