Question

For the following code in C, I want a function that can find "america" from the...

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

Homework Answers

Answer #1

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

Know the answer?
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
Similar Questions
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include <stdio.h> #define MAX_STR_LEN 100 struct Node { char *data; struct Node *left; struct Node *right; }; struct Node *new_node(char *d) { struct Node *r = malloc(sizeof(struct Node)); r->data = malloc(sizeof(char) * MAX_STR_LEN); strncpy(r->data, d, MAX_STR_LEN); free(d); r->left = NULL; r->right = NULL; return r; } void freeSubTree(struct Node *r) { free(r->data); if (r->left != NULL) freeSubTree(r->left); if (r->right != NULL) freeSubTree(r->right); free(r); } void...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...