C++ 列表中的最大元素

C++ 列表中的最大元素,c++,struct,C++,Struct,我需要找到列表中最大的元素。在下面的代码中,取消订阅项目并订购它们。如何找到列表的最后一个元素?我想我需要再添加一个函数void maksimum(),但我遇到了麻烦 #include <iostream> #include <string> #include <time.h> #include <conio.h> #include <cstdlib> using namespace std; struct element {

我需要找到列表中最大的元素。在下面的代码中,取消订阅项目并订购它们。如何找到列表的最后一个元素?我想我需要再添加一个函数
void maksimum()
,但我遇到了麻烦

#include <iostream>
#include <string>
#include <time.h>
#include <conio.h>
#include <cstdlib>

using namespace std;

struct element
{
    int number;
    element* next;
    element();
};

element::element()
{
    next = NULL;
}

struct list
{
    element* first;
    void fill_list(int number);
    void segregate();
    void show_list();
    void maksimum();
    list();
};

list::list()
{
    first = NULL;
}

void list::fill_list(int number)
{
    element *nowy = new element;
    nowy->number = number;

    if(first == 0)
    {
        first = nowy;
    }
    else
    {
        element* temp = first;
        while(temp->next)
        {
            temp = temp->next;
        }
        temp->next = nowy;
    }
}

void list::show_list()
{
    element* temp = first;
    if(temp == 0)
    {
        cout << "List is empty." << endl;
        cout << "No smallest element " << endl;
        cout << "No largest element" << endl;
    }
    else
    {
        while(temp)
        {
            cout << temp->number << endl;
            temp = temp->next;
        }
        cout << "the smallest element: : " << first->number << endl;
        if(first->next == 0)
        {
            cout << "Largest element = Smallest element :)" << endl;
        }
    }
}

void list::segregate()
{
    element* new_first = NULL;
    element* prv;
    element* temp;
    element* maks;

    while(first)
    {
        maks = first;
        prv = NULL;
        temp = first;

        while(temp->next)
        {
            if(temp->next->number > maks->number)
            {
                prv = temp;
                maks = temp->next;
            }
            temp=temp->next;
        }

        if (prv)
        {
            prv->next = maks->next;
        }
       else
       {
           first = maks->next;
       }
       maks->next = new_first;
       new_first = maks;
    }
    first = new_first;
}


int main()
{
    int n=0;
    int number=0;
    list* base = new list;
    cout << "Size of list: " << endl;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cout << "No " << i+1 << ": ";
        cin >> number;
        base->fill_list(number);
    }
    base->segregate();
    base->show_list();
    //base->maksimum();
    delete(base);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构元素
{
整数;
元素*下一个;
元素();
};
元素::元素()
{
next=NULL;
}
结构列表
{
元素*第一;
填空清单(整数);
空隙分离();
void show_list();
void maksimum();
list();
};
列表::列表()
{
第一个=空;
}
无效列表::填充列表(整数)
{
元素*nowy=新元素;
nowy->number=number;
如果(第一个==0)
{
第一个=现在;
}
其他的
{
元素*温度=第一;
while(临时->下一步)
{
温度=温度->下一步;
}
临时->下一步=现在;
}
}
void list::show_list()
{
元素*温度=第一;
如果(温度==0)
{
下一步;
}
其他的
{
first=maks->next;
}
maks->next=先新建;
新_first=maks;
}
第一个=新的第一个;
}
int main()
{
int n=0;
整数=0;
列表*base=新列表;
cout n;
对于(int i=0;i隔离();
基本->显示列表();
//base->maksimum();
删除(基数);
返回0;
}

我该怎么做呢?

好的。你说得对,但我认为我的代码显示了我的工作。没关系:)

我解决了我的问题。我的职能:^^

    void list::show_list()
{
    element * temp = first;
    if( temp == 0 )
    {
        cout << "List is empty." << endl;
        cout << "No smallest element " << endl;
        cout << "No largest element" << endl;
    }
    else
    {
        while( temp->next != 0 )
        {
            temp = temp->next;
        }
        cout << "The largest element: " << temp->number << endl;
        cout << "The smallest element: " << first->number << endl;
    }
}
void list::show_list()
{
元素*温度=第一;
如果(温度==0)
{

你说你在寻找最大的问题时遇到了问题,但是你也应该解释一下你尝试了什么,以及你遇到了什么困难。