[分享] 單班計程11.2 測試用main
有鑑於這題不少人卡在莫名其妙的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
推
11/30 20:59, , 1F
11/30 20:59, 1F
推
11/30 23:26, , 2F
11/30 23:26, 2F
推
11/30 23:31, , 3F
11/30 23:31, 3F
→
11/30 23:31, , 4F
11/30 23:31, 4F
→
12/01 00:01, , 5F
12/01 00:01, 5F
推
12/01 00:20, , 6F
12/01 00:20, 6F
推
12/01 00:33, , 7F
12/01 00:33, 7F
推
12/01 22:58, , 8F
12/01 22:58, 8F
推
12/01 23:17, , 9F
12/01 23:17, 9F