(a) Given the following declarations, write a single function to print out every
ID: 3718106 • Letter: #
Question
(a) Given the following declarations, write a single function to print out every third value in the list, starting with the first element. That is, the function should print the 1st, 4th, 7th, etc. elements until the end of the list. Your function should take one LinkedList pointer parameter, and return nothing. Assume the list has already been populated (i.e. filled-in/initialised), and all relevant structs are declared as follows:
typedef struct LinkedListNode { int value; struct LinkedListNode* next; } LinkedListNode; typedef struct { LinkedListNode* head; } LinkedList;
Note: the list length is not necessarily divisible by 3.
(b) Given the following declarations, write a single function to print out each name in the list, but only where: • The name starts with a capital letter; and • The age is at least 18. Your function should take a single pointer to List and return nothing. Assume the list has already been populated (i.e. filled-in/initialised), and all relevant structs are declared as follows:
typedef struct { char name[20]; int age; } Person; typedef struct Node { struct Node* next; Person* p; } Node; typedef struct { Node *head; } List;
Explanation / Answer
a.) ans
/*Function to print 1st,4th,7th... element of a list*/
void printElement(LinkedList *start){
LinkedListNode *ptr;
int count=0;
ptr = start;
while(ptr!=NULL){
if(count%3 == 0){
printf("%dth element is: %d ",count+1,ptr->value);
}
ptr=ptr->next;
count++;
}
}
b.)ans
/*Function to print the name which start with Upper case letter and age is greater and equal to 18*/
void printName(List *start){
List *ptr;
ptr = start;
while(ptr!=NULL){
if((ptr->head->p->name[0] >100) && (ptr->head->p->name[0]<133) && (ptr->head->p->age >17)){
printf("Name is: %s ",ptr->head->p->name);
}
ptr=ptr->head->next;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.