Member 13297682 Ответов: 1

Найти минимальный элемент в связанном списке


найдите минимальный элемент в связанном списке с помощью этих функций
int findmin(struct node *)
void display(struct node *)
void append(struct node **, int) - для добавления узла в конце

Что я уже пробовал:

#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() */

Patrice T

А у вас есть вопрос ?

1 Ответов

Рейтинг:
0

Michael_Davies

Ваш Main должен напечатать значение, возвращаемое findmin, в настоящее время он вызывает findmin, но ничего не делает с возвращаемым значением.

У тебя есть;

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


Попробуй;

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