For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program.
#include <stdio.h>
#include <stdlib.h>
struct Node {
void *data;
struct Node *next;
};
struct List {
struct Node *head;
};
static inline void initialize(struct List *list)
{
list->head = 0;
}
struct Node *addingFront(struct List *list, void *data);
struct Node *addingFront(struct List *list, void *data){
struct Node *node = malloc(sizeof(struct Node));
if(!node){
return NULL;
}
node->data = data;
node->next = list->head;
list->head=node;
return node;
}
void traversingList(struct List *list, void (*f)(void *));
void traversingList(struct List *list, void (*f)(void *))
{
struct Node *nod = list->head;
while(nod!=NULL)
{
(f)(nod->data);
nod=nod->next;
}
}
static void printString(void **p)
{
char **array;
array = (char **)p;
// array = p;
printf("%s \n",*array);
}
int main()
{
char *a[] = {"Hello","world","america"};
int n = 3;
struct Node *node;
struct List list;
initialize(&list);
print("The words are: ");
for (int i = 0; i < n; i++) {
addingFront(&list, a+i);
}
traversingList(&list, &printString);
printf("\n");
return 0;
}
Here is the solution to above problem in C. Please read the code comments for more information
GIVE A THUMBS UP!!!
Explanation
1. CheckAmerica function compares a word and check if it is equivalent to america
2. traverseForAmerica uses checkAmerica to find the if america is in the list
C CODE
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
struct Node {
void *data;
struct Node *next;
};
struct List {
struct Node *head;
};
static inline void initialize(struct List *list)
{
list->head = 0;
}
struct Node *addingFront(struct List *list, void *data);
struct Node *addingFront(struct List *list, void *data){
struct Node *node = malloc(sizeof(struct Node));
if(!node){
return NULL;
}
node->data = data;
node->next = list->head;
list->head=node;
return node;
}
int checkAmerica(void **p)
{
char **array;
array=(char **)p;
//here check if the word is equivalent to
america
if(strcmp(*array,"america")==0)
{
return 1; //equal to america
}
return 0; //if not equal to america
}
static void printString(void **p)
{
char **array;
array = (char **)p;
printf("%s \n",*array);
}
void traversingList(struct List *list,void (*f)(void **p));
void traversingList(struct List *list,void (*f)(void **p))
{
struct Node *nod = list->head;
while(nod!=NULL)
{
(*f)(nod->data);
nod=nod->next;
}
}
void traverseForAmerica(struct List *list,int (*f)(void
**p));
void traverseForAmerica(struct List *list,int (*f)(void **p))
{
struct Node *nod = list->head;
while(nod!=NULL)
{
int returnValue = (*f)(nod->data);
if(returnValue)
{
printf("america is on the list\n");
return;
}
nod=nod->next;
}
printf("america is not on the list\n");
}
int main()
{
char *a[] = {"Hello","world","america"};
int n = 3;
struct Node *node;
struct List list;
initialize(&list);
printf("The words are: ");
int i;
for (i = 0; i < n; i++) {
addingFront(&list, a+i);
}
//traverse and print the list
traversingList(&list,printString);
printf("\n");
//now traverse and check if it contains america
traverseForAmerica(&list,checkAmerica);
printf("\n");
return 0;
}
SCREENSHOT OF OUTPUT
Get Answers For Free
Most questions answered within 1 hours.