有問題(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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章