Question
TEMPLATE FOR THE SECOND CODING PROBLEM two.c #include #include #include #include scanner.h typedef struct node { char *name; struct node *next; } Node; void print(Node
TEMPLATE FOR THE SECOND CODING PROBLEM
two.c
#include
#include
#include
#include "scanner.h"
typedef struct node {
char *name;
struct node *next;
} Node;
void print(Node *list) {
if (list == NULL) return;
printf(" %s ", list
-
>name);
print(list
-
>next);
}
Node *addEnd(char *n, Node *list) {
Node *newOne = malloc(sizeof(Node));
newOne
-
>name =
malloc(strlen(n)+1);
strcpy(newOne
-
>name, n);
newOne
-
>next = NULL;
if (list == NULL)
return newOne;
Node *temp = list;
while (temp
-
>next != NULL)
temp = temp
-
>next;
temp
-
>next
= newOne;
return list;
}
void printOdds(Node *list) {
// code for printOdds
goes here
}
void printEvens(Node *list) {
// code for printEvens
goes here
}
int main(int argc, char *argv[]) {
Node *list = NULL;
char *name;
printf("Enter a name to add or 'xxx' to quit : ");
name = readToken(stdin);
while (strcmp(name, "xxx") != 0) {
list = addEnd(name, list);
printf("Enter a name to add or 'xxx' to qu
it : ");
name = readToken(stdin);
}
printf("The list is: "); print(list); printf("
\
n
\
n");
printf("Printing the odd (first/thi
rd/fifth/etc) values
\
n");
printOdds(list);
printf("
\
n");
printf("
Printing the even (second/four
th/sixth/etc) values
\
n");
printEvens(list);
printf("
\
n");
return 0;
Write a program named two.c that uses linked lists. The main part of this program is shown on the next page (just copy this code to your laptop). It has four functions: addEnd (which adds a new element at the end of the list), print (which prints the current list), printOdds and printEvens. You must write these last two functions. The first, printOdds, prints the odd elements in the list (the first, third, fifth, elements). The second, printEvens, prints the even elements in the list (the second, fourth, sixth, elements). A sample execution of this program is shown below ./a.out Enter a name to addor 'xxx to quit: fred Enter a name to addor 'xxx to quit: wilma Enter a name to add or 'xxx' to quit : betty Enter a name to add or 'xxx' to quit : barney Enter a name to addor 'xxx to quit: dino Enter a name to addor 'xxx to quit: xx The list is: fred wilma betty barney dino Printing the odd (first/third/fifth/etc) values in the list fred betty dino Printing the even (second/fourth/sixth/etc) values in the list wilma barneyStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started