[問題] 中正軟體設計第4小題

看板Grad-ProbAsk作者 (艾斯寇德)時間15年前 (2009/05/04 01:29), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
http://www.cs.ccu.edu.tw/recruit/MasterExam/98software.pdf 依照題意 每個node都是 typedef struct Lnode{ int value; struct Lnode* next; }Lnode; 但insert front... ? 函式說明該函式必須insert in the front of linked list insert front一般解法需要有前一個指標 我當時有想到解法,但覺得那可能不是題目的意思,所以只好寫insert back int insert(struct Lnode* LL,int x){ struct Lnode* ptr; ptr = (struct Lnode*)malloc(sizeof(struct Lnode) ); if(!ptr) return 0; /* bad allocation */ ptr->value = x; ptr->next = LL->next; LL->next = ptr; return 1; } 我的另一個解符合題意,但不是很好看 int insert(struct Lnode* LL,int x){ struct Lnode* ptr; ptr = (struct Lnode*)malloc(sizeof(struct Lnode) ); if(!ptr) return 0; /* bad allocation */ ptr->value = LL->value; /* 將ptr塞到LL的next並且成為LL的內容 */ ptr->next = LL->next; LL->next = ptr; /* 取代LL為新值 */ LL->value = x; return 1; } 有人有另一種覺得可行的做法嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.227.126.125

05/04 01:38, , 1F
第二個解法是對的啊
05/04 01:38, 1F

05/04 01:44, , 2F
原來如此@_@ 謝謝 我以為insert front連位址都要變動
05/04 01:44, 2F
文章代碼(AID): #19_TIAJI (Grad-ProbAsk)