Question

IN C PROGRAMMING. PLEASE INCLUDE COMMENTS

In this lab programming assignment you will extend the Contact Linked List we created during lecture in the following ways: 1

Once all three functions are working, the output from main() should be as follows: Printing Team List: lead Coach has scored

#include
#include
#include

typedef struct Contact_struct {
char myName[20]; //name of this Contact
int numGoals;
struct Contact_struct* next; //pointer to the next Contact
} Contact;

//Contact Constructor
void Contact_Create(Contact* thisContact, char thisName[], int thisGoals, Contact* nextContact) {
strcpy(thisContact->myName,thisName);
thisContact->numGoals = thisGoals;
thisContact->next = nextContact;
}

void Contact_PrintList(Contact* headContact){
Contact* currContact = headContact;
while (currContact != NULL){
printf("%s has scored %d goals\n",currContact->myName, currContact->numGoals);
currContact = currContact->next;
}
}

int Contact_SumGoals(Contact* headContact){

//Develop Functionality for Contact_SumGoals here

return 0;
}


void Contact_Add(Contact* headContact, char addName[], int addGoals){
Contact* newContact = (Contact*)malloc(sizeof(Contact));
Contact_Create(newContact,addName,addGoals,NULL);
  
Contact* currContact = headContact;
while (currContact->next != NULL){
currContact = currContact->next;
}
currContact->next = newContact;
}

void Contact_Drop(Contact* headContact){
Contact* prevContact = headContact;
Contact* currContact = headContact->next;
while (currContact->next != NULL){
prevContact = currContact;
currContact = currContact->next;
}
prevContact->next = NULL;
}

void Contact_Remove(Contact* headContact, char addName[]){
Contact* prevContact = headContact;
Contact* currContact = headContact->next;
while (currContact != NULL) {
if (strcmp(currContact->myName,addName)==0) {
prevContact->next = currContact->next;
currContact = NULL;
} else {
prevContact = currContact;
currContact = currContact->next;
}
}
}

void Contact_RemoveByIndex(Contact* headContact, int removeInd){

//Develop Functionality for Contact_RemoveByIndex here
  
}

void Contact_Insert(Contact* headContact, char findName[], char addName[], int addGoals){
Contact* newContact = (Contact*)malloc(sizeof(Contact));
Contact_Create(newContact,addName,addGoals,NULL);
  
Contact* currContact = headContact;
while (currContact != NULL) {
if (strcmp(currContact->myName,findName)==0) {
newContact->next = currContact->next;
currContact->next = newContact;
currContact = NULL;
} else {
currContact = currContact->next;
}
}
}

void Contact_InsertByIndex(Contact* headContact, int addInd, char addName[], int addGoals){
  
//Develop Functionality for Contact_InsertByIndex here
  
}

int main() {
Contact* headCoach = NULL;
headCoach = (Contact*)malloc(sizeof(Contact));
Contact_Create(headCoach,"Head Coach",0,NULL);
  
Contact_Add(headCoach,"Remy",15);
Contact_Add(headCoach,"Riley",2);
Contact_Add(headCoach,"Armani",6);
Contact_Add(headCoach,"Jessie",22);
Contact_Add(headCoach,"Santana",5);
  
//Traverse Linked List and Print Names
printf("Printing Team List:\n");
Contact_PrintList(headCoach);
printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));
  
//Removing from back
printf("Removing Santana from the back:\n");
Contact_Drop(headCoach);
Contact_PrintList(headCoach);
printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Adding to back
printf("Adding Devyn to the back:\n");
Contact_Add(headCoach,"Devyn",12);
Contact_PrintList(headCoach);
printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Removing Armani
printf("Finding Armani and Removing from the List:\n");
Contact_Remove(headCoach,"Armani");
Contact_PrintList(headCoach);
printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Inserting Casey in after Remy
printf("Finding Remy and Inserting Casey after:\n");
Contact_Insert(headCoach,"Remy","Casey",3);
Contact_Insert(headCoach,"Armani","Sam",8);
Contact_PrintList(headCoach);
printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Removing 4th Contact (Jessie)
printf("Removing the contact at index 4 (Jessie):\n");
Contact_RemoveByIndex(headCoach, 4);
Contact_PrintList(headCoach);
printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Adding Sam in 3rd spot
printf("Adding Sam at index 3:\n");
Contact_InsertByIndex(headCoach, 3, "Sam",14);
Contact_PrintList(headCoach);
printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

printf("\n");
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct Contact_struct {

char myName[20]; //name of this Contact

int numGoals;

struct Contact_struct* next; //pointer to the next Contact

} Contact;

//Contact Constructor

void Contact_Create(Contact* thisContact, char thisName[], int thisGoals, Contact* nextContact) {

strcpy(thisContact->myName,thisName);

thisContact->numGoals = thisGoals;

thisContact->next = nextContact;

}

void Contact_PrintList(Contact* headContact){

Contact* currContact = headContact;

while (currContact != NULL){

printf("%s has scored %d goals\n",currContact->myName, currContact->numGoals);

currContact = currContact->next;

}

}

int Contact_SumGoals(Contact* headContact){

int sum =0;

Contact* curr = headContact;

while(curr!=NULL){

sum = sum + curr->numGoals;

curr = curr->next;

}

return sum;

}


