Re: [問題] 廣義串列(generalized list)要怎麼建

看板C_and_CPP作者 (喲)時間11年前 (2012/09/16 12:04), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《supercygnus (......)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : Dev c++ : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : 問題(Question): : 不知道怎麼建 是要做HSM的Data Structures in C++所說的generalized list嗎? http://www.cs.miami.edu/~geoff/Courses/MTH517-00S/Content/LinkedLists/More LinkedLists.html 第一個要先把資料做個統合啊,假設你已經有個generalized list struct GList { Data d; struct GList* next; } Data d 要可以是數字,文字,或是另一個 GList, 那就 class Data { public: Data(double n); Data(string s); Data(struct GList list); } 然後有幾個操作要做: struct GList* getEmptyList() { struct GList* list; list->next = null; return list; } struct GList* appendList(struct GList* list, Data d) { struct GList* p; p = list; while (p->next != null) p = p->next; p->next = new getEmptyList(); p->next->d = d; } 這樣用起來就是: list = getEmptyList(); data1 = new Data(10); list1 = appendList(list, data1); data2 = new Date("hello,world"); list2 = appendList(list1, data2); data3 = new Data(list1); data3 = appendList(list2, data3); 可以這樣做,但有點麻煩, Data d 要反解出資料也麻煩. 用C++的繼承和多型應該可以做得比較簡單. : 餵入的資料(Input): : 預期的正確結果(Expected Output): : 錯誤結果(Wrong Output): : 程式碼(Code):(請善用置底文網頁, 記得排版) : #include<iostream> : using namespace std; : ListNode getNode(); : struct ListNode{ : ListNode *link; : bool tag; : union{ : char data; : ListNode *dlink; : }; : }; : int main(void){ : ListNode x; : ListNode* h=NULL; : system("pause"); : return 0; : } : ListNode createList(){ : ListNode* p,q; : char x; : p=getNode(); : } : ListNode getNode(){ : ListNode k; : return k; : } : 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 36.226.98.45
文章代碼(AID): #1GLK_BI- (C_and_CPP)
文章代碼(AID): #1GLK_BI- (C_and_CPP)