In: Computer Science
C++ Only Please
10.15 LAB: Warm up: Contacts
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
(1) Create three files to submit.
(2) Build the ContactNode class per the following specifications:
Parameterized constructor. Parameters are name followed by phone number.
Public member functions
Private data members
Ex. of PrintContactNode() output:
Name: Roxanne Hughes Phone number: 443-555-2864
(3) In main(), prompt the user for three contacts and output the
user's input. Create three ContactNodes and use the nodes to build
a linked list. (2 pts)
Ex:
Person 1 Enter name: Roxanne Hughes Enter phone number: 443-555-2864 You entered: Roxanne Hughes, 443-555-2864 Person 2 Enter name: Juan Alberto Jr. Enter phone number: 410-555-9385 You entered: Juan Alberto Jr., 410-555-9385 Person 3 Enter name: Rachel Phillips Enter phone number: 310-555-6610 You entered: Rachel Phillips, 310-555-6610
(4) Output the linked list. (2 pts)
Ex:
CONTACT LIST Name: Roxanne Hughes Phone number: 443-555-2864 Name: Juan Alberto Jr. Phone number: 410-555-9385 Name: Rachel Phillips Phone number: 310-555-6610
I have tried the following 3 files:
main.cpp
#include "ContactNode.h"
#include<stdlib.h>
#include<stdio.h>
int main() {
// Declare variable.
char contactName[50];
char contactPhoneNum[50];
// Declare pointers.
ContactNode *head = NULL;
ContactNode *tail = NULL;
// declare number of links.
ContactNode *one;
ContactNode *two;
ContactNode *three;
printf("Person 1\n");
// prompt the user to enter name
printf("Enter name:\n");
scanf("%[^\n]%*c",contactName);
// prompt the user to enter phone number.
printf("Enter phone number:\n");
scanf("%[^\n]%*c",contactPhoneNum);
printf("You entered: %s, %s\n",contactName,contactPhoneNum);
// call the function.
one = CreateContactNode(contactName,contactPhoneNum);
printf("\nPerson 2\n");
// prompt the user to enter name
printf("Enter name:\n");
scanf("%[^\n]%*c",contactName);
printf("Enter phone number:\n");
// prompt the user to enter phone number.
scanf("%[^\n]%*c",contactPhoneNum);
printf("You entered: %s, %s\n",contactName,contactPhoneNum);
// call the function.
two = CreateContactNode(contactName,contactPhoneNum);
printf("\nPerson 3\n");
// prompt the user to enter name
printf("Enter name:\n");
scanf("%[^\n]%*c",contactName);
printf("Enter phone number:\n");
// prompt the user to enter phone number.
scanf("%[^\n]%*c",contactPhoneNum);
printf("You entered: %s, %s\n",contactName,contactPhoneNum);
// call the function.
three = CreateContactNode(contactName,contactPhoneNum);
// call the function.
InsertContactAfter(&head,&tail,one);
InsertContactAfter(&head,&tail,two);
InsertContactAfter(&head,&tail,three);
// call the function.
PrintContactNode(head);
return 0;
}
ContactNode.cpp
#include "ContactNode.h"
#include<malloc.h>
#include<stdio.h>
#include<string.h>
// definition of the function.
ContactNode* allocateMemory() {
// return value.
return (ContactNode *) malloc(sizeof(ContactNode));
}
// definition of the function.
ContactNode* CreateContactNode(char contactName[], char
contactPhoneNum[]) {
// alllocate the memory.
ContactNode *newNode = allocateMemory();
// check the condition
if(newNode == NULL) {
printf("Stack Overflow error");
return NULL;
}
// copy the string in variable.
strcpy(newNode->contactName,contactName);
strcpy(newNode->contactPhoneNum,contactPhoneNum);
newNode->nextNodePtr = NULL;
// return newnode.
return newNode;
}
// Definition of the function.
void InsertContactAfter(ContactNode **head,ContactNode
**tail,ContactNode *newNode) {
// Check the condition.
if(newNode == NULL) {
return;
}
// check the condition.
if(*head == NULL) {
// assign value.
*head = newNode;
*tail = newNode;
return;
}
// assign value.
(*tail)->nextNodePtr = newNode;
*tail = GetNextContact(*tail);
}
// definition of the function.
ContactNode * GetNextContact(ContactNode *node) {
// check the condition.
if(node == NULL) {
return NULL;
}
// return value.
return node->nextNodePtr;
}
// definition of the function.
void PrintContactNode(ContactNode *head) {
// check the condition.
if(head == NULL) {
printf("\nContacts are Empty.\n");
return;
}
// assign value.
ContactNode*current = head;
printf("\nCONTACT LIST\n");
// start the while loop
while(current) {
// display the result.
printf("Name: %s\n",current->contactName);
printf("Phone number: %s\n\n", current->contactPhoneNum);
current = GetNextContact(current);
}
}
ContactNode.h
#ifndef CONTACTNODE_H
#define CONTACTNODE_H
// Declare the structure.
typedef struct ContactNode {
// declare an character array.
char contactName[50];
char contactPhoneNum[50];
struct ContactNode *nextNodePtr;
}
ContactNode;
// Declare the functions.
ContactNode* CreateContactNode(char [], char []);
void InsertContactAfter(ContactNode **,ContactNode **,ContactNode
*);
ContactNode* GetNextContact(ContactNode *);
void PrintContactNode(ContactNode *);
#endif
And am only getting 4/9 points in credit. I can't seem to figure out why. I tried to include my results but it was too long.
Thank you for the help in advance. I can't figure out what I'm doing wrong for the full credit.
i build link list your code has some problem in many place so i set it
main.cpp
#include "ContactNode.cpp"
#include<stdlib.h>
#include<stdio.h>
int main() {
// Declare variable.
char contactName[50];
char contactPhoneNum[50];
// Declare pointers.
ContactNode *head = NULL;
ContactNode *tail = NULL;
// declare number of links.
ContactNode *one;
ContactNode *two;
ContactNode *three;
printf("Person 1\n");
// prompt the user to enter name
printf("Enter name:\n");
scanf("%[^\n]%*c",contactName);
// prompt the user to enter phone number.
printf("Enter phone number:\n");
scanf("%[^\n]%*c",contactPhoneNum);
printf("You entered: %s, %s\n",contactName,contactPhoneNum);
// call the function.
one = CreateContactNode(contactName,contactPhoneNum);
printf("\nPerson 2\n");
// prompt the user to enter name
printf("Enter name:\n");
scanf("%[^\n]%*c",contactName);
printf("Enter phone number:\n");
// prompt the user to enter phone number.
scanf("%[^\n]%*c",contactPhoneNum);
printf("You entered: %s, %s\n",contactName,contactPhoneNum);
// call the function.
two = CreateContactNode(contactName,contactPhoneNum);
printf("\nPerson 3\n");
// prompt the user to enter name
printf("Enter name:\n");
scanf("%[^\n]%*c",contactName);
printf("Enter phone number:\n");
// prompt the user to enter phone number.
scanf("%[^\n]%*c",contactPhoneNum);
printf("You entered: %s, %s\n",contactName,contactPhoneNum);
// call the function.
three = CreateContactNode(contactName,contactPhoneNum);
// call the function.
InsertContactAfter(&head,&tail,one);
InsertContactAfter(&head,&tail,two);
InsertContactAfter(&head,&tail,three);
// call the function.
PrintContactNode(head);
return 0;
}
ContactNode.h
#ifndef CONTACTNODE_H
#define CONTACTNODE_H
// Declare the structure.
typedef struct ContactNode {
// declare an character array.
char contactName[50];
char contactPhoneNum[50];
struct ContactNode *nextNodePtr;
}
ContactNode;
// Declare the functions.
ContactNode* CreateContactNode(char [], char []);
void InsertContactAfter(ContactNode **,ContactNode **,ContactNode
*);
ContactNode* GetNextContact(ContactNode *);
void PrintContactNode(ContactNode *);
#endif
ContactNode.cpp
#include "ContactNode.h"
#include<malloc.h>
#include<stdio.h>
#include<string.h>
// definition of the function.
ContactNode* allocateMemory() {
// return value.
return (ContactNode *) malloc(sizeof(ContactNode));
}
// definition of the function.
ContactNode* CreateContactNode(char contactName[], char
contactPhoneNum[]) {
// alllocate the memory.
ContactNode *newNode = allocateMemory();
// check the condition
if(newNode == NULL) {
printf("Stack Overflow error");
return NULL;
}
// copy the string in variable.
strcpy(newNode->contactName,contactName);
strcpy(newNode->contactPhoneNum,contactPhoneNum);
newNode->nextNodePtr = NULL;
// return newnode.
return newNode;
}
// Definition of the function.
void InsertContactAfter(ContactNode **head,ContactNode
**tail,ContactNode *newNode) {
// Check the condition.
if(newNode == NULL) {
return;
}
// check the condition.
if(*head == NULL) {
// assign value.
*head = newNode;
*tail = newNode;
return;
}
// assign value.
(*tail)->nextNodePtr = newNode;
*tail = GetNextContact(*tail);
}
// definition of the function.
ContactNode * GetNextContact(ContactNode *node) {
// check the condition.
if(node == NULL) {
return NULL;
}
// return value.
return node->nextNodePtr;
}
// definition of the function.
void PrintContactNode(ContactNode *head) {
// check the condition.
if(head == NULL) {
printf("\nContacts are Empty.\n");
return;
}
// assign value.
ContactNode*current = head;
printf("\nCONTACT LIST\n");
// start the while loop
while(current) {
// display the result.
printf("Name: %s\n",current->contactName);
printf("Phone number: %s\n\n", current->contactPhoneNum);
current = GetNextContact(current);
}
}
Pleause thumbs up if you satisfy with Answer
Comment down if any Query