Answer to Question #218592 in C++ for devy

Question #218592

Provide a member function of class linkedListType that determines the maximum element in a list. Use the following header template Type linkedListType:: max(); You may assume that operator> is overloaded for Type.  


1
Expert's answer
2021-07-20T02:32:40-0400
using namespace std;
#include <omp.h>
#include <stdio.h>

struct Node 
{
   int data;
   struct Node *next;
};
struct Node* head = NULL;


void InsertData(int NewtData)
{
	struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
	new_node->data = NewtData;
	new_node->next = head;
	head = new_node;
}


void DisplayLinkedList(void) 
{
   	struct Node* ptr; ptr=head;
  	while (ptr != NULL) 
   	{
    	cout<< ptr->data <<" ";
    	ptr = ptr->next;
   	}
}


void max(void)
{
   	struct Node* ptr; ptr=head;
   	int Max;
	Max = ptr->data;
  	while (ptr != NULL) 
   	{
    	if(ptr->data >= Max) Max = ptr->data;
		
    	ptr = ptr->next;
   	}
   	cout<<"\nMaximum Element = "<<Max;
}






int main() 
{
	int List[5] = {1, 3, 5, 33, 22};
    int n;
    for(n=0;n<5;n++) InsertData(List[n]);
    
    cout<<"\nThe linked list- is: ";
    DisplayLinkedList();
    max();
    return 0;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS