[問題] 鏈結串列的問題

看板C_and_CPP作者 (香菇)時間13年前 (2011/05/12 11:57), 編輯推噓0(007)
留言7則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 問題(Question): #include <cstdlib> #include <iostream> using namespace std; struct node{ int data; struct node *next; }; struct node *head; struct node *tail; ==>先以全域變數宣告結構體指標 head和tail void insert(struct node *x,struct node *head,struct node *tail){ int num; cout<<"輸入的數字為?"<<endl; cin>>num; if(head==NULL){ x->data=num; x->next=NULL; head=x; tail=x; cout<<head->data<<endl; } ===>insert函式用來新增第一個node,並把head和 tail設定指向那個新增的點 } int main(int argc, char *argv[]) { struct node *x; head=NULL; tail=NULL; x=(struct node *)malloc(sizeof(struct node)); insert( x, head,tail); ===>使用傳址呼叫的方式,把新new出來的x節點跟head,tail 一併傳過去 cout<<head->data<<endl;===>問題來了,在insert函式裡頭的cout<<head->data可以 產生我希望的結果,但執行完函式回到mail裡頭執行 cout<<head->data時,跑出"發生未處理的win32例外狀況" 請問為什麼會這樣?? system("PAUSE"); return EXIT_SUCCESS; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.123.102

05/12 11:59, , 1F
程式碼用置底文連結貼,並且排整齊
05/12 11:59, 1F

05/12 12:11, , 2F

05/12 12:21, , 3F
因為你修改的 head、tail是參數, 不是全域變數
05/12 12:21, 3F

05/12 12:21, , 4F
好沒誠意的程式碼...
05/12 12:21, 4F

05/12 13:08, , 5F
與置底的13誡之13類似...
05/12 13:08, 5F

05/12 13:26, , 6F
http://pastie.org/1891828 這樣就可以了
05/12 13:26, 6F

05/12 16:08, , 7F
05/12 16:08, 7F
文章代碼(AID): #1Dorgm1v (C_and_CPP)