看板 Grad-ProbAsk 關於我們 聯絡資訊
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
ssccg:第二個解法是對的啊 05/04 01:38
sunneo:原來如此@_@ 謝謝 我以為insert front連位址都要變動 05/04 01:44