⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 heterogeneouslist.cpp

📁 Realization of Heterogeneous List using C++
💻 CPP
字号:
#include <iostream>#include <string>using namespace std;class Base {protected:    int Serial;    string Name;    string Date;public:    Base(int serial, string name, string date):Serial(serial),Name(name),Date(date) {}    virtual ~Base() {}    virtual void print() {         cout << "Serial No. is: " << Serial << endl             << "Name is: " << Name << endl             << "Date is: " << Date << endl;    }    int getSerial() { return Serial; }};class Derived_1 : public Base {private:    double Value;public:    Derived_1(int serial, string name, string date, double value)    :Base(serial, name, date),Value(value) {}    ~Derived_1() {}    void print() {        Base::print();        cout << "Value is: " << Value << endl;    }};class Derived_2 : public Base {private:    string Type;public:    Derived_2(int serial, string name, string date, string type)    :Base(serial, name, date),Type(type) {}    ~Derived_2() {}    void print() {        Base::print();        cout << "Type is: " << Type << endl;    }};class Derived_3 : public Base {private:    bool Status; // 0:active    1:inertpublic:    Derived_3(int serial, string name, string date, bool status)    :Base(serial, name, date),Status(status) {}    ~Derived_3() {}    void print() {        Base::print();        cout << "Status is: " << Status << endl;    }};class Node {public:    Base* Data;    Node* Next;    Node(Base* base):Data(base),Next(NULL) {}};class List {private:    Node* Head;public:    List():Head(NULL) {}    void print();    void insert(Base*);    void remove(int);    void inquiry(int);};        void List::print() {            Node* ptrCurr = Head;            if(Head == NULL)                cout << "Empty List.\n";            while(ptrCurr != NULL) {                ptrCurr->Data->print();                ptrCurr = ptrCurr->Next;                cout << endl;            }        }        void List::insert(Base* base) {            Node* newnode = new Node(base);            if(Head == NULL) {                Head = newnode;            }            else {                newnode->Next = Head;                Head = newnode;            }        }        void List::inquiry(int serial) {            Node* ptrCurr = Head;                        while( (ptrCurr != NULL)  && (ptrCurr->Data->getSerial() != serial) )                ptrCurr = ptrCurr->Next;            if( ptrCurr == NULL )                cout << "\nOperation Exception.\n"                      << "Not Find Element.\n" << endl;            else {                cout << "\nFIND.\n";                ptrCurr->Data->print();            }                        cout << endl;        }                void List::remove(int serial) {            Node* ptrCurr = Head;            Node* ptrPrev = Head;            while( (ptrCurr != NULL)  && (ptrCurr->Data->getSerial() != serial) ) {                ptrPrev = ptrCurr;                ptrCurr = ptrCurr->Next;                   }            if(ptrCurr == NULL)                cout << "\nOperation Exception.\n"                      << "Not Find Element.\n" << endl;            else {                ptrPrev->Next = ptrCurr->Next;                cout << "\nThe Following Information Will Be Removed.\n";                ptrCurr->Data->print();                                delete ptrCurr;                cout << "Delete Completed.\n" << endl;            }        }int main() {    int enterInquiry;    int enterDelete;        Derived_1 derived_1(63, "MacBook", "2008-01-15", 14000.0);    Derived_2 derived_2(110, "TravelMate", "2004-07-04", "Laptop");    Derived_3 derived_3(150, "Aspire One", "2008-12-25", 1);        List test_list;        //insert operation    test_list.insert(&derived_1);    test_list.insert(&derived_2);    test_list.insert(&derived_3);        test_list.print();        //inquiry operation    cout << "Search_Enter the Serial No.: \n";    cin >> enterInquiry;    test_list.inquiry(enterInquiry);        //delete operation    cout << "Delete_Enter the Serial No.: \n";    cin >> enterDelete;    test_list.remove(enterDelete);        cout << "\nAfter Delete Operation:\n";    test_list.print();        system("PAUSE");    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -