[問題] list的插入問題
看板C_and_CPP (C/C++)作者freshair1219 (fresh-air)時間14年前 (2012/04/26 18:48)推噓0(0推 0噓 17→)留言17則, 2人參與討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
dev c++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
程式被強制關閉
餵入的資料(Input):
串列內容
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式被強制關閉
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct event{
float time;
int event_type;
struct event *next;
} *EVENT;
EVENT event;
int generate_event(int ,float);
int main()
{
event = NULL;
generate_event(2,5.78);
generate_event(2,4.78);
generate_event(2,9.78);
generate_event(2,2.78);
printf("%d %f",event->event_type,event->time);
free(event);
system("PAUSE");
return 0;
}
int generate_event(int next_event_type,float next_event_time){
EVENT ptr = (EVENT)malloc(sizeof(EVENT));
ptr->event_type = next_event_type;
ptr->time = next_event_time;
ptr->next = NULL;
if(event == NULL)
event = ptr;
else if(ptr->time <= event->time){
ptr->next = event;
event = ptr;
}
else{
EVENT current = event;
EVENT prev = NULL;
while(ptr->time > current->time){
prev = current;
current = current->next;
}
prev->next = ptr;
ptr->next = current;
}
return 0;
}
補充說明(Supplement):
這是我程式的一部分,我查到這部分有問題,可是不知道哪裡寫錯了,
所以把這部分截出來另外寫一個來檢查....
有修過DS的應該可以直接看懂,
所以就沒有註解了,因為這支程式本身沒有意義,只是檢查用....
順便問一下,為什麼我的dev c++ 無法使用變數監控功能......
請各位高手,先進幫我找一下錯誤,可以話順便教我一下使用變數監控功能
感謝大家QQ,不然每檢查一個function就要重寫一次,好累~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.233.234
※ 編輯: freshair1219 來自: 140.112.233.234 (04/26 19:01)
→
04/26 19:01, , 1F
04/26 19:01, 1F
→
04/26 19:05, , 2F
04/26 19:05, 2F
→
04/26 19:06, , 3F
04/26 19:06, 3F
→
04/26 19:06, , 4F
04/26 19:06, 4F
→
04/26 19:08, , 5F
04/26 19:08, 5F
→
04/26 19:08, , 6F
04/26 19:08, 6F
→
04/26 19:08, , 7F
04/26 19:08, 7F
→
04/26 19:09, , 8F
04/26 19:09, 8F
→
04/26 19:09, , 9F
04/26 19:09, 9F
→
04/26 19:10, , 10F
04/26 19:10, 10F
→
04/26 19:10, , 11F
04/26 19:10, 11F
→
04/26 19:11, , 12F
04/26 19:11, 12F
→
04/26 19:13, , 13F
04/26 19:13, 13F
→
04/26 19:13, , 14F
04/26 19:13, 14F
→
04/26 19:14, , 15F
04/26 19:14, 15F
→
04/26 19:14, , 16F
04/26 19:14, 16F
→
04/26 19:18, , 17F
04/26 19:18, 17F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章