在链表中查找最小元素 [英] Find minimum element in linked list

查看:177
本文介绍了在链表中查找最小元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这些函数查找链表中的最小元素

int findmin(struct node *)

void display(struct node *)

void append(struct node **,int) - 用于在末尾添加节点



我尝试过:



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:

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

推荐答案

你的Main需要打印findmin返回的值,目前它调用findmin但是没有对返回值做任何事情。



你有;



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));


这篇关于在链表中查找最小元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