数据结构实验 基础题集

来源:互联网 发布:软件测试管理全程实践 编辑:程序博客网 时间:2024/05/16 05:44


1.线性表基本操作函数,调用基本操作函数

#include<memory>typedef struct {int *list;int size;int maxsize;}LIST;int InitList(LIST *L,int ms){L->list=(int*)malloc(sizeof(int)*ms);if(L->list==NULL) return -1;L->maxsize=ms;L->size=0;return 1;}int InsertList(LIST *L,int item,int rc){int i;if(L->size==L->maxsize) return -1;if(rc<0) rc=0;if(rc>L->size) rc=L->size;for(i=L->size-1;i>=rc;i--)L->list[i+1]=L->list[i];L->list[rc]=item;L->size++;return 1;}int DeleteList1(LIST *L,int item){int i,j;for(i=0;i<L->size;i++) if(L->list[i]==item) break;if(i==L->size) return -1;for(j=i;j<L->size-1;j++) L->list[j]=L->list[j+1];L->size--;return 1;}int DeleteList2(LIST *L,int rc){int i;if(rc<0||rc>L->size-1) return 0;for(i=rc;i<L->size-1;i++) L->list[i]=L->list[i+1];L->size--;return 1;}int FindList(LIST *L,int item){int i;for(i=0;i<L->size;i++) if(L->list[i]==item) break;if(i==L->size) return -1;else return i;}void OutputList(LIST *L){int i;for(i=0;i<L->size;i++) printf("%d ",L->list[i]);printf("(size:%d)",L->size);printf("\n");}int main(){LIST a;int item,rc;printf("list:%p size:%d maxsize:%d\n",a.list,a.size,a.maxsize);InitList(&a,100);printf("list:%p size:%d maxsize:%d\n",a.list,a.size,a.maxsize);while(1){scanf("%d %d",&item,&rc);if(item==0) break;InsertList(&a,item,rc);OutputList(&a);}while(1){scanf("%d",&item);if(item==0) break;printf("%d at %d\n",item,FindList(&a,item));}while(1){scanf("%d",&item);if(item==0) break;DeleteList1(&a,item);OutputList(&a);}while(1){scanf("%d",&rc);if(rc==-1) break;DeleteList2(&a,rc);OutputList(&a);}return 0;}

2.合并两个有序线性表,要求同样的元素只出现一次

#include<cstdio>int a[11]={1,3,5,6,7,10,11,18,25,30,32768};int b[11]={2,5,7,9,11,15,25,35,67,100,32768};int c[20];int main(){int i,j,k;i=0;j=0;k=0;while(i<10||j<10){if(a[i]<b[j]) c[k++]=a[i++];else if(a[i]>b[j]) c[k++]=b[j++];else {c[k++]=b[j++];i++;}}for(i=0;i<k;i++) printf("%d ",c[i]);return 0;}

3.要求以较高的效率删除线性表中元素之在x到y之间的所有元素

#include<cstdio>#define X 10#define Y 20int a[21]={2,1,13,19,15,17,5,7,9,10,12,11,3,5,6,20,30,75,34,72,14};int main(){int i,j;i=0;j=0;for(j=0;j<21;j++) if(a[j]<X||a[j]>Y) a[i++]=a[j];for(j=0;j<i;j++) printf("%d ",a[j]);}


 

 

1.链表基本操作函数,调用基本操作函数

