这程序到底哪里错了,编译不通过
#include <iostream>using namespace std;
template <class T> class node
{
public:
node():id(0),next(NULL){}
T id;
node *next;
};
class link
{
public:
link():m_head(NULL),m_count(0){}
int insert(int id,int pos);//向pos位置前添加结点,pos不合法返回-1,否则0
void print_all();//遍历链表并打印元素
private:
node *m_head;//链表头指针
int m_count;//结点个数
};
int link::insert(int id,int pos)
{
node *s,*ss,*news;
int num;
news= new node;
news->id=id;
if((m_head==NULL)&&(pos==0))
{
news->next=NULL;
m_head=news;
return 0;
}
for(s=ss=m_head,num=1;(s!=NULL)&&(num<=pos);ss=s,s=s->next,num++)
if(s==NULL)
{
printf("i value exceed\n");
return -1;
}
if(ss!=s)
{
news->next = s;
ss->next=news;
}
else
{
news->next=s;
m_head=news;
}
return 0;
}
void link::print_all()
{
node *s;
for(s=m_head;s!=NULL;s=s->next)
{
printf("->%d\n",s->id);
}
}
int main()
{
link obj;
for(int i=0;i<5;i++)
{
obj.insert(i,1);
}
return 0;
}