Said P is null
# include
using namespace std;
Enum myerror_code {success, overflow, underflow, rangeerror};
Struct node
{
int data;
Node * next;
};
The class list
{
Public:
The list ();
Myerror_code insert (const int I, const int x);
The node * gethead () const;
Void the show () the const;
Private:
Int count;
Node * head;
};
The list: a list ()
{
The head node *=new node;
The head - & gt; Next=NULL;
count=0;
}
Myerror_code list: : insert (const int I, const int x)
{
Node * p=new node;
P=the head;
int j=0;
While (j!=I - 1 & amp; & p!=NULL)
{
P=p - & gt; Next;
j++;
}
If (p==NULL) return rangeerror;
Node * s=new node;
S - & gt; Data=https://bbs.csdn.net/topics/x;
S - & gt; Next=NULL;
S - & gt; Next=p - & gt; Next;
P - & gt; Next=s;
count++;
The return success.
}
CodePudding user response:
Reference:# include
using namespace std;
Enum myerror_code {success, overflow, underflow, rangeerror};
Struct node
{
The int data;
Node * next;
};
The class list
{
Public:
The list ();
Myerror_code insert (const int I, const int x);
Const struct node * gethead ();
Void the show () the const;
Private:
Int count;
Struct node * head;
};
The list: a list ()
{
The head=new node;
The head - & gt; Data=https://bbs.csdn.net/topics/-1;
The head - & gt; Next=NULL;
count=0;
}
Myerror_code list: : insert (const int I, const int x)
{
Struct node * p;
P=the head;
If (p==NULL) return rangeerror;
int j=0;
While (j!=I - 1 & amp; & P - & gt; Next!=NULL)
{
P=p - & gt; Next;
j++;
}
Struct node * s=new node;
S - & gt; Data=https://bbs.csdn.net/topics/x;
S - & gt; Next=NULL;
S - & gt; Next=p - & gt; Next;
P - & gt; Next=s;
count++;
The return success.
}
Void list: : show () const
{
Struct node * p;
P=the head - & gt; Next;
While (p) {
CoutCodePudding user response:
Fixed: line 24, line 40:head=new struct node;struct node * s=new struct node;