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
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:
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
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
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
/*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); } }
Errors
Contacts.c: In function 'InsertContactAfter': Contacts.c:21:10: warning: 'temp' may be used uninitialized in this function [-Wmaybe-uninitialized] p=CreateContactNode(temp,name,phone); ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c: In function 'main': main.c:13:9: error: too few arguments to function 'fgets' fgets(Name); ^~~~~ In file included from main.c:1:0: /usr/include/stdio.h:622:14: note: declared here extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) ^~~~~
main.c:15:9: warning: implicit declaration of function 'gets' [-Wimplicit-function-declaration] gets(phoneNo); ^~~~
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