#include<cstdio>#include<memory>typedef struct list{int data;struct list *next;}LIST;void InitList(LIST **p){*p=NULL;}void InsertList1(LIST **p,int item,int rc){int i;LIST *u,*q,*r;u=(LIST*)malloc(sizeof(LIST));u->data=item;for(i=0,r=*p;i<rc&&r!=NULL;i++){q=r;r=r->next;}if(r==*p) *p=u;else q->next=u;u->next=r;}void InsertList2(LIST **p,int item){LIST *u,*q,*r;u=(LIST*)malloc(sizeof(LIST));u->data=item;r=*p;for(r=*p;r!=NULL&&r->data<=item;q=r,r=r->next);if(r==*p) *p=u;else q->next=u;u->next=r;}int DeleteList(LIST **p,int item){LIST *q,*r;q=*p;r=q;if(q==NULL)return 1;if(q->data==item){*p=q->next;free(q);return 0;}for(;q->data!=item&&q->next!=NULL;r=q,q=q->next);if(q->data==item){r->next=q->next;free(q);return 0;}return 1;}int FindList(LIST **p,int item){LIST *q;int i;for(q=*p,i=1;q->data!=item&&q->next!=NULL;q=q->next,i++);if(q->data==item) return i;else return -1;}void OutputList(LIST **p){LIST *q;q=*p;while(q!=NULL) {printf("%d ",q->data);q=q->next;}printf("\n");}int main(){LIST *p;InitList(&p);int item,rc;printf("Insert\n");while(1){scanf("%d %d",&item,&rc);if(item==-1) break;InsertList1(&p,item,rc);OutputList(&p);}printf("Find\n");while(1){scanf("%d",&item);if(item==-1) break;printf("%d at %d\n",item,FindList(&p,item));}printf("Delete\n");while(1){scanf("%d",&item);if(item==-1) break;DeleteList(&p,item);OutputList(&p);}}

2.将一个头结点指针为a的单链表A分解成两个单链表A和B,其头结点指针分别为a和b,使得A链表中含有原链表A中序号为奇数的元素,B链表中含有原链表A中序数为偶数的元素,且保持原来的先对顺序

#include<cstdio>#include<memory>typedef struct list{int data;struct list *next;}LIST;void Output(LIST *a){while(a!=NULL){printf("%d ",a->data);a=a->next;}printf("\n");}void Input(LIST **a){LIST *u,*p;int item;while(scanf("%d",&item)&&item!=-1){u=(LIST*)malloc(sizeof(LIST));u->data=item;if(*a==NULL) {*a=u;p=u;continue;}p->next=u;p=u;}p->next=NULL;Output(*a);}void dis(LIST *a,LIST **b){LIST *r,*p,*q;*b=NULL;if(a==NULL) return;r=a;while(r!=NULL&&r->next!=NULL){p=r->next;r->next=p->next;if(*b==NULL) *b=p;else q->next=p;q=p;r=r->next;}p->next=NULL;}int main(){LIST *a,*b;a=NULL;Input(&a);Output(a);dis(a,&b);Output(a);Output(b);}

3.将链接存储线性表逆置,即最后一个结点变成第一个结点,原来的倒数第二个结点变成第二个结点,以此类推

#include<cstdio>#include<memory>typedef struct list{int data;struct list *next;}LIST;void Output(LIST *a){while(a!=NULL){printf("%d ",a->data);a=a->next;}printf("\n");}void Input(LIST **a){LIST *u,*p;int item;while(scanf("%d",&item)&&item!=-1){u=(LIST*)malloc(sizeof(LIST));u->data=item;if(*a==NULL) {*a=u;p=u;continue;}p->next=u;p=u;}p->next=NULL;}void reverse(LIST **a){LIST *q,*r,*rr;q=*a;if(q==NULL||q->next==NULL) return;rr=q->next;q->next=NULL;while(rr!=NULL){r=rr;rr=rr->next;r->next=q;q=r;}*a=r;} int main(){LIST *a,*b;a=NULL;Input(&a);Output(a);reverse(&a);Output(a);}

 

 

1.栈基本操作函数,调用基本操作函数

#include<cstdio>#define MAXN 10int push(int *stack,int maxn,int *toppt,int x){if(*toppt==maxn) return 1;stack[*toppt]=x;++(*toppt);return 0;}int pop(int *stack,int *toppt,int *cp){if(*toppt==0) return 1;--(*toppt);*cp=stack[*toppt];return 0;}void OutputStack(int *stack,int toppt){int i;for(i=toppt-1;i>=0;i--)printf("%d",stack[i]);printf("\n");}int main(){int stack[MAXN],i,top,c;top=0;printf("push\n");while(1){scanf("%d",&c);if(c==-1) break;if(push(stack,MAXN,&top,c)) printf("error\n");}printf("top %d\n",stack[top-1]);printf("pop\n");while(1){scanf("%d",&c);if(c==0) break;if(pop(stack,&top,&c)) {printf("error\n");break;}printf("%d\n",c);}OutputStack(stack,top);}

 

 

