Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
find minimum element in linked list using these functions
int findmin(struct node *)
void display(struct node *)
void append(struct node **,int)- for appending node at the end

What I have tried:

C++
#include<stdio.h>
#include<stdlib.h>

struct node
{
  int data;
  struct node *link;
}*start;
void append(struct node * , int );
void display(struct node *);
int findmin(struct node *);

int main()
{   
     int value;
     char ans;
     if (start==NULL)
        printf("Enter the value\n");
     scanf("%d",&value);
     append(start,value);
     ans='Y';
     while(ans=='Y')
     {
        printf("enter the value\n");
        scanf("%d", &value);
        append(start,value);
        printf("Do you want to add another node?Type Y/N\n");
        scanf("%c",&ans);
     }
     printf("the elements in linked list are: ");
     display(start);
     printf("The minimum element in the linked list is");
     findmin(start);
} 

  
void append(struct node *start,int value)
{
	struct node *p,*tmp;
	tmp=(struct node *)malloc(sizeof(struct node));
	tmp->data=value;
	p=start;
	while(p->link!=NULL)
		p=p->link;
	p->link=tmp;
	tmp->link=NULL;
}/*End of addatend()*/





int findmin(struct node *start)
{
	int min=start->data;
	while(start!=NULL)
	{
		if(start->data < min)
		   min = start->data;
		start=start->link;
	}
	return min;
}/*End of findmin()*/

void display(struct node *start)
{
	struct node *p;
	if(start==NULL)
	{
		printf("\nList is empty\n");
		return;
	}
	p=start;
	while(p!=NULL)
	{
		printf("%d",p->data);
		p=p->link;
	}
	printf("\n\n");
}/*End of display() */
Posted
Updated 6-Jul-17 20:34pm
v2
Comments
Patrice T 7-Jul-17 3:18am    
And you have a question ?

1 solution

Your Main needs to print the value returned by findmin, currently it call findmin but does not do anything with the returned value.

You have;

printf("the elements in linked list are: ");
display(start);
printf("The minimum element in the linked list is");
findmin(start);


Try;

printf("the elements in linked list are: ");
display(start);
printf("The minimum element in the linked list is %d", findmin(start));
 
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