Question

In: Computer Science

8.14 LAB: Warm up: Contacts (C Programming Only) You will be building a linked list. Make...

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.

  • ContactNode.h - Struct definition, including the data members and related function declarations
  • ContactNode.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

Solutions

Expert Solution

//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;
}


Related Solutions

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()...
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...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don’t forget to add a destructor that destroys the list. Demonstrate the class with a driver program. 2. List Print Modify the linked list class you created in Programming Challenge 1 to add a print member function. The function should display all the values in the linked list. Test...
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...
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 - Struct definition and related function declarations PlaylistNode.c - Related function definitions main.c - 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. Private data members char uniqueID[50] char songName[50] char artistName[50] int songLength PlaylistNode* nextNodePtr Related...
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...
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