也许是弄复杂了。
#include<iostream>
using namespace std;
struct node //节点的声明
{
int data;
node *next;
};
class ARR //类的声明
{
private:
node * head;
node * count;
int size;
public:
ARR();
void creat();
void show(node * HEAD);
void counter();
void delsame();
node * re_head(){return head;}
node * re_count(){return count;}
};
ARR::ARR() //构造函数
{
head=new node;
size=0;
}
void ARR::creat() //创建链表储存数字串
{
node *p1,*p2;
int mark,item;
int n=1;
cout<<"请输入链表结束标志(仅数字):"<<endl;
cin>>mark;
cout<<"请输入第一个数字:"<<endl;
cin>>head->data;
p1=head;
p2=head;
while(item!=mark)
{
p1=new node;
cout<<"请输入第"<<n<<"个数字"<<endl;
cin>>item;
if(item==mark)
break;
p1->data=item;
p2->next=p1;
p2=p1;
n++;
};
size=n;
p2->next=NULL;
}
void ARR::show(node * HEAD) //输出
{
node *p;
p=HEAD;
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
};
cout<<"长度是"<<size<<endl;
}
void ARR::counter()
{
node *p;
node *p1,*p2,*count;
int symble;
int m=1;
count=new node;
p1=count;
p2=count;
p=head;
symble=p->data;
while((p->next)!=NULL)
{
p=p->next;
if(symble==p->data)
{
m=m+1;
}
else
{
p1->data=m;
p2=new node;
p1->next=p2;
p1=p2;
symble=p->data;
m=1;
};
};
p2=NULL;
}
void ARR::delsame()
{
int i;
node *p1,*p2;
p1=head;
p2=head->next;
for(i=0;i<size-1;i++)
{
if(p1->data==p2->data)
{
p2=p2->next;
p1->next=p2;
size--;
i--;
}
else
{
p1=p1->next;
p2=p2->next;
}
};
}
int main ()
{
ARR x;
node *a,*b;
a=x.re_head();
b=x.re_count();
x.creat();
x.counter();
x.show(a);
x.show(b);
x.delsame();
x.show(a);
return 0;
}