This is C. Below is my code i am still running into some errors as well i will p
ID: 3804330 • Letter: T
Question
This is C. Below is my code i am still running into some errors as well i will post below
(1) Create three files to submit.
Contacts.h - Struct definition, including the data members and related function declarations
Contacts.c - Related function definitions
main.c - main() function
(2) Build the ContactNode struct per the following specifications:
Data members
char contactName[50]
char contactPhoneNum[50]
struct ContactNode* nextNodePtr
Related functions
CreateContactNode() (2 pt)
InsertContactAfter() (2 pts)
Insert a new node after node
GetNextContact() (1 pt)
Return location pointed by nextNodePtr
PrintContactNode()
Ex. of PrintContactNode() output:
(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:
(4) Output the linked list. (2 pts)
Ex:
Code plus errors
/*Contacts.h*/
#ifndef CONTACTS_H_INCLUDED
#define CONTACTS_H_INCLUDED
struct ContactNode
{
char ContactName[50];
char ContactPhone[50];
struct ContactNode* nextNodePtr;
};
struct ContactNode* CreateContactNode(struct ContactNode* p, char name[], char phone[]);/*create a new node*/
struct ContactNode* InsertContactAfter(struct ContactNode* p,char name[], char phone[]); /*insert a new node after node*/
struct ContactNode* GetNextContact(struct ContactNode* p); /*Return Location Pointed by nextNodePtr*/
void PrintContactNode(struct ContactNode* p); /*print a node*/
#endif // CONTACTS_H_INCLUDED
_______________________________________________________
/*Contacts.c*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"Contacts.h"
struct ContactNode* CreateContactNode(struct ContactNode* p, char name[], char phone[])/*create a new node*/
{
if((p=(struct ContactNode*)malloc(sizeof(struct ContactNode)))==NULL)
{
printf("Error - Could not allocate memory. Exiting... ");
exit(0);
}
strcpy(p->ContactName,name);
strcpy(p->ContactPhone,phone);
p->nextNodePtr=NULL;
return(p);
}
struct ContactNode* InsertContactAfter(struct ContactNode* p,char name[], char phone[]) /*insert a new node after node*/
{
struct ContactNode* temp;
if(p==NULL) /*if list is empty*/
{
p=CreateContactNode(temp,name,phone);
return(p); /*create and return the first node in the list*/
}
else
{
temp=CreateContactNode(temp,name,phone);
p->nextNodePtr=temp; /*create a new node and link it to the previous node in the list*/
}
return(temp);
}
struct ContactNode* GetNextContact(struct ContactNode* p) /*Return Location Pointed by nextNodePtr*/
{
return p->nextNodePtr;
}
void PrintContactNode(struct ContactNode* p) /*print a node*/
{
printf(" Name: %s ",p->ContactName);
printf("Phone Number: %s ",p->ContactPhone);
}
___________________________________________________________
/*main.c*/
#include <stdio.h>
#include <stdlib.h>
#include"Contacts.h"
int main()
{
struct ContactNode* start;
struct ContactNode* t;
int i;
char Name[50],phoneNo[50];
for(i=1;i<=3;i++)
{
printf(" Person%d ",i);
printf("Enter name: ");
gets(Name);
printf("Enter phone number: ");
gets(phoneNo);
if(i==1)
{
start=CreateContactNode(t,Name,phoneNo);
t=start;
}
else
t=InsertContactAfter(t,Name,phoneNo);
printf("You Entered: %s, %s ",t->ContactName,t->ContactPhone);
}
/*print the list*/
printf(" CONTACT LIST");
t=start; /*print out the list by traversing each node from start to end*/
while(t)
{
PrintContactNode(t);
t=GetNextContact(t);
}
}
Current errors;
1. Unit test Tests that CreateContactNode() initializes a node with name "Jane Doe" and phone number "555-555-5555. 2. Compare output Roxanne Hughes 443-555-2864 Juan Alberto Jr. Input 410-555-9385 Rachel Phillips 310-555-6610 Person 1 Enter name: Enter phone number You entered: Roxanne Hughes 443-555-2864 Person 2 Enter name: Your output correctly starts with Enter phone number You entered: Juan Alberto Jr 410-555-9385 Person 3 Enter name: Enter phone number You entered: Rachel Phillips 310-555-6610Explanation / Answer
#ifndef CONTACTS_H_INCLUDED
#define CONTACTS_H_INCLUDED
struct ContactNode
{
char ContactName[50];
char ContactPhone[50];
struct ContactNode* nextNodePtr;
};
struct ContactNode* CreateContactNode(struct ContactNode* p, char name[], char phone[]);/*create a new node*/
struct ContactNode* InsertContactAfter(struct ContactNode* p,char name[], char phone[]); /*insert a new node after node*/
struct ContactNode* GetNextContact(struct ContactNode* p); /*Return Location Pointed by nextNodePtr*/
void PrintContactNode(struct ContactNode* p); /*print a node*/
#endif // CONTACTS_H_INCLUDED
Contacts.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"Contacts.h"
struct ContactNode* CreateContactNode(struct ContactNode* p, char name[], char phone[])/*create a new node*/
{
if((p=(struct ContactNode*)malloc(sizeof(struct ContactNode)))==NULL)
{
printf("Error - Could not allocate memory. Exiting... ");
exit(0);
}
strcpy(p->ContactName,name);
strcpy(p->ContactPhone,phone);
p->nextNodePtr=NULL;
return(p);
}
struct ContactNode* InsertContactAfter(struct ContactNode* p,char name[], char phone[]) /*insert a new node after node*/
{
struct ContactNode* temp;
if(p==NULL) /*if list is empty*/
{
p=CreateContactNode(temp,name,phone);
return(p); /*create and return the first node in the list*/
}
else
{
temp=CreateContactNode(temp,name,phone);
p->nextNodePtr=temp; /*create a new node and link it to the previous node in the list*/
}
return(temp);
}
struct ContactNode* GetNextContact(struct ContactNode* p) /*Return Location Pointed by nextNodePtr*/
{
return p->nextNodePtr;
}
void PrintContactNode(struct ContactNode* p) /*print a node*/
{
printf(" Name: %s ",p->ContactName);
printf("Phone Number: %s ",p->ContactPhone);
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include"Contacts.h"
int main()
{
struct ContactNode* start;
struct ContactNode* t;
int i;
char Name[50],phoneNo[50];
for(i=1;i<=3;i++)
{
printf(" Person%d ",i);
printf("Enter name: ");
gets(Name);
printf("Enter phone number: ");
gets(phoneNo);
if(i==1)
{
start=CreateContactNode(t,Name,phoneNo);
t=start;
}
else
t=InsertContactAfter(t,Name,phoneNo);
printf("You Entered: %s, %s ",t->ContactName,t->ContactPhone);
}
/*print the list*/
printf(" CONTACT LIST");
t=start; /*print out the list by traversing each node from start to end*/
while(t)
{
PrintContactNode(t);
t=GetNextContact(t);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.