实验四.稀疏矩阵的三元组顺序表示方法及基本操作的实现(建立、输出、转置)并实现一个主菜单来实现。
#include<stdio.h>#define maxsize 50#define maxrow 10#define maxcol 10typedef struct {int i,j;int data;}triple;typedef struct {triple elem[maxsize+1];int mu,nu,tu;}tsmatrix;void createsmatrix (tsmatrix *t){t=(tsmatrix*)malloc(sizeof(tsmatrix));if(!t) exit(0); t->mu=0; t->nu=0; t->tu=0;}void transposesmatrix(tsmatrix m,tsmatrix*t){int q=1,col,p;t->mu=m.nu;t->nu=m.mu;t->tu=m.tu;if(t->tu){q=1;for(col=1;col<=m.nu;col++)for(p=1;p<=m.tu;++p)if(m.elem[p].j==col){t->elem[q].i=m.elem[p].j;t->elem[q].j=m.elem[p].i;t->elem[q].data=m.elem[p].data;++q;}}}void scanftsmatrix(tsmatrix *t){int count;printf("enter the nums of matrix's rows:\n");scanf("%d",&t->mu);printf("enter the nums of matrix's columns:\n");scanf("%d",&t->nu);printf("enter the nums of matrix's datas:\n");scanf("%d",&t->tu);for(count=1;count<=t->tu;count++){printf("enter the non-zero nums'row,column,data:\n");scanf("%d%d%d",&t->elem[count].i,&t->elem[count].j,&t->elem[count].data); }}/*void printftsmatrix(tsmatrix*t){int counter,col;printf("the xishu juzheng is :\n");for(counter=1;counter<=t->tu;){for(col=1;col<=t->nu;col++){if(t->elem[counter].j==col) {printf("%d\t",t->elem[counter].data);counter++;if(t->elem[counter].i==t->elem[counter-1].i) ;else if(col==t->nu) printf("\n");else {for(col++;col<=t->nu;col++)printf("*\t");printf("\n"); }}else printf("*\t");}}}*/void printftsmatrix(tsmatrix*t){int counter,a[maxrow][maxcol]={0},row,col;int*p;p=&a;printf("the sparse matrix is :\n");for(counter=1;counter<=t->tu;counter++){a[t->elem[counter].i-1][t->elem[counter].j-1]=t->elem[counter].data;}for(row=1;row<=t->mu;row++){for(col=1;col<=t->nu;col++){printf("%d\t",*p);p++;}printf("\n");p=p+maxcol-col+1;}}void main(){tsmatrix T,M;int x;char character/*zifu*/,n;createsmatrix(&T);scanftsmatrix(&T);while(character!='#'){printf("choice:1--transposesmatrix;2--printftsmatrix\n");printf("enter the num for your choice\n");scanf("%d",&x);switch(x){case 1:transposesmatrix(T,&M);printftsmatrix(&M);break;case 2:printftsmatrix(&T);break;}n=getchar();printf("enter one char'#' for your choice to quit:\n");scanf("%c",&character);getch();}}实验五树及二叉树实验1)按先序次序输入二叉树中结点的值,建立一棵以二叉链表作存储结构的二叉树,后按先序、中序、后序顺序分别遍历这棵二叉树。
2)编写一个求二叉树叶子结点数的算法。
3)交换左右孩子。
#include <stdio.h>#define NULL 0typedef struct bitnode{int data;struct bitnode *lchild, *rchild;}bitnode,*bitree;void createbitree(bitree *t){char ch,zifu;scanf("%c",&ch);zifu=getchar();if(ch=='#') *t=NULL;else{if(!((*t)=(bitree)malloc(sizeof(bitnode)))) ;/* if(!(*t=(bitnode*)malloc(sizeof(bitnode)))) ;*/(*t)->data=ch;printf("enter the lchild\n");createbitree(&((*t)->lchild));printf("enter the rchild\n");createbitree(&((*t)->rchild));}}void preorder(bitree *t){if(*t!=NULL){printf("%c\t",(*t)->data);preorder(&((*t)->lchild));preorder(&((*t)->rchild));}}void inorder(bitree *t){if(*t!=NULL){inorder(&((*t)->lchild));printf("%c\t",(*t)->data);inorder(&((*t)->rchild));}}void postorder(bitree *t){if(*t!=NULL){postorder(&((*t)->lchild));postorder(&((*t)->rchild));printf("%c\t",(*t)->data);}}int leafcount(bitree *t){int num,num1,num2;if(*t==NULL) num=0;else if(((*t)->lchild==NULL)&&((*t)->rchild==NULL)) num=1;else {num1=leafcount(&(*t)->lchild);num2=leafcount(&(*t)->rchild);num=num1+num2;}return num;}void changeleaf(bitree *t){bitree p,q;if(*t!=NULL){p=(*t)->rchild;(*t)->rchild=(*t)->lchild;(*t)->lchild=p;changeleaf(&((*t)->lchild));changeleaf(&((*t)->rchild));}}void instruction(void){printf("\nEnter your choice:\n""1 to preorder\n""2 to inorder\n""3 to postorder\n""4 to change the leaf\n""5 to leafcount\n""6 to end\n");}void main(){bitree t;int choice;printf("Enter the root of the tree\n");createbitree(&t);instruction();scanf("%d",&choice);while (choice!=6){switch (choice){case 1:printf("The perorder of the tree is:\n");preorder(&t);instruction();scanf("%d",&choice);break;case 2:printf("The ororder is:\n");inorder(&t);instruction();scanf("%d",&choice);break;case 3:printf("The postorder is:\n");postorder(&t);instruction();scanf("%d",&choice);break;case 5:printf("The leafs is %d\n",leafcount(&t));instruction();scanf("%d",&choice);break;case 4:changeleaf(&t);printf("After change the tree is:\n");preorder(&t);instruction();scanf("%d",&choice);break;default:printf("Error\n");break;}}printf("End of run\n");getch();}实验六1)建立无向图和有向图的邻接矩阵存储,计算顶点的度,并按要求输出图的基本信息。