2.假设一个算术表达式中包含有圆括号,方括号和花括号3种类型的括号,编写一个判别表达式中的括号是否正确匹配的函数correct(char *exp,int max),其中,传入的参数为表达式和表达式长度

 

#include<cstdio>#include<cstring>#define MAXN 1000int push(char *stack,int maxn,int *toppt,char x){if(*toppt==maxn) return 1;stack[*toppt]=x;++(*toppt);return 0;}int pop(char *stack,int *toppt,char *cp){if(*toppt==0) return 1;--(*toppt);*cp=stack[*toppt];return 0;}void OutputStack(char *stack,int toppt){int i;for(i=toppt-1;i>=0;i--)printf("%d",stack[i]);printf("\n");}char brackets(char c){if(c==')') return '(';if(c==']') return '[';if(c=='}') return '{';}int correct(char *exp,int max){int i;char stack[MAXN];int top=0;char c;for(i=0;i<max;i++){if(exp[i]=='('||exp[i]=='['||exp[i]=='{') push(stack,MAXN,&top,exp[i]);if(exp[i]==')'||exp[i]==']'||exp[i]=='}') {c='1';pop(stack,&top,&c);if(c!=brackets(exp[i])) {return 1;break;}}}if(top!=0) return 1;return 0;}int main(){char exp[1000];while(strcmp(exp,"end")!=0){scanf("%s",exp);printf("%d\n",correct(exp,strlen(exp)));}return 0;}

3.将中缀表示的算术表达式转换成后缀表示,并计算表达式的值

#include<cstdio>#include<cstring>#define MAXN 1000int eval(char c,int opd1,int opd2){if(c=='+') return opd1+opd2;if(c=='-') return opd1-opd2;if(c=='*') return opd1*opd2;if(c=='/') return opd1/opd2;}int push1(int *stack,int maxn,int *toppt,int x){if(*toppt==maxn) return 1;stack[*toppt]=x;++(*toppt);return 0;}int pop1(int *stack,int *toppt,int *cp){if(*toppt==0) return 1;--(*toppt);*cp=stack[*toppt];return 0;}int push(char *stack,int maxn,int *toppt,char x){if(*toppt==maxn) return 1;stack[*toppt]=x;++(*toppt);return 0;}int pop(char *stack,int *toppt,char *cp){if(*toppt==0) return 1;--(*toppt);*cp=stack[*toppt];return 0;}void OutputStack(char *stack,int toppt){int i;for(i=toppt-1;i>=0;i--)printf("%d",stack[i]);printf("\n");}char brackets(char c){if(c==')') return '(';if(c==']') return '[';if(c=='}') return '{';}int operate(char *str,int *exp){char c;int opd1,opd2,temp,c1;int s[MAXN];int i;int top=0;for(i=0;str[i]!='\0';i++){c=str[i];if(c>='0'&&c<='9'){c1=c-'0';if(push1(s,MAXN,&top,c1)!=0){printf("表达式太长,栈满");return -2;}}else if(c=='+'||c=='-'||c=='*'||c=='/'){pop1(s,&top,&opd1);if(pop1(s,&top,&opd2)!=0)return -1;temp=eval(c,opd2,opd1);push1(s,MAXN,&top,temp);}else return -1;}pop1(s,&top,exp);if(top!=0)return -1;return 0;}int trans(char *sin,char *sout){char s[MAXN],c;int top=0,i=0,off=0;for(i=0;sin[i]!='\0';i++){if(sin[i]>='0'&&sin[i]<='9') sout[off++]=sin[i];else switch(sin[i]){case '(':push(s,MAXN,&top,sin[i]);break;case ')':while(pop(s,&top,&c)==0){if(c=='(') break;sout[off++]=c;}if(c!='(') printf("表达式括号不匹配");break;case '+':case '-':while(top>0&&s[top-1]!='('){pop(s,&top,&c);sout[off++]=c;}push(s,MAXN,&top,sin[i]);break;case '*':case '/':while(top>0&&s[top-1]=='*'||s[top-1]=='/'){pop(s,&top,&c);sout[off++]=c;}push(s,MAXN,&top,sin[i]);break;}}while(pop(s,&top,&c)==0)sout[off++]=c;sout[off]='\0';return 0;}int main(){int exp;char sin[MAXN];char sout[MAXN];scanf("%s",sin);trans(sin,sout);printf("%s\n",sout);operate(sout,&exp);printf("%d\n",exp);}

 

 

 

