学C++第一天 面向对象写2杈

此时此刻没有学到数据结构这块 所以 思想还是停留在C  的坑

#include <iostream>

using namespace std;


class data
{
public:
	data(int i):x(i){}
	virtual ~data(){ cout << "data.....\n"; }
	virtual void pint() = 0;
	int getvalue(){ return x; }
	void setvalue(int i){ x = i; }
private:
	int x;
};
class shu:public data
{
public:
	shu(int x):data(x){}
	void pint()
	{
		cout << this->getvalue() <<"  ";
	}
};
class node
{
public:
	node(data *pdata):Nada(pdata),zuo(NULL),you(NULL){}
	node():Nada(NULL),zuo(NULL),you(NULL){}
	~node(){ delete Nada; Nada = NULL; cout << "node......\n"; }
	data*getNatda(){ return Nada; }
	node*getyou(){ return you; }
	void setzuo(node*Lzuo){ zuo = Lzuo; }
	void setyou(node*Lyou){ you = Lyou; }
	void setNada(data*Lnada){ Nada = Lnada; }
     node *getzuo()
	 { 
		    return zuo;
	}
private:
	data*Nada;
	node*zuo;
	node*you;
};
class Stack
{
public:
	Stack() :top(NULL), datas(NULL), Conut(NULL){}
	~Stack(){ 
		if (Conut)
		 delete Conut; }
	Stack*getST(){ return top; }
	void SetSt(Stack*p){ top = p; }
	node*getNode(){ return datas; }
	void SetNode(node*p){ datas = p; }
	  void addConut(){ *(Conut)+=1; }
	  void subConut(){ *(Conut)-=1; }
	  void SetConut(){ 
		  if (Conut==NULL)
		       Conut=new int(0); }
	bool is_emty(){ return *Conut == 0; }
private:
	Stack *top;
	node *datas;
	int *Conut;
};

class List
{
public:
	List() :Ldata(NULL), Ltp(NULL),LConut(NULL){}
	~List(){ 
		if (LConut)
		 delete LConut; }
	List*getLst(){ return Ltp; }
	void SetLnode(node*p){ Ldata = p; }
	node*Getnode(){ return Ldata; }
	bool Lis_emty(){ return *LConut == 0; }
	void SetLst(List*p){ Ltp =p ; }
	void SetLconut()
	{ 
		if (LConut == NULL)
			LConut = new int(0);
	}
	void addLConut(){ *(LConut)+=1; }
	void subLConut(){ *(LConut)-=1; }
private:
	List *Ltp;
	node* Ldata;
    int *LConut;
};

