
数据结构刷题
考研数据结构题目,大多是伪代码,具体细节根据使用的语言具体分析
vener__
软件没开消息通知,看缘分回复
展开
-
判断有向图中是否存在从vi到vj的路径
可根据具体情况和存储结构修改为一个函数,这个算法里dfs是走到底的,合并为一个函数的时候可以加上判断语句bool visited[Maxsize];SqStack S;bool Pathij(Graph G,int i,int j){ int v; for(v=0;v<G.vexnum;v++) visited[v]=false; InitStack(S); DFS(G,i); if(visited[j]) return true; return false;}vo原创 2020-12-10 17:12:08 · 2153 阅读 · 1 评论 -
求无向图的连通分量个数
bool visited[Maxsize];SqQueue Q;int Num(Graph G){ int i; int num=0; for(i=0;i<G.vexnum;i++) visited[i]=false; InitQueue(Q); for(i=0;i<G.vexnum;i++) if(!visited[i]) { num++; BFS(G,i);//或使用DFS }}void BFS(Graph G,int v){ int p.原创 2020-12-10 16:47:31 · 1491 阅读 · 2 评论 -
判断单链表是否关于中心对称
bool function(LinkList L,int n){ Elemtype array[n/2]; LNode *p=L->next; int i; for(i=0;i<n/2;i++) { array[i]=p->data; p=p->next; } i--; if(n%2!=0) p=p->next; while(p!=NULL&&i>=0) { if(array[i]==p->data) {.原创 2020-12-09 21:44:38 · 1009 阅读 · 0 评论 -
判断一棵二叉树是否是平衡二叉树
bool judgeAVL(BiTree T){ if(T==NULL) return true; int hl=height(T->lchild); int hr=height(T->rchild); if(hl-hr>1||hl-hr>1) return false; else return judgeAVL(T->lchild)&&judgeAVL(T->rchild);}其中height函数可使用https://.原创 2020-12-09 21:34:24 · 164 阅读 · 0 评论 -
递归求二叉树的高度
int height(BiTree T){ if(T==NULL) return 0; int hl=height(T-lchild); int hr=height(T->rchild); return hl>hr ? hl+1 : hr+1;}原创 2020-12-09 21:33:55 · 536 阅读 · 0 评论 -
递归求叶结点个数
之前写过用递归遍历的方法求叶子结点个数https://blog.youkuaiyun.com/vener_/article/details/108295114这个感觉也非常不错int Leaf(BiTree T){ if(T==NULL) return 0; if(T->lchid==NULL&&T->rchild==NULL) return 1; return Leaf(T->lchild)+Leaf(T->rchild);}...原创 2020-12-08 21:18:51 · 1164 阅读 · 0 评论 -
求非空二叉树的宽度
1.王道课后版(略有改动)typedef struct{ BiTree data[Maxsize]; int level[Maxsize];//这个数组也可以单独定义 int front,rear;}Q;int BTWidth(BiTree T){ BiTNode *p; int k,max,i,n; Q.front=Q.rear=-1; Q.data[Q.rear]=T; Q.rear++; Q.level[Q.rear]=1; while(Q.front<Q.re原创 2020-12-08 20:48:00 · 509 阅读 · 4 评论 -
删除不带头结点的循环单链表的结点x的前驱结点
1.存储结构描述typedef struct LNode{ Elemtype data; struct LNode *next;}LNode,*CLinkList;void init(CLinkList &T){ L->next=L;}2. 有两种循环方法,但是都无法解决需要删除第一个结点时候循环链表的连续问题,可能需要题目进一步约束吧void DeletePre(CLinkList &L,Elemtpye x){ LNode *p=L->.原创 2020-12-06 17:15:47 · 713 阅读 · 0 评论 -
将图的邻接表转为邻接矩阵
typedef struct{ int vertex[max]; int arc[max][max]; int vexnum,arcnum;}MGraph;typdef struct ArcNode{ int adjvex; struct ArcNode *next;}ArcNode;typedef struct VNode{ int data; ArcNode *first;}VNode;typedef struct{ VNode vertices[max]; in.原创 2020-12-04 16:30:15 · 966 阅读 · 3 评论 -
判断两个二叉树是否相似或相等
1.判断两个二叉树是否相似typedef struct BiTNode{ Elemtype data; struct BiTNode *lchild,*rchild;}BiTNode;bool similar(BiTNode T1,BiTNode T2){ bool l,r; if(T1==NULL&&T2==NULL) return true; else if(T1==NULL||T2==NULL) return 0; else { l=simila原创 2020-12-04 16:11:50 · 816 阅读 · 0 评论 -
递增单链表的合并
1.合并后生成新的链表void merge(LinkList A,LinkList B,LinkList &C){ LNode *pa,*pb,*rc,*s; pa=A->next;pb=B->next; InitLinkList(C); rc=Lc; while(pa&&pb) { if(pb->data<=pa->data) { s=(LNode*)malloc(sizeof(LNode)); s->da原创 2020-11-28 20:09:45 · 440 阅读 · 0 评论 -
求树中所有叶子结点路径
typedef struct CSNode{ ElemType data; struct CSNode *fch,*nsib;}CSNode,*CSTree;typedef struct { EemType data[Max]; int top;}SqStack;void AllTreePath(CSTree T,SqStack S){ whlie(T) { Push(S,T->data); if(!T->fch) PrintStack(S); el.原创 2020-10-24 22:03:58 · 325 阅读 · 0 评论 -
孩子兄弟表示法和双亲表示法求树的高度/深度
typedef struct CSNode{ ElemType data; struct CSNode *fch,*nsib;}CSNode,*CSTree;int Height(CSTree T){ int hc,hs; if(T==NULL) return 0; else { hc=height(T->fch); hs=height(T->nsib); if(hc+1>hs) return hc+1; else return hs;.原创 2020-10-21 16:12:34 · 2398 阅读 · 0 评论 -
孩子兄弟表示法求树的度和高度
孩子兄弟表示法typedef struct CSNode{ ElemType data; struct CSNode *firstchild,*nextsibling;}CSNode,*CSTree;void Degree(CSTree T,int &max){ if(T==NULL) return; if(T->firstchild) { p=T->firstchild; n=1; while(p->nextsibling) {原创 2020-09-30 16:44:01 · 3524 阅读 · 1 评论 -
将长度为n的顺序表转换为带头结点的单链表,元素位序不变
typedef struct{ Elemtype data[Max]; int length;}SqList;typedef struct LNode{ Elemtype data; struct LNode *next;}LNode,*LinkList;LinkList convert(SqList L){ LinkList A=(LNode*)malloc(sizeof(LNode)); LNode *p,*r=A; for(int i=0;i<n;i++) { .原创 2020-09-30 16:16:20 · 1422 阅读 · 0 评论 -
输出二叉树中每个叶子结点到跟结点的路径
typedef struct BiTNode{ ElemType data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;void Route(BiTree &T){ if(T==NULL) return; if(T->lchild==NULL&&T->rchild==NULL) { cout<<T->data<<endl;原创 2020-09-30 16:05:15 · 2354 阅读 · 0 评论 -
将数组A(1:n)中所有正数排在所有负数的前边
844真题相当于一趟快速排序的划分,但是由于枢轴0没有本身的位置,所以需要每次进行交换而不是像快排那样交错进行交换void Patition_0(int A[],int n){ int x; int low=1,high=n; while(low<high) { while(low<high&&A[high]<0) high--; while(low<high&&A[low]>0) i++; if(lo原创 2020-09-29 16:59:17 · 990 阅读 · 0 评论 -
844题目
设从键盘输入一整数的序列:a1,a2,....,an,试编写算法实现:用栈结构存储输入的整数,当ai不等于-1时,将ai进栈;当ai=-1时,输出栈顶整数并出栈。算法对应异常情况(入栈满等)给出相应信息typedef struct{ int data[max]; int top;}SqStack;void PushPop(SqStack &S){ int x=-1; S.top=-1; for(int i=0;i<n;i++) { scanf("%d",&原创 2020-09-29 16:44:08 · 714 阅读 · 0 评论 -
递增的单链表A和B表示集合,求A和B的交集并存放于A中
844真题typedef struct LNode{ ElemType data; struct LNode *next;}LNode,*LinkList;LinkList common(LinkList &A,LinkList B){ LNode *pa=A->next; LNode *pb=B->next; LNode *r=A; LNode *s; while(pa&&pb) { if(pa->data==pb->dat原创 2020-09-29 16:27:28 · 685 阅读 · 2 评论 -
用两个栈实现队列的进队、出队和判空操作
typedef struct{ ElemType data; int top;}SqStack;void EnQueue(SqStack &S1,SqStack &S2,int x){ int t; if(S1.top==max-1) { if(!Isempty(S2)) return; else { while(!Isempty(S1)) { t=Pop(S1); Push(S2,t); } Push(S1,x).原创 2020-09-28 16:58:29 · 319 阅读 · 0 评论 -
用队列将栈中元素逆置
元素为int型typedef struct{ int data[Max]; int front,rear;}SqQueue;typedef struct{ int data[Max]; int top;}SqStack;void function(SqStack &S){ SqQueue Q; InitQueue(Q); int t; if(S==NULL||S.top=-1) return; while(!Empty(S)) { Pop(S,t);原创 2020-09-28 16:23:17 · 2026 阅读 · 0 评论 -
判断邻接矩阵存储的图是否为连通图
用深度优先搜索判断typedef struct{ char vex[Max]; int arc[Max][Max]; int vexnum,arcnum;}MGraph;bool visited[Max];bool Judge(MGraph G){ for(int v=0,v<G.vexnum,v++) { visited[v]=false; } DFS(G,0); for(int v=0;v<G.vexnum;v++) if(!visited[v])原创 2020-09-28 16:07:32 · 2907 阅读 · 1 评论 -
有向图图的邻接矩阵求每个结点的度
typedef struct{ char vex[Max]; int arc[Max][Max]; int vexnum,arcnum;}MGraph;void Print_io(MGraph G){ int v=G.vexnum; int e=G.arcnum; for(int i=0;i<v;i++) { int out=0; for(int j=0;i<v;j++) { out+=G.arc[i][j]; } } cout<<".原创 2020-09-28 15:51:29 · 2635 阅读 · 1 评论 -
求二叉链表深度的算法
844真题typedef struct BiTNode{ ElemType data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;int Depth(BiTree T){ int level; if(T==NULL) return 0; else { int ldepth=Depth(T->lchild); int rdepth=Depth(T->rchild); if(ldepth>rdept原创 2020-09-28 15:23:10 · 973 阅读 · 0 评论 -
二叉树的后序遍历的逆序输出
typedef struct BiTNode{ ElemType data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;void PostOrder(BiTree T,SqStack &S){ if(T!=NULL) { reverse_PostOrder(T->lchild); reverse_PostOrder(T->rchild); Push(S,T->data); }}void Pri.原创 2020-09-27 16:40:28 · 4369 阅读 · 1 评论 -
将单链表中的偶数和奇数放到两个链表里
844真题元素类型为inttypedef struct LNode{ int data; struct LNode *next;}LNode,*LinkList;void devide(LinkList &L){ LNode *q=L,p=L->next,*s; LinkList *Odd=(LNode *)malloc(sizeof(LNode)); LinkList *Even=(LNode *)malloc(sizeof(LNode)); while(p)原创 2020-09-27 16:19:46 · 1234 阅读 · 1 评论 -
判断单链表序列
升序序列返回1,降序返回-1,无序返回0844真题typedef struct LNode{ Elemtype data; struct LNode *next;}LNode,*LinkList;int judge(LinkList L){ LNode *p=L->next; while(p->data==p->next->data) p=p->next;//找到起始不相等的元素 if(p->next->data>p->d原创 2020-09-27 16:03:02 · 154 阅读 · 0 评论 -
线性表的逆置
844真题1.顺序存储typedef struct{ ElemType data[MaxSize]; int length;}SqList;void Reverse(SqList &L){ ElemType t; for(i=0;i<L.length/2;i++) { t=L.data[i]; L.data[i]=L.data[L.length-i+1]; L.data[L.length-i+1]=t; }}2.链式存储typedef st原创 2020-09-25 16:44:56 · 408 阅读 · 0 评论 -
在单链表中删除值相同的多余结点
typedef struct LNode{ ElemTpye data; struct LNode *next;}LNode,*LinkList;LinkList Del_Com(LinkList &L){ LNode *p,*q,*s; for(p=L->next;p!=NULL;p=p->next) { for(q=p->next,s=p;q!=NULL;q=q->next) { if(q->next->data==p->.原创 2020-09-25 15:55:00 · 1387 阅读 · 2 评论 -
将无向图的邻接矩阵转化为邻接表
typedef struct{ int vertex[max]; int arc[max][max]; int vexnum,aecnum;}MGraph;typedef struct ArcNode{ int adjvex; struct ArcNode *next;}ArcNode;typedef struct VNode{ int data; ArcNode *first;}VNode,AdjList[max];typedef struct{ AdjList ver.原创 2020-09-25 15:36:59 · 5527 阅读 · 8 评论 -
找出两个链表的值相同的结点并生成新的链表
设计生成集合C=A交B A,B,C用链式结构表示2013年844真题typedef struct LNode{ ElemType data; struct LNode *next;}LNode,*LinkList;LinkList common(LinkList A,LinkList B){ LinkList C=(LNode *)malloc(sizeof(LNode)); LNode *p,*q,*s; for(p=A-...原创 2020-09-24 17:07:44 · 817 阅读 · 0 评论 -
求二叉树中结点x的双亲结点
typedef struct BiTNode{ ElemType data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;BiTNode *parent(BiTree T,ElemType x){ BiTNode *ans; if(T==NULL) return NULL; if(T->lchild==NULL&&T->rchild==NULL) return NULL else { if.原创 2020-09-24 16:50:48 · 11179 阅读 · 18 评论 -
在单链表的头结点后插入一个结点
typedef struct LNode{ ElemType data; struct LNode *next;}LNode,*LinkList;void Insert(LinkList &L,ElemType x){ if(L==NULL) return; LNode *s=(LNode*)malloc(sizeof(LNode)); s->data=x; s->next=L->next; L->next=s;}2013年.原创 2020-09-24 16:27:53 · 1930 阅读 · 1 评论 -
创建有向图的邻接矩阵的算法
typedef struct { VertexType vexs[VNum]; EdgeType Arc[VNum]; int vexnum,arcnum;}MGraph;void Create(MGraph &G,int v,int e){ G.vexnum=v; G.arcnum=e; for(i=0;i<v;i++) scanf(&G.vexs[i]); for(i=0;i<v;i++) for(j=o;j<v;j++) G.ar.原创 2020-09-24 14:40:59 · 1498 阅读 · 0 评论 -
带头结点的有尾指针的链队列的出队入队操作
typedef struct LNode{ ElemType data; struct LNode *next;}LNode,*LinkList;typedef struct{ LinkList front;//指向头结点 LNode *rear;//队尾指针}LinkQueue;void EnQueue(LinkQueue &Q,ElemType x){ LNode *p=(LNode*)malloc(sizeof(LNode)); p->data=x; p-&.原创 2020-09-23 16:54:31 · 1640 阅读 · 0 评论 -
求二叉链表中的最大值结点
typedef struct BiTNode{ ElemType data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;ElemType Max(BiTree T){ if(T==NULL) return; m=T->data; if(T->lchild&&T->rchild) if(T->lchild>T->rchild) return (m>T->lch.原创 2020-09-23 15:58:58 · 800 阅读 · 0 评论 -
在有序循环链表中插入新结点
2011年844真题某电器商场仓库中一批电视机,按其价格从低到高的次序构成了一个循环链表,表中的每个元素指出了价格、数量和链指针三个域。现在新到m台价格为h元的电视机入库。试编写出仓库电视机链表增加电视机的算法typedef struct LNode{ int num; float price; struct LNode *next;}LNode,*LinkList;void Insert(LinkList L,int m,float h){ LNode *p,q; p=L-&g原创 2020-09-23 15:28:10 · 1271 阅读 · 1 评论 -
求顺序存储的二叉树中结点i,j最近的公共祖先结点
ElemType Com_Ancestor(SqTree T,int i,int j){ if(T[i]!='#'&&T[j]!='#'); { whlie(i!=j) { if(i>j) i=i/2; else j=j/2; } return T[i]; }}原创 2020-09-23 14:38:52 · 565 阅读 · 1 评论 -
在二叉树中查找值为x的结点并输出所有祖先(值x的结点至多有一个)
void Search(BiTree T,ElemType x){ BTNode *p=T,r=NULL; Stack S; InitStack(S); while(p!=NULL||!IsEmpty(S)) { if(p&&p->data!=x) { Push(S,p); p=p->lchild; } else if(p->data==x) { for(i=1,i<=S.top;i++) { prin.原创 2020-09-22 16:19:45 · 3648 阅读 · 9 评论 -
删除二叉树中元素值为x的结点及其子树
void DeleteTree(BiTree T){ if(T) { DeleteTree(T->lchild); DeleteTree(T->rchild); free(T); }}void Search(BiTree T,ElemType x){ SqQueue Q; if(T) { if(T->data==x) { DeleteTree(T); return; } InitQueue(Q); EnQueue(Q,T);.原创 2020-09-22 15:32:28 · 2728 阅读 · 1 评论