之前用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;};