while (p != NULL)
{
if (p->data > max)
{
max = p->data;
p = p->next;
}
}
将其更新为
while (p != NULL)
{
if (p->data > max)
{
max = p->data;
}
p = p->next;
}
否则你的代码将陷入无限循环。
,
我通常更喜欢一行比较,这样代码容易理解,不会出现总线
int Max(struct node *p) {
int max_number = INT_MIN;
while (p != NULL) {
max_number = max(max_number,p->data);
p = p->next;
}
return max_number;
}
,
您的代码是 C 和 C++ 代码的混合体。您应该选择其中一种,并使用适合您选择的习语。混合使用 C 和 C++ 是失败的秘诀。按照编码,该程序比 C++ 更接近 C,只需删除 C++isms 和 C 中的程序。
它被卡住的原因是当值 p
不大于 while
时 p->data
不会在 max
循环中更新.但是请注意,还有其他问题:如果所有值都小于 -100
,代码将找不到最大值,如果列表为空,它将返回 -100
。
这是一个修改后的 C 版本,修正了两个问题:
#include <stdio.h>
#include <stdlib.h>
//declaring a global head/first pointer which stores the address of first node
struct node {
int data;
struct node *next;
} *first = NULL;
/* append the elements from a[] to the end of the linked list */
void create(const int a[],int n) {
struct node *last;
last = first;
while (last && last->next) {
last = last->next;
}
for (int i = 0; i < n; i++) {
// t = new node;
struct node *t = (struct node *)malloc(sizeof(struct node));
if (t == NULL)
return;
t->data = a[i];
t->next = NULL;
if (last)
last->next = t;
else
first = t;
last = t;
}
}
void display(const struct node *p) {
while (p != NULL) {
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int getmax(struct node *p) {
int max = 0;
if (p != NULL) {
max = p->data;
p = p->next;
while (p != NULL) {
if (max < p->data) {
max = p->data;
}
p = p->next;
}
}
return max;
}
int main() {
int a[] = { 3,5,7,10,15,8,12,20 };
create(a,sizeof(a) / sizeof(a[0]));
display(first);
printf("The maximum of the linked list is: %d\n",getmax(first));
return 0;
}
,
除了这两行
using namespace std;
和
cout << "The maximum of the linked list is : " << m;
程序中没有任何内容可以作为 C++ 代码而不是 C 代码引用。此外,您甚至忘记包含标题 <iostream>
。
而且程序有一个不一致的界面。一些函数直接处理全局变量first
,而另一些函数通过参数接受变量。
即使是函数 create
也可以调用未定义的行为,因为用户可以将零或负数作为第二个参数传递。
void create(int a[],int n) {
int i;
struct node *t,*last;
first = (struct node *)malloc(sizeof(struct node));
first->data = a[0];
//...
在这种情况下,表达式 a[0]
具有不确定的值。
函数Max
(为什么它的名字以大写字母开头?)有一个错误。移动到下一个节点仅在 p->data > max
if (p->data > max) {
max = p->data;
p = p->next;
}
您需要放置语句
p = p->next;
在 if 语句之后
int Max(struct node *p) {
int max = -100;
while (p != NULL) {
if (p->data > max) {
max = p->data;
}
p = p->next;
}
return max;
}
但无论如何,该函数没有多大意义,因为空列表的返回值 -100
可能是列表的有效数据。
如果您正在学习 C++,请从 C++ 17 标准开始学习,并使用 C++ 的特性而不是 C 的特性作为 C++ 运算符 new 而不是 C 函数 malloc。
您的程序确实是一个 C++ 程序,可以通过以下方式查找示例。
#include <iostream>
#include <optional>
#include <functional>
#include <iterator>
struct node
{
int data;
node *next;
};
void clear( node * &head )
{
while ( head ) delete std::exchange( head,head->next );
}
void create( node *&head,const int a[],size_t n )
{
clear( head );
node **current = &head;
for (size_t i = 0; i < n; i++)
{
*current = new node{ a[i],nullptr };
current = &( *current )->next;
}
}
std::ostream &display( const node *head,std::ostream &os = std::cout )
{
for (const node *current = head; current; current = current->next)
{
os << current->data << " -> ";
}
return os << "null";
}
std::optional<int> max( const node *head )
{
std::optional<int> max_value;
if (head)
{
max_value = head->data;
for (const node *current = head->next; current; current = current->next)
{
if (max_value < current->data) max_value = current->data;
}
}
return max_value;
}
int main()
{
node *head = nullptr;
int a[] = { 3,20 };
create( head,a,std::size( a ) );
display( head ) << '\n';
auto max_value = max( head );
if ( max_value )
{
std::cout << "The maximum of the linked list is : " << *max_value << '\n';
}
else
{
std::cout << "The list is empty.\n";
}
clear( head );
max_value = max( head );
if ( max_value )
{
std::cout << "The maximum of the linked list is : " << *max_value << '\n';
}
else
{
std::cout << "The list is empty.\n";
}
return 0;
}
程序输出为
3 -> 5 -> 7 -> 10 -> 15 -> 8 -> 12 -> 20 -> null
The maximum of the linked list is : 20
The list is empty.
本文链接:https://www.f2er.com/71757.html