幫忙看看為什么顯示未初始化變數list::head
說P是null
#include<iostream>
using namespace std;
enum myerror_code { success, overflow, underflow, rangeerror };
struct node
{
int data;
node* next;
};
class list
{
public:
list();
myerror_code insert(const int i, const int x);
node* gethead()const;
void show()const;
private:
int count;
node* head;
};
list::list()
{
node* head = new node;
head->next = NULL;
count = 0;
}
myerror_code list::insert(const int i, const int x)
{
node* p = new node;
p = head;
int j = 0;
while (j != i - 1&&p!=NULL )
{
p = p->next;
j++;
}
if (p == NULL)return rangeerror;
node* s = new node;
s->data = x;
s->next = NULL;
s->next = p->next;
p->next = s;
count++;
return success;
}
uj5u.com熱心網友回復:
供參考:#include<iostream>
using namespace std;
enum myerror_code {success, overflow, underflow, rangeerror};
struct node
{
int data;
node* next;
};
class list
{
public:
list();
myerror_code insert(const int i, const int x);
struct node* gethead()const;
void show()const;
private:
int count;
struct node* head;
};
list::list()
{
head = new node;
head->data = -1;
head->next = NULL;
count = 0;
}
myerror_code list::insert(const int i, const int x)
{
struct node* p;
p = head;
if (p == NULL)return rangeerror;
int j = 0;
while(j != i-1 && p->next!=NULL )
{
p = p->next;
j++;
}
struct node* s = new node;
s->data = x;
s->next = NULL;
s->next = p->next;
p->next = s;
count++;
return success;
}
void list::show()const
{
struct node *p;
p = head->next;
while(p){
cout<<p->data<<" count="<< count << endl;
p=p->next;
}
}
int main()
{
list tmp;
tmp.show();
tmp.insert(1,1);
tmp.insert(2,2);
tmp.insert(2,3);
tmp.show();
}
//1 count=3
//3 count=3
//2 count=3
//請按任意鍵繼續. . .
uj5u.com熱心網友回復:
修正 :第24行 ,第40行:head = new struct node;
struct node* s = new struct node;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279006.html
標籤:C++ 語言
下一篇:一道匯編題求解
