作者sunneo (艾斯寇德)
看板Grad-ProbAsk
标题[问题] 中正软体设计第4小题
时间Mon May 4 01:29:43 2009
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
1F:→ ssccg:第二个解法是对的啊 05/04 01:38
2F:→ sunneo:原来如此@_@ 谢谢 我以为insert front连位址都要变动 05/04 01:44