Question

In: Computer Science

C++ Only Please 10.15 LAB: Warm up: Contacts You will be building a linked list. Make...

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.

  • ContactNode.h - Class declaration
  • ContactNode.cpp - Class definition
  • main.cpp - main() function

(2) Build the ContactNode class per the following specifications:

  • Parameterized constructor. Parameters are name followed by phone number.

  • Public member functions

    • InsertAfter() (2 pts)
    • GetName() - Accessor (1 pt)
    • GetPhoneNumber - Accessor (1 pt)
    • GetNext() - Accessor (1 pt)
    • PrintContactNode()
  • Private data members

    • string contactName
    • string contactPhoneNum
    • ContactNode* nextNodePtr


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.

Solutions

Expert Solution

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  


Related Solutions

8.13 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep...
8.13 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. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please 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. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
The aim of this lab session is to make you familiar with implementing a linked list....
The aim of this lab session is to make you familiar with implementing a linked list. In this lab you will complete the partially implemented LinkedIntegerList. Specifically, you will implement the following methods. • public boolean remove(int value) • public int removeAt(int index) • public boolean set(int index, int value) • public int indexOf(int value) Task-1 Add a new class named Lab3Tester class to test the current implementation of the linked list. (Note: you may reuse the code from SimpleIntegerListTester...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
You will be building a linked list. Make sure to keep track of both the head...
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. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt) SetNext() - Mutator...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
c++   In this lab you will create a program to make a list of conference sessions...
c++   In this lab you will create a program to make a list of conference sessions you want to attend. (List can be of anything...) You can hard code 10 Sessions in the beginning of your main program. For example, I have session UK1("Descaling agile",    "Gojko Adzic",       60);        session UK2("Theory of constraints", "Pawel Kaminski", 90); and then:        List l;        l.add(&UK1);        l.add(&UK2); Your Session struct should have the following member data: The Title The Speaker The session...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT