Use this template to write the program in C language. #include <stdio.h> #includ
ID: 3683488 • Letter: U
Question
Use this template to write the program in C language.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node* insertFront(node* head, int d);
node* insertBack(node* head, int d);
void print(node* head);
int max(node* head);
int min(node* head);
int locInList(node* head, int x);
int main()
{
node* head = NULL;
head = insertFront(head, 5);
head = insertFront(head, 4);
head = insertBack(head, 6);
head = insertBack(head, 7);
print(head);
printf("Max: %d ", max(head));
printf("Min: %d ", min(head));
printf("locInList 5: %d ", locInList(head, 5));
printf("locInList 9: %d ", locInList(head, 9));
return 0;
}
node* insertFront(node* head, int d)
{
}
node* insertBack(node* head, int d)
{
}
void print(node* head)
{
}
int max (node* head)
{
}
int min (node* head)
{
}
int locInList(node* head, int x)
{
}
There are no specific requirements for the main function, other than it must successfully demonstrate all of the other required functions. You may leave my sample code (output shown below or modify it as you wish. You will need to successfully implement the following functions: insertFront .Inserts a new node to the front of the list .Params: head pointer, integer data insertBack Inserts a new node to the back of the list .Params: head pointer, integer data Print Prints the current linked list .Params: head pointer .This is the ONLY function that will print anything!! Max .Returns the maximum value in the list. (Does not print) .Params: head pointer Min .Returns the minimum value in the list (Does not print) .Params: head pointer loclnList .Returns the location of a number in the list. (Does not print) o Locations should start at 0, like an array. o If the number is not in the list, return -1 .Params: head pointer, integer to search for Sample Output: 345 6 Max: 6 Min: 0 loclnList 5: 1 loclnList 9:-1Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node* insertFront(node* head, int d);
node* insertBack(node* head, int d);
void print(node* head);
int max(node* head);
int min(node* head);
int locInList(node* head, int x);
int main()
{
node* head = NULL;
head = insertFront(head, 5);
head = insertFront(head, 4);
head = insertBack(head, 6);
head = insertBack(head, 7);
print(head);
printf("Max: %d ", max(head));
printf("Min: %d ", min(head));
printf("locInList 5: %d ", locInList(head, 5));
printf("locInList 9: %d ", locInList(head, 9));
return 0;
}
node* insertFront(node* head, int d)
{
node* new_node =(node*) malloc(sizeof(node));
new_node->data=d;
new_node->next=head;
return new_node;
}
node* insertBack(node* head, int d)
{
node* new_node =(node*) malloc(sizeof(node));
new_node->data=d;
new_node->next=NULL;
if(head==NULL)
return new_node;
node* last_node=head;
while(last_node->next != NULL)
last_node=last_node->next;
last_node->next=new_node;
return head;
}
void print(node* head)
{
printf("Elements of the linked list are: ");
node* curr=head;
while(curr != NULL){
printf("%d ",curr->data);
curr=curr->next;
}
printf(" ");
}
int max (node* head)
{
int max,i=0;
node* curr=head;
while(curr != NULL){
if(i==0){
max=curr->data;
i=1;
}
else{
if(curr->data > max)
max=curr->data;
}
curr=curr->next;
}
return max;
}
int min (node* head)
{
int min,i=0;
node* curr=head;
while(curr != NULL){
if(i==0){
min=curr->data;
i=1;
}
else{
if(curr->data < min)
min=curr->data;
}
curr=curr->next;
}
return min;
}
int locInList(node* head, int x)
{
int ind=0;
node* curr=head;
while(curr != NULL){
if(curr->data == x)
return ind;
ind++;
curr=curr->next;
}
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.