Data Structures and Algorithms (Eng
懒狗的救赎
HUMBLE.
展开
-
6-2 Two Stacks In One Array (20 分)
Stack CreateStack( int MaxElements ){ Stack s=malloc(sizeof (struct StackRecord)); s->Array=(int *)malloc(MaxElements*(sizeof (int))); s->Capacity=MaxElements; s->Top1=-1; s->Top2=s->Capacity; return s;}int IsEmpty(原创 2021-04-18 11:07:42 · 192 阅读 · 0 评论 -
6-1 Deque (25 分)
Deque CreateDeque(){ PtrToNode head = malloc(sizeof(struct Node)); head->Next=head->Last=NULL; Deque D=malloc(sizeof (struct DequeRecord)); D->Front=D->Rear=head; return D;}int Push( ElementType X, Deque D ){ PtrToNode原创 2021-04-18 10:36:41 · 318 阅读 · 0 评论 -
6-17 Shortest Path [4] (25 分)
void ShortestDist( MGraph Graph, int dist[], int path[], Vertex S ){ int vis[MaxVertexNum]={0},n=Graph->Nv; for(int i=0;i<n;i++){ dist[i]=INFINITY; path[i]=-1; } dist[S]=0; for(int i=0;i<n;i++){ int min=原创 2021-04-17 11:47:23 · 171 阅读 · 0 评论 -
6-16 Shortest Path [3] (25 分)
void ShortestDist( MGraph Graph, int dist[], int count[], Vertex S ){ int vis[MaxVertexNum]={0},n=Graph->Nv; for(int i=0;i<n;i++){ dist[i]=Graph->G[S][i]; count[i]=0; } //vis[S]=1; dist[S]=0; count[S]=1;原创 2021-04-17 11:21:19 · 303 阅读 · 0 评论 -
6-15 Iterative Mergesort (25 分)
void merge_pass( ElementType list[], ElementType sorted[], int N, int length ){ int h1=0,e1=length-1,h2=length,e2=2*length-1; //h1、h2分别是两个归并数组的开头,e1、e2是结尾 int k=0,last=0; while(1){// printf("%d %d - %d %d\n",h1,e1,h2,e2); if(e2&原创 2021-04-16 23:27:45 · 244 阅读 · 0 评论 -
6-14 Count Connected Components (20 分)
int vis[MaxVertexNum]={0};void DFS(LGraph Graph,int v){ vis[v]=1; PtrToAdjVNode p=Graph->G[v].FirstEdge; while(p){ if(vis[p->AdjV]==0){ DFS(Graph,p->AdjV); } p=p->Next; }}int CountConnecte原创 2021-04-16 16:12:40 · 257 阅读 · 0 评论 -
6-13 Topological Sort (25 分)【拓扑排序】
代码:bool TopSort( LGraph Graph, Vertex TopOrder[] ){ int k=0,n=Graph->Nv,v; PtrToAdjVNode p; int stack[MaxVertexNum],top=-1; int inDegree[MaxVertexNum]={0}; for(int i=0;i<n;i++){ //统计入度 p=Graph->G[i].FirstEdge;原创 2021-04-16 14:31:44 · 298 阅读 · 0 评论 -
6-12 Shortest Path [2] (25 分)【单元最短路径 - 迪杰斯特拉】
void ShortestDist( MGraph Graph, int dist[], Vertex S ){ int n=Graph->Nv; for(int i=0;i<n;i++){ dist[i]=Graph->G[S][i]; } int vis[MaxVertexNum]={0}; vis[S]=1; dist[S]=0; int min,v; for(int i=0;i<n;i++){原创 2021-04-16 13:30:55 · 333 阅读 · 0 评论 -
6-11 Shortest Path [1] (25 分)【非权值图 - BFS求单源最短路径】
void ShortestDist( LGraph Graph, int dist[], Vertex S ){ for(int i=0;i<Graph->Nv;i++){ dist[i]=-1; } int vis[MaxVertexNum]={0}; PtrToAdjVNode p; vis[S]=1;// printf("%d ",S); dist[S]=0; int queue[MaxVertexNum]={原创 2021-04-15 16:53:04 · 246 阅读 · 0 评论 -
6-8 Percolate Up and Down (20 分)【堆结点的插入与删除】
堆节点的插入:void PercolateUp( int p, PriorityQueue H ){ //插入时,小的数字向上调整 int i; int temp=H->Elements[p]; for(i=H->Size;temp<H->Elements[i/2];i/=2){ //当插入的数字小于父节点则向上调整 H->Elements[i]=H->Elements[i/2]; } H->Element原创 2021-04-14 17:04:02 · 1382 阅读 · 0 评论 -
6-7 Isomorphic (20 分)
int Isomorphic( Tree T1, Tree T2 ){ if(!T1&&!T2) return 1; else if(T1&&T2){ if(T1->Element==T2->Element){ int b1=Isomorphic(T1->Left,T2->Left)||Isomorphic(T1->Left,T2->Right);原创 2021-04-14 14:56:13 · 274 阅读 · 0 评论 -
6-5 Evaluate Postfix Expression (25 分)
ElementType EvalPostfix( char *expr ){ double s1[10],t; int top1=-1,top2=-1,l=strlen(expr),neg=0; char s2[10]; for(int i=0;i<l;){ if(expr[i]=='-'&&expr[i+1]!=' '&&i!=l-1){ neg=1; i++;原创 2021-04-14 12:33:47 · 375 阅读 · 0 评论 -
6-3 Add Two Polynomials (20 分)
Polynomial Add( Polynomial a, Polynomial b ){ Polynomial s=(Polynomial)malloc(sizeof (struct Node)); Polynomial p=a->Next,q=b->Next,r=s; while(p&&q){ if(p->Exponent>q->Exponent){ r->Next=p;原创 2021-04-14 09:55:42 · 217 阅读 · 0 评论