[問題] 中正軟體設計第4小題
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
05/04 01:44, 2F