摘要:数据结构阶段二复习阶段二只考编程题目,所以进行常见带代码整理考串到图的遍历,由于是写代码,所以我尽量精简一些由于个人习惯,可能和代码会混合起来,所以编写的时候,请写文件,并使用的编译方式。
阶段二只考编程题目,所以进行常见带代码整理
考串到图的遍历,由于是写代码,所以我尽量精简一些
由于个人习惯,可能C和C++代码会混合起来,所以编写的时候,请写.cpp文件,并使用C++的编译方式。
因为这个玩意写的比较早后面老师又更新了范围之类的,所以你们挑着看吧,就代码模块而言这篇文档应该足够了。
如果按老师说的加难度,那我觉得应该是算法和思维方面加难度吧。
另外STL很爽,可以学一下。但是考试尽量别用吧,下面代码为了简化可能直接使用STL,你们可以自己手打栈和队列
机房主要是codeblock和dev c++,所以我以这两个举例
明天去机房更新(如果我空的话)
#include using namespace std;const int MAXSIZE=105;typedef struct { int data[MAXSIZE]; int top;}SeqStack;void Init(SeqStack &S){S.top=-1;}bool Empty(SeqStack S){return S.top==-1;}void Push(SeqStack &S,int x){S.data[++S.top]=x;}int Pop(SeqStack &S){return S.data[S.top--];}int Top(SeqStack S){return S.data[S.top];}int main(){ SeqStack S; Init(S); Push(S,1);Push(S,2); cout<<"栈顶:"<<Top(S)<<endl; cout<<"栈顶出队:"<<Pop(S)<<endl; Pop(S); if(Empty(S))printf("Stack Empty/n"); else printf("Stack not Empty/n"); return 0;}
#include using namespace std;typedef struct StackNode{ int data; struct StackNode *next;}StackNode,*LinkStack;void Init(LinkStack &top){top=NULL;}bool Empty(LinkStack top){return top==NULL;}int Top(LinkStack top){return top->data;}void Push(LinkStack &top,int x){ StackNode *s; s=(StackNode*)malloc(sizeof(StackNode)); s->data=x;s->next=top; top=s;}int Pop(LinkStack &top){ int ret=top->data; StackNode *p=top; top=top->next;free(p); return ret;}int main(){ int x; LinkStack Stk; Init(Stk); Push(Stk,1);Push(Stk,2); cout<<"栈顶:"<<Top(Stk)<<endl; cout<<"栈顶出队:"<<Pop(Stk)<<endl; Pop(Stk); if(Empty(Stk))printf("Stack Empty/n"); else printf("Stack not Empty/n"); return 0;}
#include using namespace std;const int MAXSIZE=105;typedef struct{ int data[MAXSIZE]; int front,rear;}SeQueue;void Init(SeQueue &Q){Q.front=Q.rear=0;}int Head(SeQueue Q){return Q.data[Q.front];}bool Empty(SeQueue Q){return Q.front==Q.rear;}void Push(SeQueue &Q,int x){ Q.data[Q.rear]=x; Q.rear=(Q.rear+1)%MAXSIZE;}int Pop(SeQueue &Q){ int ret=Q.data[Q.front]; Q.front=(Q.front+1)%MAXSIZE; return ret;}int main(){ SeQueue Q; Init(Q); Push(Q,1);Push(Q,2); cout<<Head(Q)<<endl; if(Empty(Q))printf("Queue Empty!/n"); else printf("Queue not Empty!/n"); Pop(Q); cout<<Head(Q)<<endl; return 0;}
#include using namespace std;typedef struct Node{ int data; struct Node *next;}LQNode,*LinkQNode;typedef struct{ LQNode *front,*rear;}LQueue,*LinkQueue;void Init(LinkQueue &Q){ LinkQNode p; Q=(LinkQueue)malloc(sizeof(LQueue));//申请头尾指针节点 p=(LinkQNode)malloc(sizeof(LQNode));//申请链队头节点 p->next=NULL;Q->front=Q->rear=p;}void Push(LinkQueue &Q,int x){ LinkQNode p=(LinkQNode)malloc(sizeof(LQNode)); p->data=x;p->next=NULL; Q->rear->next=p;Q->rear=p;}int Pop(LinkQueue &Q){ int ret=0; LinkQNode p=Q->front->next; Q->front->next=p->next;ret=p->data; if(p==Q->rear)Q->rear=Q->front;//只有一个元素的话,出队后为空 free(p); return ret;}int Head(LinkQueue Q){return Q->front->next->data;}bool Empty(LinkQueue Q){return(Q->front==Q->rear);}int main(){ LinkQueue Q; Init(Q); Push(Q,1);Push(Q,2); cout<<Head(Q)<<endl; if(Empty(Q))printf("Queue Empty!/n"); else printf("Queue not Empty!/n"); Pop(Q); cout<<Head(Q)<<endl; return 0;}
貌似没啥好写的,应该不会很难,感兴趣的话可以看看C++ STL的string.
如果搞你的话应该是不让你用string.h里的库函数的
typedef struct BinNode{ char data; //可以多维护一些信息,深度啊,度数啊之类的,可以直接建树的时候维护掉 struct BinNode *left; struct BinNode *right;}BinNode,*BinTree;void CreateBinTree(BinTree &T){ char ch;cin>>ch; if(ch=="@")T=NULL; else{ T=new BinNode; T->data=ch; CreateBinTree(T->left); CreateBinTree(T->right); }}
void PreOrder(BinTree T){//先序遍历 if(T){ printf("%c",T->data); PreOrder(T->left); PreOrder(T->right); }}void InOrder(BinTree T){//中序遍历 if(T){ InOrder(T->left); printf("%c",T->data); InOrder(T->right); }}void BackOrder(BinTree T){//后序遍历 if(T){ BackOrder(T->left); BackOrder(T->right); printf("%c",T->data); }}
#include //STL栈,不用手写了void PreOrder2(BinTree T){//非递归先序遍历 if(T){ stack<BinTree>stk; stk.push(T); BinTree cur = T; while(!stk.empty()){ cur = stk.top();stk.pop(); cout<<cur->data; if(cur->right!=NULL)stk.push(cur->right);//栈后进先出 if(cur->left!=NULL)stk.push(cur->left); } }}void InOrder2(BinTree T){//非递归中序遍历 if(T){ stack<BinTree>stk; BinTree cur = T; while(!stk.empty()||cur!=NULL){ if(cur!=NULL){ stk.push(cur); cur = cur->left; }else{ cur = stk.top();stk.pop(); cout<<cur->data; cur = cur->right; } } }}void BackOrder2(BinTree T){//非递归后序遍历 if(T){ stack<BinTree>stk1; stack<BinTree>stk2; BinTree cur = T; stk1.push(cur); while(!stk1.empty()){ cur = stk1.top();stk1.pop(); stk2.push(cur); if(cur->left!=NULL)stk1.push(cur->left); if(cur->right!=NULL)stk1.push(cur->right); } while(!stk2.empty()){ cur = stk2.top();stk2.pop(); cout<<cur->data; } }}
可以顺便统计不同度的结点个数
#include //STL队列 不用手写了int deg0,deg1,deg2;void BFS(BinTree T){//可以顺便统计不同度数的结点个数 if(T){ queue<BinTree>q; q.push(T); while(!q.empty()){ BinTree head = q.front();q.pop();//获取队首后出队 cout<<head->data; if(head->left==NULL&&head->right==NULL)deg0++; else if(head->left!=NULL&&head->right!=NULL)deg2++; else deg1++; if(head->left)q.push(head->left); if(head->right)q.push(head->right); } }}
这些玩意其实都可以直接在建树的时候就维护起来的,下面是建树后的一些递归算法。
int CountLeaf(BinTree T){//统计叶子结点个数 int res=0; if(T==NULL)return 0; if(T->left==NULL)return 1+CountLeaf(T->right); else return CountLeaf(T->left)+CountLeaf(T->right);}int GetDepth(BinTree T){//统计树的深度 int LeftDepth=0,RightDepth=0; if(T==NULL)return 0; else{ LeftDepth=GetDepth(T->left); RightDepth=GetDepth(T->right); if(LeftDepth>RightDepth)return LeftDepth+1; else return RightDepth+1; }} int dep[105];//dep表层数,记录第i层有多少个结点int GetWidth(BinTree T){//统计二叉树的最大宽度 if(T==NULL)return 0; //BFS层序遍历 queue<BinTree>q;q.push(T); queue<int>depth;depth.push(1); while(!q.empty()){ BinTree head=q.front();q.pop(); int now_depth=depth.front();depth.pop(); dep[now_depth]++; if(head->left){ q.push(head->left); depth.push(now_depth+1); } if(head->right){ q.push(head->right); depth.push(now_depth+1); } } int ret=0; for(int i=1;i<=105;i++)ret=max(ret,dep[i]); return ret;}
bool Compare(
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/124056.html
摘要:也就是说事件流一定是按上面的顺序经过这三个阶段。关于事件捕获,事件冒泡的引用场景,有事件委托等。下面引用两篇文章。 什么是捕获?什么是冒泡? 给元素绑定事件会经历三个阶段:一:捕获阶段(capture phase)--先从根元素流向目标元素;二:目标阶段(target phase)--在目标元素上的事件被触发;三:冒泡阶段(bubbling phase)--目标元素流向根元素 就像你中...
摘要:也就是说事件流一定是按上面的顺序经过这三个阶段。关于事件捕获,事件冒泡的引用场景,有事件委托等。下面引用两篇文章。 什么是捕获?什么是冒泡? 给元素绑定事件会经历三个阶段:一:捕获阶段(capture phase)--先从根元素流向目标元素;二:目标阶段(target phase)--在目标元素上的事件被触发;三:冒泡阶段(bubbling phase)--目标元素流向根元素 就像你中...
摘要:也就是说事件流一定是按上面的顺序经过这三个阶段。关于事件捕获,事件冒泡的引用场景,有事件委托等。下面引用两篇文章。 什么是捕获?什么是冒泡? 给元素绑定事件会经历三个阶段:一:捕获阶段(capture phase)--先从根元素流向目标元素;二:目标阶段(target phase)--在目标元素上的事件被触发;三:冒泡阶段(bubbling phase)--目标元素流向根元素 就像你中...
摘要:也就是说事件流一定是按上面的顺序经过这三个阶段。关于事件捕获,事件冒泡的引用场景,有事件委托等。下面引用两篇文章。 什么是捕获?什么是冒泡? 给元素绑定事件会经历三个阶段:一:捕获阶段(capture phase)--先从根元素流向目标元素;二:目标阶段(target phase)--在目标元素上的事件被触发;三:冒泡阶段(bubbling phase)--目标元素流向根元素 就像你中...
摘要:今天邀请了一位美女学霸做个考研经验分享。但从备考压力过程来看,难度绝对不输小学到现在的任何一次考试。如果你坚持不下去了,可以去看看最近的电影哪吒之魔童降世。如果说大圣归来探讨了考研这个话题,那哪吒之魔童降世,则完美的诠释了这几句话。 今天邀请了一位美女学霸 ZeroBanMa 做个考研经验分享。 ---- ---- ---- 考研的这段经历,也是自我成长的一种,即使失败,我也觉得没...
阅读 2427·2021-11-22 09:34
阅读 3364·2021-11-17 09:33
阅读 2531·2021-09-01 10:30
阅读 1517·2019-08-30 15:52
阅读 854·2019-08-29 18:40
阅读 962·2019-08-28 18:30
阅读 2245·2019-08-23 17:19
阅读 1161·2019-08-23 16:25