有问题(counter函数)

  也许是弄复杂了。
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值