c++一些基本算法代码

c++一些基本算法代码

ID:10544667

大小:31.55 KB

页数:27页

时间:2018-07-07

上传者:U-3183
c++一些基本算法代码_第1页
c++一些基本算法代码_第2页
c++一些基本算法代码_第3页
c++一些基本算法代码_第4页
c++一些基本算法代码_第5页
资源描述:

《c++一些基本算法代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

272010年11月29号开始实习#includeusingnamespacestd;structnode{intdata;node*next;};voidmain(){node*p1,*p2,*p3,*p4,*p5,*p;p1=newnode();p2=newnode();p3=newnode();p4=newnode();p5=newnode();p1->data=1;p2->data=2;p3->data=3;p4->data=4;p5->data=5;p1->next=p2;p2->next=p3;p3->next=p4;p4->next=p5;p5->next=NULL;cout<<"p1->data:"<data<data:"<data<data:"<data<data:"<data<data:"<data<data:"<data<next->data:"<next->data<next->next->data:"<next->next->data<#include#include#include#defineEQ(a,b)((a)==(b))#defineLT(a,b)((a)<(b))#defineLQ(a,b)((a)>(b))#defineLH+1#defineEH0#defineRH-1typedefintKeyType;typedefstruct{intkey;//关键字域}ElemType;typedefstructBSTNode{ElemTypedata;intbf;//结点的平衡因子structBSTNode*lchild,*rchild;//左右孩子指针}BSTNode,*BSTree;voidInitBSTree(BSTree&T){T=NULL;}voidR_Rotate(BSTree&p){BSTNode*lc;lc=p->lchild;//lc指向的*p的左子树根结点p->lchild=lc->rchild;//lc的右子树挂接为*p的左子树lc->rchild=p;p=lc;//p指新的得根结点}//R_Rotate 27voidL_Rotate(BSTree&p){BSTNode*rc;rc=p->rchild;//lc指向的*p的右子树根结点p->rchild=rc->lchild;//lc的左子树挂接为*p的右子树rc->lchild=p;p=rc;//p指新的根结点}//L_RotatevoidLeftBalance(BSTree&T){BSTNode*lc,*rd;lc=T->lchild;switch(lc->bf){caseLH:T->bf=lc->bf=EH;R_Rotate(T);break;caseRH:rd=lc->rchild;switch(rd->bf){caseLH:T->bf=RH;lc->bf=EH;break;caseEH:T->bf=lc->bf=EH;break;caseRH:T->bf=EH;lc->bf=LH;break;}//switch(rd->bf)rd->bf=EH;L_Rotate(T->lchild);R_Rotate(T);}//switch(lc->bf)}//LeftBalancevoidRightBalance(BSTree&T){BSTNode*lc,*ld;lc=T->rchild;switch(lc->bf) 27{caseLH:ld=lc->lchild;switch(ld->bf){caseLH:T->bf=EH;lc->bf=RH;break;caseEH:T->bf=lc->bf=EH;break;caseRH:T->bf=LH;lc->bf=EH;break;}//switch(rd->bf)ld->bf=EH;R_Rotate(T->rchild);L_Rotate(T);caseRH:T->bf=lc->bf=EH;L_Rotate(T);break;}//switch(lc->bf)}//RightBalanceintInsertAVL(BSTree&T,ElemTypee,bool&taller){if(!T){T=(BSTree)malloc(sizeof(BSTNode));T->data=e;T->lchild=T->rchild=NULL;T->bf=EH;taller=true;}else{if(EQ(e.key,T->data.key)){taller=false;cout<<"Thekey"<data.key)){if(!InsertAVL(T->lchild,e,taller)){//cout<<"未插入。"<bf){caseLH:LeftBalance(T);taller=false;break;caseEH:T->bf=LH;taller=true;break;caseRH:T->bf=EH;taller=false;break;}//switch(T->bf)}//ifelse{if(!InsertAVL(T->rchild,e,taller)){//cout<<"未插入。"<bf){caseLH:T->bf=EH;taller=false;break;caseEH:T->bf=RH;taller=true;break;caseRH:RightBalance(T); 27taller=false;break;}//switch(T->bf)}//else}//elsereturn1;}//InsertAVLvoidPrint_BSTTree(BSTreeT,inti){//i表示结点所在的层次,初始应该为0if(T){if(T->rchild)Print_BSTTree(T->rchild,i+1);for(intj=1;j<=i;j++)cout<<"";cout<data.key<lchild)Print_BSTTree(T->lchild,i+1);}}intmain(){BSTreeT;ElemTypee;InitBSTree(T);booltall=false;boolchoice=true;chary;while(choice){cout<<"Inputtheinsert_key:";cin>>e.key;InsertAVL(T,e,tall);Print_BSTTree(T,0);cout<<"Continue?(y/n)";cin>>y;if(y=='Y'||y=='y')choice=true;elsechoice=false;} 27return0;}文档三(引用的举例)#includeusingnamespacestd;voidswap(int&a,int&b){inttemp;temp=a;a=b;b=temp;}voidswap2(inta,intb){inttemp;temp=a;a=b;b=temp;}voidmain(){inti=9;intj=12;swap2(i,j);cout<<"i="<usingnamespacestd;#definemaxsize20 27classstack{public:stack(){top=-1;}intGetTop(){returntop;}voidInput(intx){if(top==maxsize-1){throw"stackisfull";}data[++top]=x;}//input()boolOutput(){if(top==-1){throw"stackisNull";returnfalse;}else{cout<usingnamespacestd;structnode{//用于保存节点的数据信息intdata;//数据node*next;//用于保存下一个节点的信息};classLianStack{//声明一个类public:LianStack()//构造函数{top=NULL;//初始化节点为空,说明该栈是空栈}voidInPutStack(intx)//入栈操作{node*s;//生成一个节点,这个节点的下一个指针指向前一个节点。s=newnode();s->data=x;s->next=top; 27top=s;}voidOutPut()//出栈函数{if(top==NULL){cout<<"stackisemputy!"<data<next;deletep;}}private:node*top;//定义一个头指针};voidmain(){LianStacklianstack;inta;charselect;do//入栈{cout<<"inputdata:";cin>>a;lianstack.InPutStack(a);cout<<"doyouwanttoinputcontinue(y/n:";cin>>select;}//出栈while(select=='y');cout<>output;}cout<usingnamespacestd;#definemaxsize10//队列的长度classQueue{public:Queue()//构造函数{front=rear=0;//初始时队列是一个空的队列}intGetRear()//用于获取指向队尾的变量{returnrear;}//GetRear()intGetFront()//获取队前序号{returnfront;}//intGetFront()voidEnqueue(intx)//入队操作{if((rear+1)%maxsize==front)//如果队满时输出队满{cout<<"queueisfull"<>data;queue.Enqueue(data);}//出队for(intj=0;j<10;j++){queue.DeleteQueue();}}队列的链式存储结构#include 27usingnamespacestd;structnode{//节点的定义intdata;node*next;};classQueue{public:Queue(){//构造函数,用于设置头指针与尾指针为空node*s;s=newnode();s->next=NULL;front=rear=s;}//Queue()node*GetFront()//用于获取指向头部的指针{returnfront;}voidEnqueue(intx)//入队操作函数,x为要入队的数据。{node*s;//生成一个节点,将x的值保存到里面,然后使rear指向该节点。s=newnode();s->data=x;s->next=NULL;rear->next=s;rear=s;}//Enqueue()voidDeleQueue()//出队操作,用于将头指针指向的下一个节点出队。{if(rear==front){//队列为空的情况cout<<"thisqueueisemputy!"<next==rear){//队列长度为1的情况。cout<data<<""<next;cout<data;}}cout<>data;//输入要入队的数据queue.Enqueue(data);//调用入队函数}//出队操作for(intj=0;j<=10;j++){//队长为十queue.DeleQueue();//调用出队函数操作}}图的顺序存储#includeusingnamespacestd;#definemaxsize30intvisited[maxsize];classGraph{public:Graph(){num_sides=0;num_dot=0;for(inti=0;i>dot[num_dot++];}//InitGraph();voidInitSides()//初始化边的信息{for(inti=0;i>num_sides;intm,n;for(inta=0;a>m;cout<>n;sides[m][n]=1;sides[n][m]=1;}}//InitSides();voidPrintGraphdot()//输出接点数据{for(inti=0;i>n;for(inti=0;iusingnamespacestd;structNode{//节点结构体intdata;Node*next;};classLianBiao{ 27public:LianBiao(){//构造函数root=newNode();root->next=NULL;}Node*GetRoot(){returnroot;}//GetRoot()voidInitLianBiao(intx)//插入节点{Node*s;s=newNode();s->data=x;s->next=root->next;root->next=s;}voidScanLianBiao(Node*root)//输出所有的节点{Node*p;p=root->next;while(p){cout<data<<"";p=p->next;}//while()}//ScanLianBiao()voidFindByseat(intx)//查找第x个节点{Node*p;p=root->next;intj=1;while(p&&jnext;j++;}if(!p){cout<<"查找失败,没有你要查找的位置"<data<next;intj=1;while(p&&p->data!=x){p=p->next;j++;}if(!p){cout<<"没有你要查找的数据节点!"<data=x;s->next=root->next;root->next=s;}//AddNode()voidDeleNodeBySeat(intx)//通过位置删除节点{Node*p;p=root->next;Node*q;intj=1;while(p&&jnext;j++;}//while()if(p->next){q=p->next;p->next=q->next;cout<<"你所要删除第"<data<next;q=p->next;if(!p){cout<<"该链表为空"<data==x){cout<<"删除了节点:"<data;deletep;return;}else{while(q&&q->data!=x){p=q;q=q->next;}if(q){cout<<"删除了节点:"<data<next=q->next;deleteq;}elsecout<<"没有你要删除的节点数据!"<usingnamespacestd;#definemaxsize20intvisted[maxsize];structSidesNode{//定义边表结构体public: 27intnum;SidesNode*next;};//SidesNode()structDotNode{//顶点表结构体public:intshuju;SidesNode*first;};//DotNode()classGraph{public:Graph(){//构造函数numofdot=0;numofsides=0;for(inti=0;i>numofdot>>numofsides;for(inti=0;i>m;data[i].shuju=m;data[i].first=NULL;}//for()cout<<"节点数据输入完毕!"<>v1;cout<>v2;s=newSidesNode();s->num=v2;s->next=data[v1].first;data[v1].first=s;//将节点添加到表头的头结点处}//for(intj=0;j>node_data;data[numofdot].shuju=node_data;data[numofdot].first=NULL;numofdot++;cout<<"请输入增加的边的数量:";cin>>cout_sides;for(inti=0;i>v1;cout<>v2;s=newSidesNode();s->num=v2;s->next=data[v1].first;data[v1].first=s;}//for()}//AddNodeAndSides()voidDeleteSides()//删除边函数{inti,j;cout<<"请输入要删除的边的起点:";cin>>i;cout<<"终点:"<>j;SidesNode*p,*q;p=data[i].first; 27q=p->next;if(p==NULL){cout<<"你输入的边不存在!"<num==j){data[i].first=p->next;deletep;return;}//ifelse{while(q&&q->num!=j){p=q;q=q->next;}//while()if(q){cout<<"删除边成功!"<next=q->next;deleteq;}elsecout<<"你输入的边部存在!"<next;if(p->num==x){data[j].first=p->next;deletep;continue;}//if(p->num==x)else{while(q&&q->num!=x){p=q;q=q->next;}//while()if(q){p->next=q->next;deleteq;continue;}//if()continue;}//else}}continue;}//for(j)}//DeleteNode() 27voidPrintGraph()//输出所有点的邻接点。{intj;SidesNode*p;p=newSidesNode();for(inti=0;inum;cout<next;}//while(!p)cout<num;if(visted[j]==0)ScanShengdu(j);p=p->next;}//while()}//ScanShengdu()private:DotNodedata[maxsize];intnumofdot;intnumofsides;};voidPrintMenu() 27{cout<<"********1.addnodeandsides********"<>select;switch(select){case1:{gh.AddNodeAndSides();//增加节点数据break;}case2:{gh.PrintGraph();//输出所有点的邻接点用以验证是否插入成功break;}case3:{intdm;cout<<"pleaseinputthenode'svalue:";cin>>dm;gh.DeleteNode(dm);break;}case4:{gh.ScanShengdu(0);//深度优先遍历图break;} 27case5:{gh.DeleteSides();break;}default:break;}gotoloop;}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
关闭