C-like language-linear table exercise: determine the node with the largest value in a singly linked list through a traversal.

This code can run normally. Is a real C language
analysis: traverse once to find the largest value, and then output that node

#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; 
}
Published 56 original articles · praised 53 · visits 2326

Guess you like

Origin blog.csdn.net/September_C/article/details/105616318