大家帮忙看看,怎么编译不了

qingqing_9020 2013-11-27 04:31:35
#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;

}







...全文
85 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Adol1111 2013-11-27
  • 打赏
  • 举报
回复
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;
	}	

}
qingqing_9020 2013-11-27
  • 打赏
  • 举报
回复
结果就是停止工作,按理说应该输出数组的长度才对啊
qingqing_9020 2013-11-27
  • 打赏
  • 举报
回复
结果就是停止工作,按理说应该输出数组的长度才对啊
懒懒的吉他手 2013-11-27
  • 打赏
  • 举报
回复
把编译错误结果贴上来,直接看代码太费劲

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