[問題] 鏈結串列的問題
開發平台(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:11, 2F
→
05/12 12:21, , 3F
05/12 12:21, 3F
→
05/12 12:21, , 4F
05/12 12:21, 4F
→
05/12 13:08, , 5F
05/12 13:08, 5F
→
05/12 13:26, , 6F
05/12 13:26, 6F
→
05/12 16:08, , 7F
05/12 16:08, 7F