作者fei6409 (fei6409)
看板b99902HW
标题[分享] 单班计程11.2 测试用main
时间Tue Nov 30 20:20:33 2010
有监於这题不少人卡在莫名其妙的bug(?)
就放个简单的测试在上面罗~
然後小小的建议,做完malloc後最好对拿到的记忆体初始化一下,
比较不容易出错这样,
像是讲义上的 assert(); 就是为了避免没初始化的情形发生~
#include<stdio.h>
#include<stdlib.h>
struct listnode{
int data;
struct listnode *next;
};
/*
put your code here.
*/
void print(struct listnode *now){
while(now!=NULL){
printf("%d ", now->data);
now=now->next;
}
puts("");
return;
}
int main(){
int i;
struct listnode *list1, *list2, *ptr, *now;
list1=(struct listnode*)malloc(sizeof(struct listnode));
list2=(struct listnode*)malloc(sizeof(struct listnode));
for(i=0, now=list1; i<10; i+=2){
ptr=(struct listnode*)malloc(sizeof(struct listnode));
ptr->next=NULL;
ptr->data=i;
now->next=ptr;
now=now->next;
}
list1=list1->next;
printf("now print the content of list1\n");
print(list1);
for(i=1, now=list2; i<10; i+=2){
ptr=(struct listnode*)malloc(sizeof(struct listnode));
ptr->next=NULL;
ptr->data=i;
now->next=ptr;
now=now->next;
}
for( ; i<=15; i++){
ptr=(struct listnode*)malloc(sizeof(struct listnode));
ptr->next=NULL;
ptr->data=i;
now->next=ptr;
now=now->next;
}
list2=list2->next;
printf("now print the content of list2\n");
print(list2);
now=merge_linked_lists(list1, list2);
printf("now print the content of your list\n");
print(now);
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.91.122
1F:推 m80126colin:推真强者 11/30 20:59
2F:推 wctaiwan:感谢,今天弄超久的 11/30 23:26
3F:推 rod24574575:然後11.2传上去时好像要自己附一下标头档 11/30 23:31
4F:→ rod24574575:我一开始没附结果他跟我说NULL不能用... 11/30 23:31
5F:→ fei6409:应该是11.3? 我是传这题才有用标头档... 12/01 00:01
6F:推 m80126colin:我也是有用到标头档.......如果NULL写成0就不用?? 12/01 00:20
7F:推 rod24574575:喔 我有点忘记是哪个了 反正都附就不会错 (? 12/01 00:33
8F:推 garychou:虽然我写好了 还是推一下罗 第三题的确要传标头档 12/01 22:58
9F:推 sandy30716:第三题有测试用的吗? 12/01 23:17