h>
typedef struct dxs{
int a; struct dxs *next;
}Dxs, *Dxss;
void Structure( Dxss head, int n );
void Show(Dxss head);
void Add( Dxss head1, Dxss head2, Dxss head3 );
void frees( Dxss head );
void main() {
Dxss ha,hb,hc;
int n;ha=(Dxss)malloc(sizeof(Dxs));
hb=(Dxss)malloc(sizeof(Dxs));
hc=(Dxss)malloc(sizeof(Dxs));
printf("请输入多项式1的项数\n”);scanf(”%d”,&n);
Structure(ha,n); printf("请输入多项式2的项数\n");
scanf("%d",&n);Structure(hb,n);
Add(ha,hb,hc);printf(”多项式HC的式子是\n”);
Show(hc); frees(ha); frees(hb); frees(hc); printf(”\n\n”);}
void Structure(Dxss head,int n){
Dxss p,q;int a;
printf("请输入要录入系统的多项式的系数,从次数较小的开始\n”);
p=head;do{scanf("%d",&a);q=(Dxss)malloc(sizeof(Dxs));q->a=a;p-〉next=q;
q-〉next=NULL;p=q;
}while(——n);
}
void Show(Dxss head){
int m=0;Dxss p;p=head—>next;while(p!
=NULL){
printf(”%d*X^%d",p—>a,m++);p=p—>next;if(p!
=NULL)printf(”+");
}
printf(”n");}
void Add(Dxss head1,Dxss head2,Dxss head3){
Dxss p,q,l,m;p=head1—>next;q=head2—〉next;l=head3;
while((p!
=NULL)&&(q!
=NULL)){
m=(Dxss)malloc(sizeof(Dxs));
m—>a=p—〉a;l—〉next =m;l=m;p=p->next;
}
l—>next =NULL;}
void frees(Dxss head){
Dxss p,q;p=head;while(p!
=NULL)
{q=p;p=p—〉next;free(q);
}}
实验结果:
心得:
通过这次试验让我认识到了要注意细节,否则很容易出错
实验名称
实验二树
实验目的和要求
熟悉树的各种表示方法和各种遍历方式,掌握有关算法的实现,了解树在计算机科学及其它工程技术中的应用。
实验内容
[问题描述]
任意给定一棵二叉树。
试设计一个程序,在计算机中构造该二叉树,并对它进行遍历.
[输入]
一棵二叉树的结点若无子树,则可将其子树看作“。
”,输入时,按照前序序列的顺序输入该结点的内容。
对下图,其输入序列为ABD.。
EH。
。
。
CF.I.。
G。
。
。
[输出]
若为空二叉树,则输出:
THIS IS A EMPTY BINARY TREE。
若二叉树不空,按后序序列输出,对上例,输出结果为:
DHEBIFGCA.
[存储结构]
采用二叉链表存储。
实习题
1编写递归算法,计算二叉树中叶子结点的数目。
2.编写递归算法,在二叉树中求位于先序序列中第K个位置的结点.
3.将上述例题用非递归程序实现。
主要仪器设备
台式或笔记本计算机
实验记录(可分栏或加页)
#include〈stdio。
h>
#include〈stdlib。
h〉
#include〈malloc。
h〉
int count=0;
struct node{
char info; struct node *llink,*rlink;
};
typedef struct node NODE;
NODE *creat(){char x; NODE *p; scanf(”%c",&x); printf("%c”,x);
if(x!
='.'){ p=(NODE*)malloc(sizeof(NODE));
p-〉info=x; p-〉llink=creat(); p—〉rlink=creat(); }
else
p=NULL; return p; }
void run(NODE*t){
if(t){ run(t-〉llink);run(t—>rlink); printf(”%c",t—>info);
if(((t-〉llink )=NULL)&&((t-〉rlink)=NULL))
count++;} }
void main(){
NODE *T; printf("qing shu ru er cha shu:
\n”);
T=creat(); printf("\n”); if(!
T)
printf("This is a empty binary tree”);
else
{printf(”The result of post travese is:
\n”);run(T);
}
printf(”总共有叶子结点数%d”,count);
printf("\n");}
实验结果:
心得:
树是数据结构非常重要的部分,要很好地掌握
实验名称
实验三图
实验目的和要求
熟悉图的存储结构,掌握有关算法的实现,了解图在计算机科学及其他工程技术中的应用。
实验内容
采用邻接表存储结构,编写一个求无向图的连通分量个数的算法。
主要仪器设备
台式或笔记本计算机
实验记录
#include〈stdio。
h>
#include
int n;
struct VNode{
int position;struct VNode*next;};
struct ArcNode{int mark;struct VNode*first;};
void DFS(structArcNode*v,struct ArcNode*w){
struct VNode*L;w—〉mark=1;L=w-〉first;while(L!
=NULL){
if((v+(L—〉position))->mark==0){
DFS(v,(v+L->position));}
L=L—>next;}}
int main(){int i,j,k;int num=0;struct ArcNode*p;struct VNode*temp;
struct VNode*flag;printf("\n请输入顶点个数n:
");scanf("%d",&n);
while(n〈1){printf("你输入的值不合理,请重新输入:
\n");
scanf(”%d",&n);}
p=(struct ArcNode*)malloc(n*sizeof(struct ArcNode));
for(i=0;i〈n;i++){
printf("\n请输入以V%d为弧尾的所有弧,并以-1结束输入\n”,i+1);
scanf(”%d",&k);if(k==-1){
p[i]。
mark=0;p[i].first=NULL;}
else{
temp=(struct VNode*)malloc(sizeof(struct VNode));
temp-〉position=k;temp-〉next=NULL;p[i].first=temp;
p[i].mark=0;flag=temp;scanf("%d",&k);while(k!
=—1){
temp=(struct VNode*)malloc(sizeof(struct VNode));
temp-〉position=k;temp->next=NULL;flag->next=temp;
flag=temp;scanf("%d",&k);
}}}
i=0;while(p[i]。
mark==0){
DFS(p,(p+i));num++;i=0;
while(p[i]。
mark!
=0&&i}}
printf("此图的连通分量个数为:
%d\n”,num);
system(”pause");return 0;
}
实验结果:
心得:
图比较复杂,要注意算法的复杂度,减少程序代码
实验名称
实验四查找
实验目的和要求:
实验内容
主要仪器设备
台式或笔记本计算机
实验记录(可分栏或加页)
#include”stdio.h”
typedefstruct{
inta[30];intlength;
}sqtable;
sqtablest;intb=0;
voidcreatest(intk)
{inti;printf("Pleaseinputdata:
”);st。
a[0]=—100;
for(i=1;(!
b&&(i<=k));i++)
{scanf(”%d”,&(st.a[i]));
if(st.a[i]〈st.a[i-1]){printf(”Inputdataerror.\n");b=1;
}}
if(!
b){st。
length=k;
printf(”Thetableisbuilted。
\n”);
}}
intstfind(sqtablest,intl,inth,inty)
{intm;while(l〈=h)
{m=(l+h)/2;if(y==st。
a[m]);returnm;
elseif(ya[m])returnstfind(st,l,m—1,y);
elsereturnstfind(st,m+1,h,y);}}
main(){intn,x,l,h,s;
l=1;h=st.length;printf(”\nPleaseinputn:
");scanf("%d”,&n);
createst(n);h=st。
length;if(b==0)
{printf(”Pleaseinputyouwantfindvalue:
");
scanf(”%d”,&x);s=stfind(st,l,h,x);}
printf("Find%dinposition%d.\n",x,s);
}
实验结果:
心得:
本次试验让我明白了查找的重要性,在查找过程中要注意程序的时间复杂度及循环次数。
实验名称
实验五排序
实验目的和要求:
通过本次实验,掌握线性表的排序方法,并分析时间复杂度.
实验内容
设计一个用链表表示的直接选择排序算法,并用程序实现。
算法说明:
已知待排序初始序列用单链表存贮,头指针head指向第一个结点,从这个待排序列中找出最小结点,插入head之后,用r来指示.r以前为已排序序列,r以后为未排序序列。
再从未排序序列中找出最小结点插入r的后面,让r指向这个结点。
反复执行这个过程,直到排好序.
主要仪器设备
台式或笔记本计算机
实验记录(可分栏或加页)
#include〈stdio.h〉
voidSort(int*a,intm){
inti=0;intj=m-1;inttemp;
while(i〈=j){
while(i〈m&&a[i]>=0){i++;}
while(j〉=0&&a[j]〈0){j--;}
if(i〈j){
temp=a[i];a[i]=a[j];a[j]=temp;
i++;j--;
}}}
intmain(){
intn,i;int*p;printf(”你有多少个数据有待整序(必须大于1):
\n");
scanf(”%d”,&n);while(n〈2){
printf("你输入的数值不合理,请重新输入:
\n”);scanf(”%d”,&n);}
p=(int*)malloc(n*sizeof(int));printf("请输入数据:
\n");
for(i=0;iSort(p,n);printf(”整序后的数据为:
\n");
for(i=0;iprintf("\n");system(”pause");return0;
}
实验结果:
心得:
排序过程中应注意细节,循环次数,细节决定成败