谁能帮我调试一下这程序啊
题目:根据一个结点数据类型为整型的单链表生成两个单链表,使得第一个单链表中包含原单链表中所有数值为奇数的结点,使得第二个单链表中包含原单链表中所有数值为偶数的结点,原单链表保持不变。
程序代码:
#include<iostream>
using namespace std;
template<class Elem>
class List{
public:
virtual bool insert(const Elem&)=0;
virtual void setstart()=0;
virtual void setend()=0;
virtual int leftlength()const=0;
virtual int rightlength()const=0;
virtual void print()const=0;
virtual bool setpos(int pos)=0;
virtual bool getvalue(Elem&)const=0;
virtual void clear()=0;
};
template<class Elem>
class Link
{
public:
Elem element;
Link *next;
Link(const Elem&elemval,Link*nextval=NULL)
{
element=elemval;
next=nextval;
}
Link(Link *nextval=NULL)
{
next=nextval;
}
};
template<class Elem>
class LList : public List<Elem>
{
private:
Link<Elem>*head;
Link<Elem>*tail;
Link<Elem>*fence;
int leftcnt;
int rightcnt;
void init()
{
fence=tail=head=new Link<Elem>;
leftcnt=rightcnt;
}
void removeall()
{
while(head!=NULL){
fence=head;
head=head->next;
delete fence;
}
}
public:
LList(int size=DefaultListSize){init();}
virtual ~LList(){removeall();}
void clear() { removeall(); init(); }
bool insert(const Elem&item);
void setstart()
{
fence=head;
rightcnt+=leftcnt;
leftcnt=0;
}
void setend()
{
fence=tail;
leftcnt+=rightcnt;
rightcnt=0;
}
void next()
{
if(fence!=tail)
{
fence=fence->next;
rightcnt--;
leftcnt++;
}
}
int leftlength()const{return leftcnt;}
int rightlength()const{return rightcnt;}
bool getvalue(Elem&it)const{
if(rightlength()==0)return false;
it=fence->next->element;
return true;
}
void print()const;
bool setpos(int pos)
{
if((pos<0)||(pos>rightcnt+leftcnt))
return false;
fence=head;
for(int i=0;i<pos;i++)
fence=fence->next;
return true;
}
};
template<class Elem>
bool LList<Elem>::insert(const Elem&item){
fence->next=new Link<Elem>(item,fence->next);
if(tail==fence)
tail=fence->next;
rightcnt++;
return true;
}
template<class Elem>
void LList<Elem>::print()const
{
Link<Elem>*temp=head;
cout<<"<";
while(temp!=fence)
{
cout<<temp->next->element<<" ";
temp=temp->next;
}
while(temp->next!=NULL)
{
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<">\n";
}
int main()
{
LList<int>a(18),b(18),c(18);
for(int i=0;i<=17;i++)
a.insert(i);
cout<<"输入的线性表为"<<endl;
a.print();
cout<<endl;
int it,j=1;
for(a.setstart();a.getvalue(it);a.next())
{
if(j%2==0)
b.insert(it);
else
c.insert(it);
j++;
}
cout<<"第一条偶数链表"<<endl;
b.print();
cout<<endl;
cout<<"第二条奇数链表为"<<endl;
c.print();
cout<<endl;
return 0;
}