东北大学842考研

2005-c-二-2

#include <stdio.h> #define maxSize 100 void delstring(char *pstr1,char *pstr2) { char *p,*q,*t; int len=0;//子串长度 p=pstr1; q=pstr2; while(*q!='\0') { ++len;//求子串长度 ++q; } while(*p!='\0') { q=pstr2;//每次完成后子串指针复位 while(*p==*q&&*q!='\0')//匹配子串 { p++; q++; } if(*q=='\0') { t=p-len; while(*p!='\0') { *(p-len)=*p; p++; } *(p-len)='\0'; p=t; }else{ ++p; } } printf("%s",pstr1); } int main() { char a[maxSize],b[maxSize]; scanf("%s",a); scanf("%s",b); printf("%s\n",a); delstring(a,b); }

2005-d-三

参考链接 http://www.acmerblog.com/ancestors-of-a-given-node-6048.html /** 1、顺序存储,假设根的存储下标是1 将当前结点的下标连续整除以2,直到1为止, 中间所有得到的商的下标都是其祖先,并且是从其双亲直到根为止。 2、链式存储 (1)使用非递归的后序遍历,当遍历到该结点时,辅助栈中从栈顶到栈底依次为该结点从双亲开始到根为止的所有祖先。 (2)递归实现。 **/ //顺序存储,采用下标关系找出祖先结点 void ancestor(int bt[],int i,int x) { if(bt[i]==x)//满足条件,输出祖先 { while(i!=1) { printf("%d",i/2); i/=2; } } else { ancestor(bt,2*i,x); ancestor(bt,2*i+1,x); } } //后序非递归遍历,输出值为x的所有祖先 void afterOrder(BiTree *root,int x) { struct Tree { BiTree *bt; int tag;//1-第一次访问(左孩子)、2-非第一次访问(右孩子) }; Tree stack[maxSize],top=0;//定义一个栈 Tree p; p.bt=root; p.tag=1;//根结点入栈 while(p!=NULL || top>0)//此处当p==NULL时,继续出栈 { while(p.bt!=NULL) { ++top; stack[top].bt=p; stack[top].tag=1; p.bt=p.bt->lchild;//指向左孩子 } //添加代码功能START if(bt->data==x)//找到x { printf("所查结点的所有祖先结点的值为:\n"); for(i=1;i<=top;i++) printf("%d",stack[i].

2005-d-二

//链表节点 typedef struct LNode { int data;//节点值 struct LNode *next;//指向下一个节点 } LNode; //哈希节点 typedef struct HNode { LNode *first;//链接的第一个指针域 } HNode; //哈希表 typedef struct HashTable { HNode list[N]; }HashTable; int seadel( HashTable *H,int k) { int pos=H(k); LNode *p,*r; p=H->list[pos]->first; //判断第一个结点是否是 if(p->data==k) { H->list[pos]->first=p->next; free(p); } r=p; p=p->next; //第一个不是 while(p!=NULL) { if(p->data==k) { r->next=p->next; free(p); return 1; }else{ r=p; p=p->next; } } return 0; }

2005-d-四

int visit[maxSize];//初始化全为0 void DFS(AGraph *G,int v,int t) { ArcNode *p; visit[v]=1; p=G->adjlist[v].firstarc; while(p!=NULL) { //在深度优先搜索中添加部分 if(p->adjvex==t) { printf("exist"); break; }else{ if(visit[p->adjvex]==0) { DFS(G,p->adjvex,t); p=p->nextarc; } } } }

2005-c-一-2

#include <stdio.h> int main() { char *s="121"; int k=0,a=0,b=0; do{ k++; if(k%2==0){a=a+s[k]-'0';continue;} b=b+s[k]-'0';a=a+s[k]-'0'; }while(s[k+1]); printf("k=%d,a=%d,b=%d",k,a,b); }

2007-c-四

//优化简洁算法 void recycle(LNode *c) { int i=1; LNode *p,*q,*pre; pre=c; p=c->next; while(p!=NULL) { if((x&(x-1))==0)//此处采取位运算:如果一个数x为2的平方,那么x&x-1==0 { printf("%d",p->data); pre->next=p->next; q=p;//暂存待释放结点 p=p->next; free(q);//释放 } p=p->next;pre=pre->next;//指针下移 ++i;//报数增加一 } if(c->next!=NULL) recycle(c);//圈子里面,还有人继续报数 } ----------------------------------------------------------------------------------------- #include #include #define N 10 typedef struct LNode { int data; struct LNode *next; } LNode; //建立线性表 LNode* create() { LNode *c,*s,*r; int i; c=(LNode*)malloc(sizeof(LNode)); c->next=NULL; r=c; for(i=1;i<=N;++i) { s=(LNode*)malloc(sizeof(LNode)); s->data=i; s->next=NULL; r->next=s; r=r->next; } return c; } //删除线性表某个节点 void seaDel(LNode *c,int x) { LNode *p,*q; p=c; //查找 while(p->next!

2007-d-四

#include #include typedef struct BTNode { int data; struct BTNode * lchild; struct BTNode * rchild; } BTNode; /* 创建排序树 */ BTNode* create() { BTNode *root; int n; scanf( "%d", &n ); if ( n == -1 ) return(NULL); else{ root = (BTNode *) malloc( sizeof(BTNode) ); root->data = n; root->lchild = create(); root->rchild = create(); } return(root); } /* 向二叉排序树中插入值 */ void insert( BTNode* root, int key ) { BTNode * p = NULL, *pre = NULL; BTNode * c = (BTNode *) malloc( sizeof(BTNode) ); c->data = key; c->lchild = NULL; c->rchild = NULL; /* 树为空 */ if ( NULL == root ) { root == c; }else{ p = root; while ( NULL !

2007-d-三

有錯誤 #include <stdio.h>#include <stdlib.h> #define maxSize 100 //邻接表定义存储 typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; } ArcNode; typedef struct VNode { int data; ArcNode *firstarc; }VNode; typedef struct AGraph { VNode adjlist[maxSize]; int n,e; } AGraph; //邻接表建立 void create(AGraph *G) { int i; int h,t; ArcNode *p; printf("请输入顶点数、弧数:\n"); scanf("%d,%d",&G->n,&G->e); //输入顶点数、弧数 for(i=0;i<G->n;++i) { printf("请输入第%d个顶点值\n",i); scanf("%d",&G->adjlist[i].data); G->adjlist[i].firstarc=NULL; } //输入弧 for(i=0;i<G->e;++i) { printf("请按顺序输入弧头[h]、弧尾[t]\n"); scanf("%d,%d",&h,&t); p=(ArcNode*)malloc(sizeof(ArcNode)); p->adjvex=t; /*将新边表结点p 插入到顶点Vh 的边表头部*/ p->nextarc=G->adjlist[h].

2007-d-二

typedef struct BTNode { int data; int b;//平衡因子 struct BTNode *lchild; struct BTNode *rchild; } BTNode; int getDepth(BTNode *T) { int level=0; BTNode *p; p=T; while(p!=NULL) { ++level; if(p->b<0) p=p->rchild;//右子树高 else p=p->lchild;//左子树高 } return level; }

2009-d-二-1

void delpre(LNode *x) { LNode *r,*p;//r是p的前驱结点,当p的后继结点是x时,删除p p=x; if(p->next!=NULL) { while(p->next!=x) { r=p; p=p->next; } r->next=x; free(p); } }