1.链接队列基本操作函数,调用基本操作函数

#include<cstdio>#include<memory>typedef struct queue{int data;struct queue *link;}QUEUE;void EnQueue(QUEUE **head,QUEUE **tail,int x){QUEUE *u;u=(QUEUE*)malloc(sizeof(QUEUE));u->data=x;u->link=NULL;if(*head==NULL) {*head=u;*tail=u;return;}(*tail)->link=u;(*tail)=u;}int DeQueue(QUEUE **head,QUEUE **tail,int *cp){QUEUE *p;p=*head;if(*head==NULL) return -1;*cp=(*head)->data;*head=(*head)->link;if(*head==NULL) *tail=NULL;free(p);return 0;}void OutputQueue(QUEUE *head){while(head!=NULL){printf("%d ",head->data);head=head->link;}printf("\n");}int main(){QUEUE *head,*tail;int op,i;head=tail=NULL;printf("EnQueue\n");while(1){scanf("%d",&i);if(i==-1) break;EnQueue(&head,&tail,i);OutputQueue(head);}printf("the first i is %d\n",head->data);while(1){scanf("%d",&i);if(i==-1) break;DeQueue(&head,&tail,&i);printf("%d\n",i);OutputQueue(head);}return 0;}


 


 2.环形队列基本操作函数,调用基本操作函数

#include<cstdio>#define MAXN 11int EnQueue(int *queue,int maxn,int *head,int *tail,int x){if((*tail+1)%maxn==*head)return 1;*tail=(*tail+1)%maxn;queue[*tail]=x;return 0;}int DeQueue(int *queue,int maxn,int *head,int *tail,int *cp){if(*head==*tail)return 1;*head=(*head+1)%maxn;*cp=queue[*head];return 0;}void OutputQueue(int *queue,int maxn,int h,int t){while(h!=t){h=(h+1)%maxn;printf("%d ",queue[h]);}printf("\n");}void main(){int q[MAXN];int q_h=0,q_t=0;int i;printf("EnQueue\n");while(1){scanf("%d",&i);if(i==-1) break;EnQueue(q,MAXN,&q_h,&q_t,i);OutputQueue(q,MAXN,q_h,q_t);}printf("the first i is %d\n",q[(q_h+1)%MAXN]);while(1){scanf("%d",&i);if(i==-1) break;DeQueue(q,MAXN,&q_h,&q_t,&i);printf("%d\n",i);OutputQueue(q,MAXN,q_h,q_t);}printf("EnQueue\n");while(1){scanf("%d",&i);if(i==-1) break;EnQueue(q,MAXN,&q_h,&q_t,i);OutputQueue(q,MAXN,q_h,q_t);}while(1){scanf("%d",&i);if(i==-1) break;DeQueue(q,MAXN,&q_h,&q_t,&i);printf("%d\n",i);OutputQueue(q,MAXN,q_h,q_t);}}

 3.医务室模拟 假设只有一个医生,在一段时间内随机地来了几个病人,假设病人到达的时间间隔为0~14分钟之间的某个随机值,每个病人所需的处理时间为1~9分钟之间的某个随机值,试用队列结构进行模拟。要求输出医生的总等待时间和病人的平均等待时间。

