設計一個c++ 通用鏈表:實現多態雙向的功能

來源:互聯網
上載者:User
之前用c實現的通用鏈表, 將鏈表的data域從具體類型轉變為 void*指標 ,用c實現時費力氣的是指標的賦值和也要做到通用的列印

連結:c實現的通用的雙向鏈表現在用c++重新實現,思想大都一致:

不採用模板類,而採用虛函數實現多態性,達到通用的目的,data域不儲存任何跟類型有關的資訊,而是指標,將資料放於抽象類別中,由指標與之建立聯絡。

鏈表有頭指標 尾指標,以及之後的一系列插入刪除列印操作,這些函數都寫在鏈表類中。

鏈表指標類型為 鏈表結點類,結點的指標域和 data域,都是指標類型,data域 指向一個抽象類別 object對象 。

object類是一個抽象類別,當需要鏈表存放整型資料,就具體實現一個存放整型的子類去繼承這個類,必須實現抽象類別中的虛函數,以列印函數為例, 每一個不同資料類型的子類都得有自己的列印函數,而object 不關心如何列印。

class intobject :public Object{public:intobject(int d = 0) :data(d){}~intobject(){}void Print()const{cout << data << "-->";}private:int data;};////////////////////////////////////////////////////////////class strobject :public Object{public:strobject(char *str) {if (str == NULL){data = new char[1];data[0] = '\0';}else{data = new char[strlen(str) + 1];strcpy(data, str);}}~strobject(){}void Print()const{cout << "\"" << data << "\"" << "-->";}private:char * data;};////////////////////////////////////////////////////////////class floatobject :public Object{public:floatobject(float d = 0) :data(d){}~floatobject(){}void Print()const{cout << data << "-->";}private:float data;};////////////////////////////////////////////////////////////void main(){list mylist;for (int i = 0; i < 5; ++i){intobject *pi = new intobject(i);mylist.push_back(pi);}mylist.printlist();char* arr[5] = { "affd", "fdas", "fdfss", "ere", "qret" };for (int i = 0; i < 5; i++){strobject*ps = new strobject(arr[i]);mylist.push_back(ps);}mylist.printlist();float brr[5] = { 0.34, 54.32, 0.53, 43.2, 5.878 };for (int i = 0; i < 5; i++){floatobject*ps = new floatobject(brr[i]);mylist.push_back(ps);}mylist.printlist();}

鏈表的釋放:

class Object{public:Object(){}virtual ~Object()//因為是虛函數,調動父類析構的 同時 調動子類的析構{}////定義介面   通用的列印virtual void Print()const = 0;//純虛函數  子類繼承了之後必須實現列印函數};class list;class listnode{friend class list;public:listnode(){data = NULL;next = NULL;}listnode(Object *pobj){data = pobj;next = NULL;}~listnode(){delete data;next = NULL;}private:Object *data;listnode *next;};class list{public:list(){head = tail = new listnode;}~list(){listnode *delp = head->next;while (delp!= tail){head->next = delp->next;delete delp;delp = head->next;}delete head;head = tail = NULL;}void push_back(Object *pb)//尾插{listnode *s = new listnode(pb);assert(s != NULL);tail->next = s;tail = s;}void printlist()const{listnode *p = head->next;while (p != NULL){p->data->Print();p = p->next;}cout << "NULL" << endl;}private:listnode *head;listnode *tail;};////////////////////////////////////////////////////////////class intobject :public Object{public:intobject(int d = 0) :data(d){}~intobject(){cout << "delete int" << endl;}void Print()const{cout << data << "-->";}private:int data;};////////////////////////////////////////////////////////////class strobject :public Object{public:strobject(char *str) {if (str == NULL){data = new char[1];data[0] = '\0';}else{data = new char[strlen(str) + 1];strcpy(data, str);}}~strobject(){cout << "delete string" << endl;delete []data;data = NULL;}void Print()const{cout << "\"" << data << "\"" << "-->";}private:char * data;};
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.