Exercício de tabela linear de linguagem do tipo C: determine o nó com o maior valor em uma lista vinculada por meio de um

Este código pode ser executado normalmente. É uma
análise real da linguagem C : percorra uma vez para encontrar o maior valor e, em seguida, imprima esse nó

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

typedef int Status;
typedef int Elemtype;
#define OK 1
#define ERROR 0 
typedef struct LNode
{
	Elemtype data;
	struct LNode *next;
}LNode,*LinkList;
 
void CreateList_L(LinkList &L,int n)  //尾插法建立
{   int i;
    LNode *r,*p;
	L=new LNode;
	L->next=NULL;
	r=L;
	printf("请输入元素:\n");
	for(i=0;i<n;++i)
	{
		p=new LNode;
		scanf("%d",&p->data);
		p->next=NULL;
		r->next=p;
		r=p;
	}
}

void MaxElem_L(LinkList &L)
{
	int max,i=0;
	LNode *p=L->next;
	max=p->data;
	while(p)
	{
		if(p->data>max)
		   max=p->data;
        p=p->next;
	}
	p=L->next;
	while(p)
	{   i++;
		if(p->data==max)
		   printf("第%d个结点的值为:%d\n",i,p->data); 
        p=p->next;
	}
	
}
int main()
{   int n;
	LNode *L;
	
	printf("请输入线性表的元素个数:");
	scanf("%d",&n);
	CreateList_L(L,n);
	printf("链表中值最大的结点是:\n");
	MaxElem_L(L);
	return 0; 
}
Publicado 56 artigos originais · elogiado 53 · visitas 2326

Acho que você gosta

Origin blog.csdn.net/September_C/article/details/105616318
Recomendado
Clasificación