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

Please convert this following C++ program into C. Thank you for the help Contact

ID: 3820192 • Letter: P

Question

Please convert this following C++ program into C.

Thank you for the help

Contacts.cpp

#include <iostream>
#include "Contacts.h"
using namespace std;
ContactNode::ContactNode(){
nextNodePtr=NULL;
}
ContactNode::ContactNode(string name, string phoneNum){
contactName=name;
contactPhoneNum=phoneNum;
}

void ContactNode::InsertAfter(ContactNode *nextNode){
ContactNode *temp;
if(nextNodePtr==NULL)
nextNodePtr=nextNode;
else{
temp=nextNodePtr;
while(temp!=NULL){
temp=temp->GetNext();
}
temp=nextNode;
  
}
}

string ContactNode::GetName(){
return contactName;
}

string ContactNode::GetPhoneNumber(){
return contactPhoneNum;
}

ContactNode* ContactNode::GetNext(){
return nextNodePtr;
}

void ContactNode::PrintContactNode(){
ContactNode *temp;
cout<<"Contact Name: "<<GetName()<<endl;
cout<<"Phone number: "<<GetPhoneNumber()<<endl<<endl;
if(nextNodePtr!=NULL)
GetNext()->PrintContactNode();
}

Contacts.h

#ifndef Contact_H
#define Contact_H
#include<string>
using namespace std;
class ContactNode{
public:
ContactNode(); //constructor
ContactNode(string name, string phone);
void InsertAfter(ContactNode*);
  
string GetName();
string GetPhoneNumber();
ContactNode* GetNext();
  
void PrintContactNode();
private:
string contactName;
string contactPhoneNum;
ContactNode* nextNodePtr;
};
#endif

Main.cpp

#include <iostream>
#include "Contacts.cpp"
using namespace std;

int main(){
ContactNode contactList;
string name;
string phoneNum;
for(int i=0;i<3;i++){
cout<<"Enter name: ";
cin>>name;
cout<<"Enter phone number: ";
cin>>phoneNum;
  
  
contactList.InsertAfter(new ContactNode(name,phoneNum));
}
cout<<endl<<endl<<"CONTACT LIST"<<endl<<endl;
contactList.PrintContactNode();
return 0;
}

Explanation / Answer

PROGRAM CODE:


#include <stdio.h>
#include <string.h>
struct ContactNode
{
char contactName[20];
char contactPhoneNum[10];
struct ContactNode* nextNodePtr;
}*contactList;

void PrintContactNode()
{
struct ContactNode *temp = contactList;
while(temp != NULL)
{
printf(" Contact Name: %s Phone number: %s ", temp->contactName, temp->contactPhoneNum);
temp = temp->nextNodePtr;
}
}

void InsertAfter(struct ContactNode* node)
{
if(contactList == NULL)
{
contactList = (struct ContactNode *) malloc( sizeof(struct ContactNode) );
strcpy(contactList->contactName,node->contactName);
strcpy(contactList->contactPhoneNum, node->contactPhoneNum);
contactList->nextNodePtr = NULL;
}
else
{
struct ContactNode *temp = contactList;
while(temp->nextNodePtr != NULL)
{
temp = temp->nextNodePtr;
}
temp->nextNodePtr = (struct ContactNode *) malloc( sizeof(struct ContactNode) );
strcpy(temp->nextNodePtr->contactName,node->contactName);
strcpy(temp->nextNodePtr->contactPhoneNum, node->contactPhoneNum);
temp->nextNodePtr->nextNodePtr = NULL;
}
}

int main(void) {
contactList = NULL;
  
for(int i=0;i<3;i++){
char name[20];
char phoneNum[10];
printf(" Enter name: ");
scanf("%s", name);
printf(" Enter phone number: ");
scanf("%s", phoneNum);
struct ContactNode node;
strcpy(node.contactName , name);
strcpy(node.contactPhoneNum , phoneNum);
node.nextNodePtr = NULL;
InsertAfter(&node);
}
PrintContactNode();
return 0;
}

OUTPUT:

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