找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
儲值後自動升級用戶組認識好友、聊天,分享生活趣事搞笑、娛樂、精彩的影片讓你看
海賊王無修一拳超人gecosplay中出mg 無碼
office聖石傳說國中神菜美舞mist家庭教師

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

(4月新番)[繁]搖曳露

[繁]怪獸八號02-

[繁]戰隊大失格03-

[繁]迷宮飯17-

[繁]怪人的沙拉碗04-

【高清繁】✡ 霹靂英
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 1786|回復: 4
打印上一主題下一主題

[作業]用XOR的概念,實作doubly linked list。[複製鏈接]

ycess87009 該用戶已被刪除
跳轉到指定樓層
樓主
發表於 2012-11-11 12:11 AM|只看該作者|倒序瀏覽
成為伊莉的版主,你將獲得更高級和無限的權限。把你感興趣的版面一步步地發展和豐盛,那種滿足感等著你來嚐嚐喔。
本帖最後由 ycess87009 於 2012-11-11 12:15 AM 編輯

XOR的意思:如果 i=10110,j=01100,i XOR j=11010。
作業規定:
利用課本提供的方法,實作doubly linked list
每個node的structure包含一個data,一個link。

作業輸出和輸入的範例如下:
Choice 1)insert 2)show list : 1
input sequence : 1 2 6 9 3 15
Choice 1)insert 2)show list : 2
Left to Right : 1 2 6 9 3 15
...
瀏覽完整內容,請先 註冊登入會員
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊
分享分享0收藏收藏0支持支持0
若瀏覽伊莉的時侯發生問題或不正常情況,請使用Internet Explorer(I.E)。

使用道具檢舉

ycess87009 該用戶已被刪除
頭香
發表於 2012-11-11 11:29 AM|只看該作者
若瀏覽伊莉的時侯發生問題或不正常情況,請使用Internet Explorer(I.E)。
小弟想到解決的方法了,謝謝看過問題的各位。

使用道具檢舉

Rank: 1

帖子
49
積分
183 點
潛水值
19570 米
3
發表於 2012-11-11 02:08 PM|只看該作者
本帖最後由 m0neypig 於 2012-11-11 02:40 PM 編輯

Let's assume we have 4 nodes A, B, C and D,
where A is head(left most), D is tail (right most):

Node                 A        B         C       D
Link pointer     0^B    A^C    B^D   D^0
...
瀏覽完整內容,請先 註冊登入會員

使用道具檢舉

Rank: 1

帖子
49
積分
183 點
潛水值
19570 米
4
發表於 2012-11-11 02:26 PM|只看該作者
本帖最後由 m0neypig 於 2012-11-11 02:37 PM 編輯

Since I am so bored  (= =;)
and this problem is quite interesting, I attach my solution to this problem in C++.
The code is wrapped up in a hurry so please take it easy on me.
(and there are a lot of error handlings undone..)

  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>

  4. // those macro are bad... but i'm lazy
  5. #define PRINT_CMD cout << "Choice [1]insert [2]show list [3]exit: "
  6. #define PRINT_ERR1 cout << "Unrecognized options, please enter 1, 2 or 3\n"
  7. #define PRINT_ERR2 cout << "Please insert the number first...\n"

  8. using namespace std;

  9. class Node
  10. {
  11. private:
  12.         int mLink;
  13.         int mVal;

  14. public:
  15.         Node(int val):mVal(val), mLink(0){}
  16.         ~Node(){};

  17.         // append the value to the right hand side of the list
  18.         // and return the right most node we just create
  19.         // precondition: we are in the most right hand side!
  20.         Node * append(int val)
  21.         {
  22.                 Node * node  = new Node(val);
  23.                 mLink = mLink ^ (int)node;
  24.                 node->mLink = (int)this ^ 0;
  25.                 return node;
  26.         }

  27.         static void iterate(Node * list)
  28.         {
  29.                 Node * pre = 0;
  30.                 Node * cur = list;

  31.                 while(cur->mLink != (int)pre){
  32.                         cout << cur->mVal << " " ;
  33.                         Node * temp = cur;
  34.                         cur = (Node *)( (int)pre ^ cur->mLink);
  35.                         pre = temp;
  36.                 }
  37.                 cout << cur->mVal << endl;
  38.         }

  39.         static void releaseResource(Node * list)
  40.         {
  41.                 if(list == NULL) return;
  42.                 Node * pre = 0;
  43.                 Node * cur = list;
  44.                 while(cur->mLink != (int)pre){
  45.                         Node * temp = cur;
  46.                         cur = (Node *)( (int)pre ^ cur->mLink);
  47.                         delete pre;
  48.                         pre = temp;
  49.                 }
  50.                 delete pre;
  51.                 delete cur;
  52.         }
  53. };//class Node

  54. int main()
  55. {
  56.     Node * leftMost = NULL;
  57.     Node * rightMost = NULL;

  58.     string line;
  59.     PRINT_CMD;
  60.    
  61.     while ( getline(std::cin, line))
  62.     {
  63.         if(line.compare("1")==0){
  64.             if( getline(std::cin, line) ){
  65.                 istringstream iss (line,istringstream::in);
  66.                 int val;
  67.                 bool firstTime = true;
  68.                 while(iss >> val){
  69.                     if(firstTime){
  70.                         leftMost = new Node(val);
  71.                         rightMost = leftMost;
  72.                         firstTime= false;
  73.                     }
  74.                     else
  75.                         rightMost = rightMost->append(val);
  76.                 }
  77.             }
  78.         }
  79.         else if(line.compare("2")==0){
  80.             if(leftMost==NULL){
  81.                 PRINT_ERR2;
  82.                 PRINT_CMD;
  83.                 continue;
  84.             }
  85.             cout << "Left to Right :";
  86.             Node::iterate(leftMost);
  87.             cout << "Right to Left:";
  88.             Node::iterate(rightMost);
  89.         }
  90.         else if(line.compare("3")==0) {
  91.             Node::releaseResource(leftMost);
  92.             break;
  93.         }
  94.         else{
  95.             PRINT_ERR1;
  96.         }
  97.         PRINT_CMD;
  98.     }
  99.     return 0;
  100. }
複製代碼


...
瀏覽完整內容,請先 註冊登入會員
回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。

使用道具檢舉

ycess87009 該用戶已被刪除
5
發表於 2012-11-11 06:05 PM|只看該作者
本帖最後由 ycess87009 於 2012-11-11 06:10 PM 編輯

Thank you for your exposition. (^O^)





若對尊貴或贊助會員有任何疑問,歡迎向我們查詢。我們的即時通或MSN: admin@eyny.com

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部