Untitled

 avatar
unknown
plain_text
3 years ago
6.7 kB
20
Indexable
#include <iostream>
#include <string>
#include <stack>


using namespace std;

/// link list

//class Node
//{
//public:
//    int data;
//    Node* pnext=NULL;
//
//};
//class Slist {
//public:
//    Node* ph = NULL;
//    Node* pt = NULL;
//    int size = 0;
//    //
//    Node * ktNode(int data);
//    void addh(int data);
//    void addt(int data);
//    void sort();
//    void delh();
//    void print();
//};
//
//Node * Slist::ktNode(int data) {
//    Node * temp = new Node;
//    temp->data = data;
//    return temp;
//}
//void Slist::addh(int data) {
//    if (ph == NULL) {
//        ph = pt = ktNode(data);
//        size += 1;
//    }
//    else
//    {
//        Node * temp = ktNode(data);
//        temp->pnext = ph;
//        ph = temp;
//        size += 1;
//
//
//    }
//}
//void Slist::addt(int data) {
//    if (ph == NULL) {
//        ph = pt = ktNode(data);
//        size += 1;
//
//    }
//    else
//    {
//        Node* temp = ktNode(data);
//        pt->pnext = temp;
//        pt = temp;
//        size += 1;
//
//
//    }
//}
//
//void Slist::sort() {
//    for (Node * temp = ph; temp->pnext != NULL; temp=temp->pnext)
//    {
//        for (Node * temps = temp->pnext ; temps != NULL; temps = temps->pnext)
//        {
//            if (temp->data > temps->data) {
//                Node* swap = ktNode(temp->data);
//                temp->data = temps->data;
//                temps->data = swap->data;
//            }
//
//        }
//
//    }
//}
//void Slist::print() {
//    cout << endl;
//    for (Node* temp = ph; temp != NULL; temp = temp->pnext)
//    {
//        
//        cout << temp->data << " ";
//
//    }
//}
//
//



/// sx

// noi bot

void noibot(int a[],int n) {

    for (int i = 0; i < n - 1; i++)
    {
        for (int j = n-1; j > i; j--)
        {
            if (a[i] > a[j]) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }

        }

    }
}


// selec -  chon

void selec(int a[], int n) {
    for (int i = 0; i < n-1; i++)
    {
         int pMin = i;

        for (int j = i+1; j < n; j++)
        {
            if (a[pMin] >a[j])
            {
                pMin = j;
            }

        }
        if (i != pMin) {
            int temp = a[i];
            a[i] = a[pMin];
            a[pMin] = temp;
        }

    }
}

//insert - chon

void chon(int a[], int n) {
    int pos, x;
    for (int i = 1; i < n; i++) {
        x = a[i];
        pos = i;
        while (pos > 0 && x<a[pos-1])
        {
            a[pos] = a[pos - 1];
            pos--;
        }
        a[pos] = x;
    }
}


// search 

// tuyen tinh  ( de cu duyet if neu co thi thong bao )

// nhi phan

void searchnhiphan(int a[], int n, int key) {
    int l, r, m, z=0;
    l = 0; r = n - 1;
    while (l<=r)
    {
        m = (l + r) / 2;
        if (a[m] == key) {
            cout << "\nco \n";
            int z = 1;
            return;
        }
       else if (a[l] < key)l = m + 1;
        else r = m - 1;

    }
    if (z == 0)cout << "\n ko \n";
}

//// true

class infor {
public:
    string name;
    int sdt;

    void input();
    void output();
};

class Node {
    typedef Node* Tree;

public:
    infor data;
    Node* l = NULL;
    Node* r = NULL;

    friend void ktc(Tree& t);
    friend void addNode(Tree& t, infor data);
    friend void duyetcay(Tree t);
    friend void tk(Tree t, int sdt);
    friend void ditimnodethemang(Tree& x, Tree& y);
    friend void XoaNode(Tree& t, int data);




};
typedef Node* Tree;



void infor::input() {
    cout << "\n Nhap ten\n";
    getline(cin, name);
    cout << "Nhap sdt\n";
    cin >> sdt;
    cin.ignore();
}
void infor::output() {
    cout << "Ten la\n";
    cout<< name<<endl;
    cout << "Sdt\n";
    cout << sdt << endl;
}


void ktc(Tree& t) {
    t = NULL;
}

void addNode(Tree &t, infor data) {
    if (t==NULL)
    {
        t = new Node;
        t->data = data;
    }
    else
    {
        if (t->data.sdt > data.sdt) {
            addNode(t->l, data);
        }
        else if (t->data.sdt < data.sdt) {
            addNode(t->r, data);

        }
    }

}
void duyetcay(Tree t) {              // duyet cay theo thu tu tang dan de xuat ra man hinh
    if (t != NULL) {
        duyetcay(t->l);
        t->data.output();
        duyetcay(t->r);
    }
}
void tk(Tree t, int sdt) {
    if (t == NULL) {
        cout << "Khong co data trong cay\n";
    }
    else
    {
        if (sdt < t->data.sdt) {
            tk(t->l, sdt);
        }
        else if (sdt > t->data.sdt) {
            tk(t->r, sdt);
        }
        else
        {
            cout << "co sdt\n";
        }
    }

}
void ditimnodethemang(Tree& x, Tree& y) {
    if (y->l != NULL) {
        ditimnodethemang(x, y->l);
    }
    else
    {
        x->data = y->data;
        x = y;
        y = y->r;
    }

}

void XoaNode(Tree& t, int data) {
    if (t == NULL) return;
    else {
        if (t->data.sdt > data) {
            XoaNode(t->l, data);
        }
        else if (t->data.sdt < data) {
            XoaNode(t->r, data);
        }
        else {
            Node* x = t;
            if (t->l == NULL) {
                t = t->r;
            }
            else if (t->r == NULL)
            {
                t = t->l;

            }
            else {
                ditimnodethemang(x, t->r);

            }
            delete x;
        }
    }

}

// stack chuyen doi he dem

//void output(stack<int> s) {
//
//    while(!s.empty())
//    {
//        cout << s.top() << " ";
//        s.pop();
//    }
//}
//
//void chuyendoicoso(stack<int>& s,int coso,int hetp) {
//    while (hetp != 0)
//    {
//        int x = hetp % coso;
//        s.push(x);
//        hetp /= coso;
//    }
//
//}







// Driver Code
int main()
{
    //Slist list;
    //list.addt(5);
    //list.print();



 /*   stack<int> s;
    chuyendoicoso(s, 2, 10);
    output(s);*/





  /*  int a[5] = { 1,4,2,5,0 };
    int n = 5;*/



    /*chon(a, n);
    searchnhiphan(a, n, 2);*/
    


   /* for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";

    }*/

    //Tree t;
    //ktc(t);
    //infor x, y, z;
    //x.input();
    //y.input();
    //z.input();

    //addNode(t, x);
    //addNode(t, y);
    //addNode(t, z);
    //cout <<"-------"<< endl;
    //duyetcay(t);

    


}
Editor is loading...