class S
{
public:
	S() :head(NULL), Lbesk(NULL),  besk(NULL), LSt(NULL),onew(NULL){}
	~S()
	{
		if (head)
		{
      		deleteall();
		}
		if (head)
			cout << "还有数据\n";
		if (Lbesk)
		{
			delete Lbesk;
			LSt = Lbesk = NULL;
		}
		if (onew)
		{
			delete onew;
			onew = NULL;
		}
		if (besk)
		{
			delete besk;
			besk = NULL;
		}
	}
	void inst(data *pdata)
	{
		if (head == NULL&&onew == NULL)
		{
			head = new node;
			onew = new int(0);
			head->setNada(pdata);
			*(onew)+=1;
			return;
		}
		int num = pdata->getvalue();
		node *p = head;
	while(num <= p->getNatda()->getvalue())
		{
			if (p->getyou() == NULL)
			{
				node *p1 = new node;
				p1->setNada(pdata);
				p->setyou(p1);
				*(onew) +=1;
				return;
			}
			p = p->getyou();
		}
	while (num > p->getNatda()->getvalue())
	{
		if (p->getzuo() == NULL)
		{
			node *p1 = new node;
			p1->setNada(pdata);
			p->setzuo(p1);
			*(onew) +=1;
			return;
		}
		p = p->getzuo();
	}
	}
	data *find(int num);
	void show_xianxu()
	{
		if (head == NULL)
		{
			return;
		}
		node*p=head;
		while (p != NULL || !besk->is_emty())
		{
			if (p != NULL)
			{
				Push(p);
				p->getNatda()->pint();
				p = p->getyou();
				continue;
			}
			else
			{
				p = besk->getST()->getNode();
				Pop_stack();
				p = p->getzuo();
			}
		}
	}
	void show_houxu()
	{
		if (head == NULL)
			return;
		node*p = head;
		node*p1 = NULL;
		while (p != NULL || !besk->is_emty())
		{
			if (p!=NULL)
			{
				Push(p);
				p = p->getyou();
				continue;
			}
			p = besk->getST()->getNode();
			if (p->getzuo() == NULL || p->getzuo() == p1)
			{
				p1 = p;
				p1->getNatda()->pint();
				Pop_stack();
				p = NULL;
			}
			else
			{
				p = p->getzuo();
			}
		}
	}
	void show_zhongxu()
	{
		if (head == NULL)
			return;
		node *p = head;
		while (p != NULL || !besk->is_emty())
		{
			if (p != NULL)
			{
				Push(p);
				p = p->getyou();
				continue;
			}
			else
			{
				p = besk->getST()->getNode();
				p->getNatda()->pint();
				Pop_stack();
				p = p->getzuo();
			}
		}
	}
	void show_Cenci()
	{
		if (head == NULL )
			return;
		node *p= head;
		Pus_list(p);
		while (p != NULL || !Lbesk->Lis_emty())
		{
			p = Lbesk->getLst()->Getnode();
			p->getNatda()->pint();
			if (p->getyou())
			{
				Pus_list(p->getyou());
			}
			if (p->getzuo())
			{
				Pus_list(p->getzuo());
			}
			Pop_list();
			p = NULL;
		}
	}
	void deleteall()
	{
		if (head == NULL )
			return;
		node *p = head;
		Pus_list(p);
		while (p != NULL || !Lbesk->Lis_emty())
		{
			p = Lbesk->getLst()->Getnode();
			if (p->getyou())
			{
				Pus_list(p->getyou());
			}
			if (p->getzuo())
			{
				Pus_list(p->getzuo());
			}
			delete_Pop();
			p = NULL;
		}
	}
	void Push(node*one)
	{
		if (besk == NULL||!besk->getST())
		{
			if (besk == NULL)
			{
				besk = new Stack;
				besk->SetConut();
			}
			Stack *p = new Stack;
			
			p->SetNode(one);
			besk->SetSt(p);
			besk->addConut();
			return;
		}
		Stack *p = new Stack;
		p->SetNode(one);
		p->SetSt(besk->getST());
		besk->SetSt(p);
		besk->addConut();
		return;
	}
	void Pop_stack()
	{
		if (besk->is_emty())
			return;
		Stack *p = besk->getST();
		besk->SetSt(p->getST());
		besk->subConut();
		delete p;
		p = NULL;
	}
	void Pus_list(node*one)
	{
		if (Lbesk == NULL||!Lbesk->getLst())
		{
			if (Lbesk == NULL)
			{
				Lbesk = new List;
				Lbesk->SetLconut();
			}
			List *p = new List;
			p->SetLnode(one);
			Lbesk->SetLst(p);
			Lbesk->addLConut();
			LSt = p;
		
			return;
		}
		List *p = new List;
		p->SetLnode(one);
		LSt->SetLst(p);
		LSt = p;
		Lbesk->addLConut();
		return;
	}
	void Pop_list()
	{
		if (Lbesk->Lis_emty())
			return;
		List *p = Lbesk->getLst();
		 Lbesk->SetLst(p->getLst());
		 Lbesk->subLConut();
		delete p;
		if (Lbesk->Lis_emty())
			LSt = NULL;
	}
	void delete_Pop()
	{
		if (Lbesk->Lis_emty())
			return;
		if (!Lbesk->getLst()->getLst())
		{
			delete Lbesk->getLst()->Getnode();
			Lbesk->getLst()->SetLnode(NULL);
			delete Lbesk->getLst();
			Lbesk->SetLst(NULL);
			LSt = NULL;
			Lbesk->subLConut();
			if (Lbesk->Lis_emty())
				head = NULL;
			return;
		}
		delete Lbesk->getLst()->Getnode();
		Lbesk->getLst()->SetLnode(NULL);
		List *p = Lbesk->getLst();
		Lbesk->SetLst(p->getLst());
		delete p;
		Lbesk->subLConut();
		if (Lbesk->Lis_emty())
			head = NULL;
		return;
	}

private:
	int *onew;
	Stack *besk;
	node*head;
	List *Lbesk;
	List *LSt;
};
class re
{
public:
	void inst(data *pdata)
	{
		l.inst(pdata);
	}
	void Run();
private:
	S l;

};
void re::Run()
{
	
	data *pdata = NULL;
	int num;
	int i = 0;
	while (i != 7)
	{
		fflush(stdin);
		cout << "\n输入2茶树节点数值:";
		cin >> num;
		pdata = new shu(num);
		l.inst(pdata);
		cout << i++ << endl;
		if (i % 7 == 0)
		{
			cout << "\n先序遍历\n";
			l.show_xianxu();
			cout << "\n中序遍历\n";
			l.show_zhongxu();
			cout << "\n后续遍历\n";
			l.show_houxu();
			cout << "\n层次遍历\n";
			l.show_Cenci();
		}
	}
}
void main()
{
	re r;
	r.Run();
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值