#include<stdio.h>#include<stdlib.h>#include<memory>using namespace std;typedef struct {int arrive;int treat;}PATIENT;typedef struct queue{PATIENT data;struct queue *link;}QUEUE;void EnQueue(QUEUE **head,QUEUE **tail,PATIENT x){QUEUE *u;u=(QUEUE*)malloc(sizeof(QUEUE));u->data=x;u->link=NULL;if(*head==NULL){*head=u;*tail=u;return;}(*tail)->link=u;(*tail)=u;}void DeQueue(QUEUE **head,QUEUE **tail,PATIENT *cp){QUEUE *p;p=*head;if(*head==NULL) return;*cp=(*head)->data;*head=(*head)->link;if(*head==NULL) *tail=NULL;free(p);return;}void OutputQueue(QUEUE *head){while(head!=NULL){printf("%d %d",head->data.arrive,head->data.treat);head=head->link;}}void InitData(PATIENT *pa,int n){int parr=0;int i;for(i=0;i<n;i++){pa[i].arrive=parr+rand()%15;pa[i].treat=rand()%9+1;parr=pa[i].arrive;}}void main(){QUEUE *head,*tail;PATIENT *p,curr;int n,i,finish;int nowtime;int dwait,pwait;head=tail=NULL;while(1){scanf("%d",&n);nowtime=dwait=pwait=0;if(n==0) return;if(n>20||n<0) continue;p=(PATIENT*)malloc(n*sizeof(PATIENT));InitData(p,n);for(i=0;i<n||head!=NULL;){if(head==NULL){if(p[i].arrive-nowtime>0)dwait+=p[i].arrive-nowtime;nowtime=p[i].arrive;EnQueue(&head,&tail,p[i++]);}DeQueue(&head,&tail,&curr);pwait+=nowtime-curr.arrive;finish=nowtime+curr.treat;while(i<n&&p[i].arrive<=finish)EnQueue(&head,&tail,p[i++]);nowtime=finish;}free(p);printf("%d %.2f\n",dwait,(float)pwait/n);}}

 

 

1.已知Fibonacci数列为0,1,1,2,3,……,即

Fib(0)=0;

Fib(1)=1;

Fib(n)=Fib(n-1)+Fib(n-2)

试编程求Fibonacci数列中的前20项

#include<cstdio>int main(){int i;int fib[20];fib[0]=1;fib[1]=1;for(i=2;i<20;i++)fib[i]=fib[i-1]+fib[i-2];for(i=0;i<20;i++){printf("%5d",fib[i]);if((i+1)%5==0) printf("\n");}}

2.试通过循环,按行顺序为一个5*5的二维数组A赋1~25的自然数,然后输出该数组的左下半三角

#include<cstdio>#define MAX 5int main(){int i,j,n=1;int a[MAX][MAX];for(i=0;i<MAX;i++)for(j=0;j<MAX;j++)a[i][j]=n++;for(i=0;i<MAX;i++){for(j=0;j<=i;j++)printf("%d\t",a[i][j]);printf("\n");}}

 

1.二叉树基本操作函数,调用基本操作函数