void Contact_Add(Contact* headContact, char addName[], int addGoals){

Contact* newContact = (Contact*)malloc(sizeof(Contact));

Contact_Create(newContact,addName,addGoals,NULL);

Contact* currContact = headContact;

while (currContact->next != NULL){

currContact = currContact->next;

}

currContact->next = newContact;

}

void Contact_Drop(Contact* headContact){

Contact* prevContact = headContact;

Contact* currContact = headContact->next;

while (currContact->next != NULL){

prevContact = currContact;

currContact = currContact->next;

}

prevContact->next = NULL;

}

void Contact_Remove(Contact* headContact, char addName[]){

Contact* prevContact = headContact;

Contact* currContact = headContact->next;

while (currContact != NULL) {

if (strcmp(currContact->myName,addName)==0) {

prevContact->next = currContact->next;

currContact = NULL;

} else {

prevContact = currContact;

currContact = currContact->next;

}

}

}

void Contact_RemoveByIndex(Contact* headContact, int removeInd){


Contact* curr = headContact;

Contact* prev = NULL;

int i=0;

while(i < removeInd && curr!=NULL){

i++;

prev = curr;

curr = curr->next;

}


if(prev==NULL){

headContact = curr->next;

}else{

prev->next = curr->next;

}

}

void Contact_Insert(Contact* headContact, char findName[], char addName[], int addGoals){

Contact* newContact = (Contact*)malloc(sizeof(Contact));

Contact_Create(newContact,addName,addGoals,NULL);

Contact* currContact = headContact;

while (currContact != NULL) {

if (strcmp(currContact->myName,findName)==0) {

newContact->next = currContact->next;

currContact->next = newContact;

currContact = NULL;

} else {

currContact = currContact->next;

}

}

}

void Contact_InsertByIndex(Contact* headContact, int addInd, char addName[], int addGoals){

//Develop Functionality for Contact_InsertByIndex here

Contact* curr = headContact;

Contact* prev = NULL;

int i=0;

while(i < addInd && curr!=NULL){

i++;

prev = curr;

curr = curr->next;

}

Contact* newContact = (Contact*)malloc(sizeof(Contact));

Contact_Create(newContact,addName,addGoals,NULL);

if(prev==NULL){

newContact->next = curr;

headContact = newContact;

}else{

prev->next = newContact;

newContact->next = curr;

}



}

int main() {

Contact* headCoach = NULL;

headCoach = (Contact*)malloc(sizeof(Contact));

Contact_Create(headCoach,"Head Coach",0,NULL);

Contact_Add(headCoach,"Remy",15);

Contact_Add(headCoach,"Riley",2);

Contact_Add(headCoach,"Armani",6);

Contact_Add(headCoach,"Jessie",22);

Contact_Add(headCoach,"Santana",5);

//Traverse Linked List and Print Names

printf("Printing Team List:\n");

Contact_PrintList(headCoach);

printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Removing from back

printf("Removing Santana from the back:\n");

Contact_Drop(headCoach);

Contact_PrintList(headCoach);

printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Adding to back

printf("Adding Devyn to the back:\n");

Contact_Add(headCoach,"Devyn",12);

Contact_PrintList(headCoach);

printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Removing Armani

printf("Finding Armani and Removing from the List:\n");

Contact_Remove(headCoach,"Armani");

Contact_PrintList(headCoach);

printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Inserting Casey in after Remy

printf("Finding Remy and Inserting Casey after:\n");

Contact_Insert(headCoach,"Remy","Casey",3);

Contact_Insert(headCoach,"Armani","Sam",8);

Contact_PrintList(headCoach);

printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Removing 4th Contact (Jessie)

printf("Removing the contact at index 4 (Jessie):\n");

Contact_RemoveByIndex(headCoach, 4);

Contact_PrintList(headCoach);

printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

//Adding Sam in 3rd spot

printf("Adding Sam at index 3:\n");

Contact_InsertByIndex(headCoach, 3, "Sam",14);

Contact_PrintList(headCoach);

printf("Total Goals scored: %d\n\n",Contact_SumGoals(headCoach));

printf("\n");

return 0;

}

-------------------------------------------------------------------------------------------------------------------------------------

SEE OUTPUT

saved main.c https://Alienated TepidTrial. rahulkumar29.repl.run Files clang version 7.0.0-3-ubuntuo.18.04.1 (tags/RELEASE_70

PLEASE COMMENT if there is any concern.

======================================================

Add a comment
Know the answer?
Add Answer to:
IN C PROGRAMMING. PLEASE INCLUDE COMMENTS #include #include #include typedef struct Contact_struct { char myName[20]; //name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; };...

    Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; }; void func(struct student stud); int main() { struct student astud; astud.id=9401; strcpy(astud.name, "Joe"); astud.grade = 'A'; func(astud); return 0; } Abdelghani Bellaachia, CSCI 1121 Page: 16 void func(struct student astud) { printf(" Id is: %d \n", astud.id); printf(" Name is: %s \n", astud.name); printf(" Grade is: %c \n", astud.grade); } Modify this program to include the address of a student as a separate structure....

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node;...

    IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...

  • Implement a program that: reads a number of personal records (for example, using PERSON struct from...

    Implement a program that: reads a number of personal records (for example, using PERSON struct from the earlier lab) from the standard input, creates a database of personal records, allows for adding new entries to the database, allows for deleting entries from the database, includes functions to acquire a record of personal data, and includes functions to display (print) a single selected record from the database, and also allows for printing all records in the database. NOTES: if name is...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT