Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
void max ()
{
struct node *max = (struct node*)malloc(sizeof(struct node));
struct node *min = (struct node*)malloc(sizeof(struct node));
struct node *temp = head->next;

if (head ==  NULL){ printf("list is empty\n");}
max-> data = temp-> data;
min-> data = temp-> data;
while(temp->next!=NULL)
{
if(temp->data > max->data){max->data = temp->data;}
else if (temp->data < min->data){min->data = temp->data;}
temp=temp->next;
}
printf(" min = %d // max = %d",min->data, max->data);

}
C++



What I have tried:

this is a function to print the min and max of a linked list

if the linkedlist i/p______ : 1 2 3 4 5 6
_______________o/p is min = 2 / max = 5

is does not take first or last nodes into account, how to fix that?
Posted
Updated 25-Mar-17 13:48pm

Don't assign new Nodes to Max or Min, and don't start with the second element!
Instead, set the initial Max and Min pointers to the list head:
struct node *max = head;
struct node *min = head;
struct node *temp = head;

Then use them in your loop:
C++
while (temp != NULL)
   {
   ...
   temp = temp->next;
   }

Inside the loop, compare the temp value with min and max, and if you find a new value, set the appropriate pointer to temp.

That way, you aren't wasting memory - which you don't free, causing memory leaks - and you cover all elements in the list.
If the list contains one element, the max and min are both the same: the one and only Node.

Try it: it's pretty obvious when you think about it!
 
Share this answer
 
Advice: intend your code properly, it help reading.
C++
void max ()
{
	struct node *max = (struct node*)malloc(sizeof(struct node));
	struct node *min = (struct node*)malloc(sizeof(struct node));
	struct node *temp = head->next;

	if (head ==  NULL){ printf("list is empty\n");}
	max-> data = temp-> data;
	min-> data = temp-> data;
	while(temp->next!=NULL)
	{
		if(temp->data > max->data){
			max->data = temp->data;}
		else if (temp->data < min->data){
			min->data = temp->data;}
		temp=temp->next;
	}
	printf(" min = %d // max = %d",min->data, max->data);

}


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900