#include<cstdio>#include<memory>#include <windows.h>#include <conio.h>typedef struct tree{int data;struct tree *lchild;struct tree *rchild;}TREE;typedef struct stack{TREE *t;int flag;struct stack *link;}STACK;void gotoxy(int x, int y){COORD pos;pos.X = x - 1;pos.Y = y - 1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}void push(STACK **top,TREE *tree){STACK *p;p=(STACK*)malloc(sizeof(STACK));p->t=tree;p->link=*top;*top=p;}void pop(STACK **top,TREE **tree){STACK *p;if(*top==NULL)*tree=NULL;else{*tree=(*top)->t;p=*top;*top=(*top)->link;free(p);}}void SearchNode(TREE *tree,int key,TREE **pkpt,TREE **kpt){*pkpt=NULL;*kpt=tree;while(*kpt!=NULL){if((*kpt)->data==key)return;*pkpt=*kpt;if(key<(*kpt)->data)*kpt=(*kpt)->lchild;else*kpt=(*kpt)->rchild;}}int InsertNode(TREE **tree,int key){TREE *p,*q,*r;SearchNode(*tree,key,&p,&q);if(q!=NULL) return 1;if((r=(TREE*)malloc(sizeof(TREE)))==NULL)return -1;r->data=key;r->lchild=r->rchild=NULL;if(p==NULL)*tree=r;else if(p->data>key)p->lchild=r;else p->rchild=r;return 0;}int DeleteNode(TREE **tree,int key){TREE *p,*q,*r;SearchNode(*tree,key,&p,&q);if(q==NULL)return 1;if(p==NULL)if(q->lchild==NULL)*tree=q->rchild;else{*tree=q->lchild;r=q->lchild;while(r->rchild!=NULL)r=r->rchild;r->rchild=q->rchild;}else if(q->lchild==NULL)if(q==p->lchild)p->lchild=q->rchild;else p->rchild=q->rchild;else{r=q->lchild;while(r->rchild!=NULL)r=r->rchild;r->rchild=q->rchild;if(q==p->lchild)p->lchild=q->lchild;else p->rchild=q->lchild;}free(q);return 0;}void OutputTree(TREE *tree){STACK *top;int deep=0,no=0,maxdeep=0;top=NULL;while(tree!=NULL||top!=NULL){while(tree!=NULL){push(&top,tree);top->flag=++deep;if(maxdeep<deep)maxdeep=deep;tree=tree->lchild;}if(top!=NULL){deep=top->flag;no++;pop(&top,&tree);gotoxy(no*4,deep+2);printf("%d",tree->data);fflush(stdout);tree=tree->rchild;}}gotoxy(1,maxdeep+3);//printf("what the fuck\n");getch();}void main(){TREE *t;int n;t=NULL;printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");while(1){scanf("%d",&n);if(n==0) break;InsertNode(&t,n);}gotoxy(100,100);OutputTree(t);while(1){scanf("%d",&n);if(n==0) break;DeleteNode(&t,n);}gotoxy(100,100);OutputTree(t);}


2.已知以二叉树链表作为存储结构,试编写按中序遍历并打印二叉树的算法

 

