建立单链表,求相邻两节点data值之和为最大的第一节点

#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct Lnode
{
	int data;
	struct Lnode *next;
}Lnode, *LinkList;
LinkList createList()//建表
{
	Lnode *s, *p;
	int x;
	LinkList L = new(Lnode);
	L->next = NULL;
	p = L;
	cin >> x;
	while (x != 0)
	{
		s = new(Lnode);
		s->data = x;
		s->next = NULL;
		p->next = s;
		p = s;
		cin >> x;
	}
	return L;
}
void adjmax(LinkList L)//遍历求最大
{
	L = L->next;//L是头结点,data为空值
	int index;
	int max = L->data + L->next->data;//取第一个和第二个的和,来进行初始化
	while (L->next != NULL)
	{
		if ((L->data + L->next->data) > max)
		{
			max = (L->data + L->next->data);
			index = L->data;
		}
		L = L->next;
	}
	cout << "最大节点:" << index << endl;
}
void empty(LinkList &L)//对链表进行置空
{
	Lnode *p;
	while (L != NULL)
	{
		p = L;
		L = L->next;
		delete(p);
	}
}
int main()
{
	bool flag = true;
	LinkList myList;
	
	while (flag == true)
	{
		myList=createList();
		adjmax(myList);
		empty(myList);
		char end;
		cout << "继续输入?(Y/N):";
		cin >> end;
		if (end == 'n' || end == 'N')
			flag = false;
	}
	return 0;
}

相关文章:

  • 2022-12-23
  • 2021-12-30
  • 2022-12-23
  • 2021-06-25
  • 2021-09-09
  • 2022-12-23
  • 2022-01-22
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2021-06-28
  • 2022-01-21
  • 2021-07-19
  • 2022-12-23
  • 2021-10-26
相关资源
相似解决方案