mercredi 19 août 2020

Finding the smallest and largest numbers by single linked list and two classes

I got several numbers from user by single linked list and my program's task is finding the smallest and largest number in the linked list by two classes and print them on the sreen. But after a time, my program got closed and i didn't see anything. What went wrong?

#include<iostream>
using namespace std;

struct Node
{
    double Number;
    struct Node *Point;

} *End = nullptr;
typedef struct Node node;

namespace Min_Max
{
    class Min
    {
        node *Result = End;

        public: Min()
        {
            if(Result == nullptr)
            {
                cout << "You didn\'t enter anything!\a";
                system("pause");

                exit(EXIT_FAILURE);
            }

            node *Counter = Result->Point;

            while(Counter != nullptr)
            {
                if(Counter->Number < Result->Number)
                    Result = Counter;

                Result = Result->Point;
            }
        }

        node* Show()
        {
            return Result;
        }
};

class Max
{
    private:
        node *Result = End;

    public: 
        Max()
        {
            if(Result == nullptr)
            {
                cout << "You didn\'t enter anything!\a";
                system("pause");

                exit(EXIT_FAILURE);
            }

            node *Counter = Result->Point;

            while(Counter != nullptr)
            {
                if(Counter->Number > Result->Number)
                    Result = Counter;

                Result = Result->Point;
            }
        }

        node* Show()
        {
            return Result;
        }
    };
};

int main()
{
    node *linker = nullptr;
    register short int Counter = 1;

    while(1)
    {
        linker = new node;
        if(linker == nullptr)
        {
            cout << "An error occurred during allocating memory." << endl << endl;
            system("pause");

            return 0;
        }

        cout << "Number " << Counter << ": Enter your number: ";
        cin >> linker->Number;
        system("cls");

        if(linker->Number == 0)
        {
            delete linker;
            break;
        }

        linker->Point = End;
        End = linker;

        Counter++;
    }

    Min_Max::Min Min;
    Min_Max::Max Max;

    cout << "The smallest number is " << (Min.Show())->Number << endl;
    cout << "The largest number is " << (Max.Show())->Number << endl;

    return 0;
}

My C++ compiler is GCC-C++11 and my operating system is Windows 10.

Aucun commentaire:

Enregistrer un commentaire