void OutputTree(TREE *tree){STACK *top;int deep=0,no=0,maxdeep=0;top=NULL;while(tree!=NULL||top!=NULL){while(tree!=NULL){push(&top,tree);top->flag=++deep;if(maxdeep<deep)maxdeep=deep;tree=tree->lchild;}if(top!=NULL){deep=top->flag;no++;pop(&top,&tree);gotoxy(no*4,deep+2);printf("%d",tree->data);fflush(stdout);tree=tree->rchild;}}gotoxy(1,maxdeep+3);//printf("what the fuck\n");getch();}

 

 

1.二叉树遍历操作函数

void re_preorder(TREE *tree){if(tree!=NULL){printf("%d ",tree->data);re_preorder(tree->lchild);re_preorder(tree->rchild);}}void re_midorder(TREE *tree){if(tree!=NULL){re_midorder(tree->lchild);printf("%d ",tree->data);re_midorder(tree->rchild);}}void re_posorder(TREE *tree){if(tree!=NULL){re_posorder(tree->lchild);re_posorder(tree->rchild);printf("%d ",tree->data);}}void st_preorder(TREE *tree){STACK *top;top=NULL;while(tree!=NULL){printf("%d ",tree->data);if(tree->rchild!=NULL)push(&top,tree->rchild);if(tree->lchild!=NULL)push(&top,tree->lchild);pop(&top,&tree);}}void st_midorder(TREE *tree){STACK *top;top=NULL;while(tree!=NULL||top!=NULL){while(tree!=NULL){push(&top,tree);tree=tree->lchild;}if(top!=NULL){pop(&top,&tree);printf("%d ",tree->data);tree=tree->rchild;}}}void st_posorder(TREE *tree){STACK *top;top=NULL;do{while(tree!=NULL){push(&top,tree);top->flag=0;tree=tree->lchild;}if(top!=NULL){while(top!=NULL&&top->flag){pop(&top,&tree); printf("%d ",tree->data);}if(top!=NULL){top->flag=1;tree=(top->t)->rchild;}}}while(top!=NULL);}

 

2.根据Huffman编码的原理编写一个程序,在用户输入结点权重的基础上建立它的Huffman编码。

#include<cstdio>#define MAX 21typedef struct {char data;int weight;int parent;int left;int right;}huffnode;typedef struct {char cd[MAX];int start;}huffcode;void main(){huffnode ht[2*MAX];huffcode hcd[MAX],d;int i,k,f,j,r,n=0,c,m1,m2;scanf("%d",&n);for(i=0;i<n;i++){fflush(stdin);scanf("%c",&ht[i].data);scanf("%d",&ht[i].weight);}for(i=0;i<2*n-1;i++)ht[i].parent=ht[i].left=ht[i].right=0;for(i=n;i<2*n-1;i++){m1=m2=0x7fff;j=r=0;for(k=0;k<i;k++)if(ht[k].parent==0)if(ht[k].weight<m1){m2=m1;r=j;m1=ht[k].weight;j=k;}else if(ht[k].weight<m2){m2=ht[k].weight;r=k;}ht[j].parent=i;ht[r].parent=i;ht[i].weight=ht[j].weight+ht[r].weight;ht[i].left=j;ht[i].right=r;}for(i=0;i<n;i++){d.start=n;c=i;f=ht[i].parent;while(f!=0){if(ht[f].left==c)d.cd[--d.start]='0';elsed.cd[--d.start]='1';c=f;f=ht[f].parent;}hcd[i]=d;}for(i=0;i<n;i++){printf("%c",ht[i].data);for(k=hcd[i].start;k<n;k++)printf("%c",hcd[i].cd[k]);printf("\n");}}


 

 1.图基本操作函数,调用基本操作函数

#include<cstdio>#include<memory>#define MAX 30typedef struct node{int vno;struct node *next;}edgeNode;typedef edgeNode *lgraph[MAX];typedef int mgraph[MAX][MAX];int visited[MAX];int queue[MAX];int creat_graph(lgraph lg,mgraph mg){int vn,en,k,i,j;edgeNode *p;scanf("%d %d",&vn,&en);for(k=0;k<vn;k++)lg[k]=NULL;for(k=0;k<vn;k++)for(i=0;i<vn;i++)mg[k][i]=0;for(k=0;k<en;){i=j=-1;scanf("%d %d",&i,&j);k++;i--;j--;p=(edgeNode*)malloc(sizeof(edgeNode));p->vno=j;p->next=lg[i];lg[i]=p;p=(edgeNode*)malloc(sizeof(edgeNode));p->vno=i;p->next=lg[j];lg[j]=p;mg[i][j]=1;}return vn;}void ldfs(lgraph g,int i){edgeNode *t;printf("%4d",i+1);visited[i]=1;t=g[i];while(t!=NULL){if(!visited[t->vno])ldfs(g,t->vno);t=t->next;}}void mdfs(mgraph g,int i,int vn){int j;printf("%4d",i+1);visited[i]=1;for(j=0;j<vn;j++){if(g[i][j]&&!visited[j])mdfs(g,j,vn);}}void lbfs(lgraph g,int s,int n){int i,v,w,head,tail;edgeNode *t;for(i=0;i<n;i++)visited[i]=0;head=tail=0;printf("%4d",s+1);visited[s]=1;queue[tail++]=s;while(head<tail){v=queue[head++];for(t=g[v];t!=NULL;t=t->next){w=t->vno;if(!visited[w]){printf("%4d",w+1);visited[w]=1;queue[tail++]=w;}}}}void mbfs(mgraph g,int s,int n){int i,j,v,head,tail;for(i=0;i<n;i++)visited[i]=0;head=tail=0;printf("%4d",s+1);visited[s]=1;queue[tail++]=s;while(head<tail){v=queue[head++];for(j=0;j<n;j++){if(g[v][j]&&!visited[j]){printf("%4d",j+1);visited[j]=1;queue[tail++]=j;}}}}void main(){lgraph lg;mgraph mg;int n,i;n=creat_graph(lg,mg);for(i=0;i<n;i++)visited[i]=0;ldfs(lg,0);printf("\n");for(i=0;i<n;i++)visited[i]=0;mdfs(mg,0,n);printf("\n");lbfs(lg,0,n);printf("\n");mbfs(mg,0,n);}

2.

0 0