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.”....
"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...
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...
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...
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()...
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:...
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...
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;     }​​​​​...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT