const int size=100;
class Node
{
public:
int data;
Node *next;
Node operator=(Node n);
Node operator<(Node n);
};
int ListLength(Node *p)
{
int len=0;
while(p->next!=NULL)
{
len++;
p=p->next;
}
return len;
}
Node* LocList(Node *p,int data)
{
Node *q;
q=p;
int j=0;
while(j<data)
{
q=q->next;
j++;
}
return (q);
}
int Node::operator= (Node n)
{
}
void Insert(Node number,Node *opt)
{
Node *q=new Node;
Node *cp,*ap,*t;
cp=NULL;
t=ap=opt;
q->data=number;
if(ap==NULL)
{
ap->next=q;
q->next=NULL;
}
else
while(cp!=NULL)
if(number<cp->data)break;
else
{
ap=cp;//ap永远指向的是cp的前个地址
cp=cp->next;
}
number->next=cp;
ap->next=number;
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
}
void main()
{
/*Node x,y,z;//静态建立三个节点
Node *p;
p=&x;
cout<<"输入x的data:";
cin>>x.data;
cout<<"输入y的data:";
cin>>y.data;
cout<<"输入z的data:";
cin>>z.data;
x.next=&y;
y.next=&z;
z.next=NULL;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;*/
int n;
Node *p,*q,*l,*o;//动态建立三个节点
Node node;
o=p=l=new Node;
for(int i=0;i<10;i++)
{
q=new Node;
cout<<"请输入data:";
cin>>q->data;
p->next=q;
p=q;//此时p指针指向q
}
p->next=NULL;
cout<<"当前链表的长度是:"<<ListLength(l)<<endl;
cout<<"请输入定位的数值:";
cin>>n;
cout<<LocList(l,n)<<endl;
p=l->next;//p是第一个元素节点,l是头节点
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
Insert(node,o);
}
我用了很多方法,但是编译都说是错的,只好请教大家.在这个程序里,运算符怎么重载啊?