学C++第一天 面向对象写2杈
来源:互联网 发布:centos 7开机密码忘记 编辑:程序博客网 时间:2024/05/18 06:03
此时此刻没有学到数据结构这块 所以 思想还是停留在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
- 学C++第一天 面向对象写2杈
- 面向对象---第一天
- 面向对象第一天
- java第一天 面向对象
- Java面向对象第一天
- JAVA面向对象第一天
- 学下C#,第一天。
- 小朋友学C++(2):面向过程与面向对象
- C#面向对象第一天总结
- OC第一天——面向对象
- PHP面向对象(OOP)第一天
- java(5)面向对象(第一天)
- 我要学c第一天b
- 用C写面向对象
- Objective - C基础: 第一天 - 4.面向过程和面向对象思想
- 第一天学html写了个注册界面
- 第一天学jsp
- 第一天学PERL
- java如何进行异常处理
- The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemG:Give Me the Number
- 【千里之行,始于足下】游戏服务端开发--开发语言篇
- Shader 学习一:Shader和渲染管线
- 基于ffmpeg的HLS开源服务器搭建配置及开发详解
- 学C++第一天 面向对象写2杈
- iOS开发之block详解
- The 5th Zhejiang Provincial Collegiate Programming Contest------ProblemK:Kinds of Fuwas
- Shader 学习二:Unity shader 的组织形式
- Basic C++ Container classes summary(Array, Vector, List)
- 【IntentService】原理解析
- 解决window平台下JNI生成头文件找不到"xxx"类文件
- The 6th Zhejiang Provincial Collegiate Programming Contest->ProblemA:Second-price Auction
- 固定管线shader编写:基本属性