大家帮忙看看,怎么编译不了
#include<iostream>
using namespace std;
class Node
{
public:
int mydata;
Node *next;
Node()
{
next=NULL;
}
Node(int data,Node *next1)
{
mydata=data;
next=next1;
}
};
class SingleLinkedList
{
public:
Node *head,*s,*p;
SingleLinkedList();
SingleLinkedList(int a[],int n);
~SingleLinkedList();
int Length();
int Get(int i);
int Locate(int x);
void Insert(int i,int x);
int Delete(int i);
void PrintList();
};
SingleLinkedList::SingleLinkedList()
{
head=new Node;
head->next=NULL;
}
SingleLinkedList::SingleLinkedList(int a[],int n)
{
head=new Node;
head->next=NULL;
for(int i=0;i<n;i++)
{
s->mydata=a[i];
s->next=head->next;
head->next=s;
}
}
int SingleLinkedList::Length()
{
int count;
s=head->next;count=0;
while(s!=NULL)
{
s=s->next;
count++;
}
return count;
}
int SingleLinkedList::Get(int i)
{
int count;
s=head->next;count=1;
while(s!=NULL&&count<i)
{
s=s->next;
count++;
}
if(s==NULL)throw"位置";
else return s->mydata;
}
int SingleLinkedList::Locate(int x)
{
int count;
s=head->next;count=1;
while(s!=NULL)
{
if(s->mydata==x) return count;
s=s->next;
count++;
}
return 0;
}
void SingleLinkedList::Insert(int i,int x)
{
s=head;int count=0;
while(s!=NULL&&count<i-1)
{
s=s->next;
count++;
}
if(s==NULL)throw"位置";
else
{
p=new Node;p->mydata=x;
s->next=p->next;p->next=s;
}
}
int SingleLinkedList::Delete(int i)
{
int x;
s=head;int count=0;
while(s!=NULL&&count<i-1)
{
s=s->next;
count++;
}
if(s==NULL||s->next==NULL)throw"位置";
else
{
p=s->next;x=p->mydata;
s->next=p->next;
delete p;
return x;
}
}
void SingleLinkedList::PrintList()
{
s=head->next;
while(s!=NULL)
{
cout<<s->mydata;
s=s->next;
}
}
SingleLinkedList::~SingleLinkedList()
{
while(head!=NULL)
{
s=head;
head=head->next;
delete s;
}
}
void main()
{
int a[4]={1,2,3,4};
SingleLinkedList list(a,4);
cout<<list.Length()<<endl;
}
[解决办法]
把编译错误结果贴上来,直接看代码太费劲
[解决办法]
SingleLinkedList::SingleLinkedList(int a[],int n)
{
head=new Node;
head->next=NULL;
for(int i=0;i<n;i++)
{
s=new Node;//head都new了,s不new一下?
s->mydata=a[i];
s->next=head->next;
head->next=s;
}
}