This is in C not C++ phone.c #include <stdio.h> #include <string.h> #include <st
ID: 3710727 • Letter: T
Question
This is in C not C++
phone.c
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "phone.h"
///////////////////////////////////////////////////////////////
void printNumber(long val) {
return;
}
///////////////////////////////////////////////////////////////
MultiLinkedList *add(MultiLinkedList *list, char *first, char *last, long num) {
// allocate a new node
Node *newNode = malloc ( sizeof(Node) );
newNode->first = malloc ( strlen(first) + 1 );
strcpy(newNode->first, first);
newNode->last = malloc ( strlen(last) + 1 );
strcpy(newNode->last, last);
newNode->number = num;
// add this new node at the head of the "byFirst" list
newNode->nextFirst = list->headFirstName;
list->headFirstName = newNode;
// add this new node at the head of the "byLast" list
newNode->nextLast = list->headLastName;
list->headLastName = newNode;
// return the multi-list object with updated head pointers
return list;
}
int size(MultiLinkedList *list) {
// return the number of names in the list
return -1;
}
void printByFirst(MultiLinkedList *list) {
// print (traverse the byFirst list)
return;
}
void printByLast(MultiLinkedList *list) {
// print (traverse the byLast list)
return;
}
int lookup(MultiLinkedList *list, char *name) {
// print all instances of name (as either a first name or a last name)
return -1;
}
void updatePhone(MultiLinkedList *list, char *first, char *last, long number) {
// update the phone number for the individual specified
return;
}
MultiLinkedList *removeItem(MultiLinkedList *list, char *first, char *last) {
// remove the individual specified from the list
return NULL;
}
MultiLinkedList *updateFirst(MultiLinkedList *list, char *first, char *last, char *newFirst) {
// update the first name of the individual specified
return NULL;
}
MultiLinkedList *updateLast(MultiLinkedList *list, char *first, char *last, char *newLast) {
// update the last name of the individual specified
return NULL;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "phone.h"
///////////////////////////////////////////////////////////////
void printNumber(long val) {
int a[10];
for(int i = 9;i >= 0;i--){
a[i] = val%10;
val = val/10;
}
for(int i = 0; i < 10;i++){
if(i < 3){
printf("%d",a[i]);
}
printf("-");
if(i >= 3 && i < 6){
printf("%d",a[i]);
}
printf("-")
if(i >= 6){
printf("%d",a[i]);
}
}
}
///////////////////////////////////////////////////////////////
MultiLinkedList *add(MultiLinkedList *list, char *first, char *last, long num) {
// allocate a new node
Node *newNode = malloc ( sizeof(Node) );
newNode->first = malloc ( strlen(first) + 1 );
strcpy(newNode->first, first);
newNode->last = malloc ( strlen(last) + 1 );
strcpy(newNode->last, last);
newNode->number = num;
// add this new node at the head of the "byFirst" list
newNode->nextFirst = list->headFirstName;
list->headFirstName = newNode;
// add this new node at the head of the "byLast" list
newNode->nextLast = list->headLastName;
list->headLastName = newNode;
// return the multi-list object with updated head pointers
return list;
}
int size(MultiLinkedList *list) {
// return the number of names in the list
int size = 0;
Node *temp = malloc(sizeof(Node));
temp->nextFirst = list->headFirstName;
while(temp->nextFirst != NULL){
temp = temp->nextFirst;
size++;
}
return size-1;//-1 because we considered head
}
void printByFirst(MultiLinkedList *list) {
// print (traverse the byFirst list)
Node *temp = malloc(sizeof(Node));
temp->nextFirst = list->headFirstName;
while(temp->nextFirst != NULL){
temp = temp->nextFirst;
printf("%s ",temp->first);
printf("%s ",temp->last);
printf("%ld ",temp->number);
}
return;
}
void printByLast(MultiLinkedList *list) {
// print (traverse the byLast list)
Node *temp = malloc(sizeof(Node));
temp->nextLast = list->headLastName;
while(temp->nextLast != NULL){
temp = temp->nextLast;
printf("%s ",temp->first);
printf("%s ",temp->last);
printf("%ld ",temp->number);
}
return;
}
int lookup(MultiLinkedList *list, char *name) {
// print all instances of name (as either a first name or a last name)
int count = 0;
Node *temp = malloc(sizeof(Node));
temp->nextFirst = list->headFirstName;
temp->nextLast = list->headLastName;
while(temp->nextLast != NULL || temp->nextFirst != NULL){
if(temp->nextFirst != NULL)
temp = temp->nextFirst;
else if(temp->nextLast!=NULL)
temp = temp->nextLast;
if(strcmp(temp->first, name) == 0){
printf("%s ", temp->first);
count++;
}
else if(strcmp(temp->last, name) == 0){
printf("%s ", temp->last);
count++;
}
}
return count;
}
void updatePhone(MultiLinkedList *list, char *first, char *last, long number) {
// update the phone number for the individual specified
Node *temp = malloc(sizeof(Node));
temp->nextFirst = list->headFirstName;
temp->nextLast = list->headLastName;
while(temp->nextLast != NULL || temp->nextFirst != NULL){
if(temp->nextFirst != NULL)
temp = temp->nextFirst;
else if(temp->nextLast!=NULL)
temp = temp->nextLast;
if(strcmp(temp->first, first) == 0 && strcmp(temp->last, last) == 0)
temp->number = number;
}
return;
}
MultiLinkedList *removeItem(MultiLinkedList *list, char *first, char *last) {
// remove the individual specified from the list
Node *temp = malloc(sizeof(Node));
temp->nextFirst = list->headFirstName;
temp->nextLast = list->headLastName;
while(temp->nextLast != NULL || temp->nextFirst != NULL){
if(temp->nextFirst != NULL)
temp = temp->nextFirst;
else if(temp->nextLast!=NULL)
temp = temp->nextLast;
if(strcmp(temp->first, first) == 0 && strcmp(temp->last, last) == 0){
temp->nextLast = temp->nextLast->nextLast;
temp->nextFirst = temp->nextFirst->nextFirst;
}
}
return list;
}
MultiLinkedList *updateFirst(MultiLinkedList *list, char *first, char *last, char *newFirst) {
// update the first name of the individual specified
Node *temp = malloc(sizeof(Node));
temp->nextFirst = list->headFirstName;
temp->nextLast = list->headLastName;
while(temp->nextLast != NULL || temp->nextFirst != NULL){
if(temp->nextFirst != NULL)
temp = temp->nextFirst;
else if(temp->nextLast!=NULL)
temp = temp->nextLast;
if(strcmp(temp->first, first) == 0 && strcmp(temp->last, last) == 0){
strcpy(temp->first, newFirst);
}
}
return list;
}
MultiLinkedList *updateLast(MultiLinkedList *list, char *first, char *last, char *newLast) {
// update the last name of the individual specified
Node *temp = malloc(sizeof(Node));
temp->nextFirst = list->headFirstName;
temp->nextLast = list->headLastName;
while(temp->nextLast != NULL || temp->nextFirst != NULL){
if(temp->nextFirst != NULL)
temp = temp->nextFirst;
else if(temp->nextLast != NULL)
temp = temp->nextLast;
if(strcmp(temp->first, first) == 0 && strcmp(temp->last, last) == 0){
strcpy(temp->last, newLast);
}
}
return list;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.