Untitled

 avatar
unknown
plain_text
2 years ago
6.3 kB
3
Indexable
#include <iostream>

using namespace std;

class Singly_list
{
    struct Node
    {
	    int info;
	    Node *link;
    }; Node *first;

public:
    void init() /*initiate a list with empty elements*/
    {
        first = NULL;
    }

    void Process_list() /*browse a list*/
    {
        Node *p;
        p = first;
        cout << "Processing list" << endl;
        while (p != NULL)
        {
            cout << p->info << endl;
            p = p->link;
        }
    }

    void Insert_first(int x) /*add new element at the begining*/
    {
        Node *p;
        p = new Node;
        p->info = x;
        p->link = first;
        first = p;
    }

    int del_first() /*delete the first elements of a list*/
    {
        if (first != NULL)
        {
            Node *p = first;
            first = first->link;
            delete p;
            return 1;
        }
        else { return 0; }
    }

    void Insert_last(int x) /*add new element at the end*/
    {
        Node *p, *p_temp;
        //initiate object p
        p = new Node;
        p->info = x;
        p->link = NULL;

        //initiate object p_temp for browsing
        p_temp = first;
        if (first != NULL)
        {
            while (p_temp->link != NULL)
            {
                p_temp = p_temp->link;
            }
            p_temp->link = p;
        }
        else { first = p; }
    }

    int del_last() /*delete the last elements of a list*/
    {
        if (first != NULL)
        {
            Node *last, *prev_last;
            last = first, prev_last = NULL;
            if (last != NULL)
            {
                while (last->link != NULL)
                {
                    prev_last = last;
                    last = last->link;
                }
            }
            if (last != first) //check if "last" is the first elements in list
            {
                prev_last->link = NULL;
            }
            else { first = NULL; }
            delete last;
            return 1;
        }
        return 0;
    }

    void inputs(int &n)
    {
        int a, cond;
        cout << "Nhap vao n" << endl;
        cin >> n;
        cout << "Nhap cach insert(0:first/1:last): ";
        cin >> cond;
        cout << "Nhap vao cac phan tu" << endl;
        if (cond == 1) //insert at the beginning
        {
            for (int i = 0; i < n; i++)
            {
                cin >> a;
                Insert_first(a);
            }
        }
        else //insert at the end
        {
            for (int i = 0; i < n; i++)
            {
                cin >> a;
                Insert_last(a);
            }
        }
    }

    void switch_last_first()
    {
        Node *p = first, *m_temp = first, *n_temp;
        int m, n;
        while(p->link != NULL)
        {
            p = p->link;
        }
        n_temp = p;
        m = m_temp->info; //this is value of first
        n = n_temp->info; //this is value of last
        del_last();
        Insert_last(m);
        del_first();
        Insert_first(n);
        
    }

    void middle_elements(int n)
    {
        if(n == 2) // even number size n and n == 2 case
        {
            Node *p = first, *p_temp = p->link;
            cout << "The middle are " << p->info << " and " << p_temp->info << endl;
        }

        if(n % 2 == 0 && n > 2) //even number size n and n > 2 case
        {
            int middle = (n/2) - 2;
            Node *p = first, *p_temp;
            for(int i = 0; i <= middle; i++)
            {
                p = p->link;
            }
            p_temp = p->link;
            cout << "The middle are " << p->info << " and " << p_temp->info << endl;
        }

        if(n %2 != 0) // odd number size case
        {
            int middle = int(n/2) - 1;
            Node *p = first;
            for(int i = 0; i <= middle; i++)
            {
                p = p->link;
            }
            cout << "The middle is " << p->info << endl;
        }
    }

    void check_sorted()
    {
        Node *p = first;
        int prev_temp, check_, flag = 0;
        cout << "Input the sort checking type (0:ascend/1:descend): ";
        cin >> check_;
        if(check_ == 0) // ascending case
        {
            while(p!= NULL)
            {
                prev_temp = p->info;
                p = p->link;
                if(p!= NULL)//to prevent the null pointer while loop cannot check during the instructions
                {
                    if(prev_temp > p->info)
                    {
                        cout << "Array is not sorted (ascending case)" << endl;
                        flag = 1;
                        break;
                    }
                }
            }

            if(flag == 0)
            {
                cout << "array is sorted (ascending case)" << endl;
            }
        }
        if(check_ == 1) //descending case
        {
            while(p!= NULL)
            {
                prev_temp = p->info;
                p = p->link;
                if(p != NULL)//to prevent the null pointer while loop cannot check during the instructions
                {
                    if(prev_temp < p->info)
                    {
                        cout << "Array is not sorted (descending case)" << endl;
                        flag = 1;
                        break;
                    }
                }
            }
            if(flag == 0)
            {
                cout << "array is sorted (descending case)" << endl;
            }
        }
    }
};
int main()
{
    //-----function input(n, option)-----//
	//n: number of element in list.
	//option: 0: for inserting at the last position,
	//        1: for inserting at the first position.
	int n, cond;
	Singly_list singly_list;
    singly_list.init();
    singly_list.inputs(n);
    singly_list.Process_list();
    //singly_list.switch_last_first();
    singly_list.Process_list();
    singly_list.middle_elements(n);
    singly_list.check_sorted();
	//system("pause");
	return 0;
}