In: Computer Science
8.14 LAB: Warm up: Contacts (C Programming Only)
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 struct per the following specifications:
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
//ContactNode.h ------------------------
#ifndef CONTACTNODE_H
#define CONTACTNODE_H
typedef struct ContactNode
{
char contactName[50];
char contactPhoneNum[50];
struct ContactNode *nextNodePtr;
}ContactNode;
ContactNode* CreateContactNode(char [], char []);
void InsertContactAfter(ContactNode **,ContactNode **,ContactNode
*);
ContactNode* GetNextContact(ContactNode *);
void PrintContactNode(ContactNode *);
#endif
//-------------- ContactNode.c --------------
#include "ContactNode.h"
#include<malloc.h>
#include<stdio.h>
#include<string.h>
ContactNode* allocateMemory()
{
return (ContactNode *)
malloc(sizeof(ContactNode));
}
ContactNode* CreateContactNode(char contactName[], char
contactPhoneNum[])
{
ContactNode *newNode = allocateMemory();
if(newNode == NULL)
{
printf("Stack Overflow
error");
return NULL;
}
strcpy(newNode->contactName,contactName);
strcpy(newNode->contactPhoneNum,contactPhoneNum);
newNode->nextNodePtr = NULL;
return newNode;
}
void InsertContactAfter(ContactNode **head,ContactNode
**tail,ContactNode *newNode)
{
if(newNode == NULL)
{
return;
}
if(*head == NULL)
{
*head = newNode;
*tail = newNode;
return;
}
(*tail)->nextNodePtr = newNode;
*tail = GetNextContact(*tail);
}
ContactNode * GetNextContact(ContactNode *node)
{
if(node == NULL)
{
return NULL;
}
return node->nextNodePtr;
}
void PrintContactNode(ContactNode *head)
{
if(head == NULL)
{
printf("\nContacts are
Empty.\n");
return;
}
ContactNode*current = head;
printf("\nCONTACT LIST\n");
while(current)
{
printf("Name:
%s\n",current->contactName);
printf("Phone Number: %s\n\n",
current->contactPhoneNum);
current =
GetNextContact(current);
}
}
//----------- main.c----------------
#include "ContactNode.c"
#include<stdlib.h>
#include<stdio.h>
int main()
{
char contactName[50];
char contactPhoneNum[50];
ContactNode *head = NULL;
ContactNode *tail = NULL;
ContactNode *one;
ContactNode *two;
ContactNode *three;
printf("\nPerson 1\n");
printf("Enter name:\n");
gets(contactName);
printf("Enter phone number:\n");
gets(contactPhoneNum);
printf("You entered: %s,
%s\n",contactName,contactPhoneNum);
one =
CreateContactNode(contactName,contactPhoneNum);
printf("\nPerson 2\n");
printf("Enter name:\n");
gets(contactName);
printf("Enter phone number:\n");
gets(contactPhoneNum);
printf("You entered: %s,
%s\n",contactName,contactPhoneNum);
two =
CreateContactNode(contactName,contactPhoneNum);
printf("\nPerson 3\n");
printf("Enter name:\n");
gets(contactName);
printf("Enter phone number:\n");
gets(contactPhoneNum);
printf("You entered: %s,
%s\n",contactName,contactPhoneNum);
three =
CreateContactNode(contactName,contactPhoneNum);
InsertContactAfter(&head,&tail,one);
InsertContactAfter(&head,&tail,two);
InsertContactAfter(&head,&tail,three);
PrintContactNode(head);
return 0;
}
