中南民族大学数据结构实验文档格式.docx
《中南民族大学数据结构实验文档格式.docx》由会员分享,可在线阅读,更多相关《中南民族大学数据结构实验文档格式.docx(40页珍藏版)》请在冰豆网上搜索。
原
理
(
算
法
流
程
)
#include<
stdio.h>
#defineNULL0
structstudent
{
intnum;
intage;
intsex;
structstudent*next;
};
/*structstudent*creat()
{
structstudent*head,*p,*q;
longx;
q=head=(structstudent*)malloc
(sizeof(structstudent));
head->
next=NULL;
printf("
\npleaseinputdatdstothelist:
"
);
scanf("
%ld"
&
x);
while(x!
=0)
p=(structstudent*)malloc
p->
num=x;
q->
next=p;
q=p;
}
return(head);
}*/
voidshow(void)
{printf("
1...............insert\n"
2...............print\n"
3...............delete\n"
4...............save\n"
5...............filein\n"
6...............exit\n"
input1..6:
voidinsert(structstudent*head)
structstudent*p,*q,*t;
p=(structstudent*)malloc(sizeof(structstudent));
%d"
p->
num);
\npleaseinputthestudent'
sage:
age);
ssex.1isman,0iswoman:
sex);
q=head;
snumbleyouwanttoinsert:
//while(q->
next!
=NULL&
&
next->
num<
num)
while(q->
(q->
num&
age&
sex))
q=q->
next;
next=q->
voidprint(structstudent*head)
structstudent*p;
inti=1;
p=head->
if(p==NULL)
thelistisNULL!
\n"
while(p!
=NULL)
if(p->
sex==1)
{
\nTHe%dstudentisaboy,hisnumbleis:
i,p->
printf("
andheis%dyearsold!
p->
}
else
\nTHe%dstudentisagirl,hernumbleis:
%6ld"
andsheis%dyearsold!
p=p->
i++;
}
voiddel(structstudent*head)
structstudent*p,*q;
longnum;
\npleaseinputthestudent’numyouwanttodelete:
while(num!
=p->
{q=p;
if(num==p->
{q->
next=p->
free(p);
else
\n%ldisnotfound!
num);
voidsave(structstudent*head)
{structstudent*p1;
FILE*fp;
charfile[20]="
d:
\\aa.txt"
;
filename:
%s\n"
file);
Pleasewait!
if((fp=fopen(file,"
wb"
))==NULL)
can'
topenfile:
%s"
exit(0);
p1=head->
while(p1!
{fwrite((void*)p1,sizeof(structstudent),1,fp);
p1=p1->
fclose(fp);
structstudent*filein(void)
{structstudent*p1,*p2,*head;
inti=0;
rb"
head=p2=(structstudent*)malloc(sizeof(*p1));
p1=(structstudent*)malloc(sizeof(*p1));
while(fread(p1,sizeof(*p1),1,fp)==1)
{i++;
p2->
next=p1;
p2=p1;
p1->
free(p1);
if(i==0){printf("
Norecoredin%s!
exit(0);
voidmain()
{inta;
structstudent*la;
/*la=creat();
*/
la=(structstudent*)malloc(sizeof(structstudent));
la->
while
(1)
{show();
a);
switch(a)
{case1:
insert(la);
break;
case2:
print(la);
break;
case3:
del(la);
case4:
save(la);
case5:
la=filein();
case6:
exit
(1);
4班
计算机科学与技术
表达式求值
1、掌握栈的特性,能利用栈的特性进行实际应用。
2、掌握“运算符优先法”,并能利用该算法对表达式求值。
1、求一个表达式的值:
输入一个包含“+”、“-”、“*”、“/”、正整数和圆括号的合法表达式,计算该表达方式的运算结果。
2、有能力的同学可以考虑操作数为多位浮点数的情况。
3、所写源代码编程风格良好,有详细注释。
4、程序运行界面良好。
typedefstruct
charS[20];
inttop;
}S1;
intS[20];
}S2;
voidInitStack(S1*S)
S->
top=-1;
voidInitStack(S2*S)
{
chargettop1(S1*st)
if(st->
top==-1)return('
!
'
elsereturnst->
S[st->
top];
intgettop2(S2*st)
intopereta(inta,unsignedcharop,intb)
switch(op)
case'
+'
:
returna+b;
-'
returna-b;
*'
returna*b;
/'
returna/b;
default:
return0;
}
}
unsignedcharA[7][7]={//表3.1算符间的优先关系
'
>
'
<
'
='
};
charb[7]={'
'
'
('
)'
#'
intsearch(charc)
while(c!
=b[i])i++;
returni;
charprecede(charc1,charc2)
inti,j;
i=search(c1);
j=search(c2);
returnA[i][j];
voidpush1(S1*st,charc)
st->
top++;
top]=c;
voidpush2(S2*st,intc)
charpop1(S1*st)
charc;
if(st->
c=st->
top--;
returnc;
intpop2(S2*st)
intc;
top==-1)return(0);
boolIns(charch)//判断数字
if(ch>
=48&
ch<
=57)return
(1);
elsereturn(0);
intGetNumber(char*ch)//转化数码
return(*ch-48);
EvaluateExpression()
S1OPTR;
S2OPND;
inta,b,x,n,m;
charc,op;
InitStack(&
OPTR);
OPND);
push1(&
OPTR,'
请输入表达式:
c=getchar();
while(c!
||gettop1(&
OPTR)!
)
if(Ins(c))//(c<
9'
c>
0'
{
n=c-48;
c=getchar();
while((Ins(c)))
{
c=c-48;
n=n*10+c;
c=getchar();
}
printf("
%d\n"
n);
push2(&
OPND,n);
//c=getchar();
}
/*if(Ins(c))一位数字的运算
{
n=c-48;
}*/
switch(precede(gettop1(&
OPTR),c))
case'
push1(&
OPTR,c);
break;
x=pop1(&
op=pop1(&
b=pop2(&
a=pop2(&
push2(&
OPND,opereta(a,op,b));
returngettop2(&
voidmain()
intm;
m=EvaluateExpression();
结果是:
%d\n"
m);
稀疏矩阵加法运算
1、掌握稀疏矩阵的压缩存储存储方法。
2、能利用三元组顺序表表示并实现稀疏矩阵。
3、会利用三元组顺序表实现矩阵的加法运算。
1、进行加法运算的两个矩阵由用户输入。
并且用三元组顺序表表示。
2、程序首先判断两个矩阵是否能够相加。
若能,在进行运算后在屏幕上现实结果,否则给出相应信息。
3、程序应能得到正确结果,用户界面显示良好。
4、所写源代码编程风格良好,有详细注释。
#include<
#defineMAXSIZE20//定义非零元的最大数目
#defineMAXMN500
#defineERROR0
#defineOK1
typedefstruct//定义一个三元组
inti,j;
//非零元行号、列号
inte;
//非零元的值
}Triple;
typedefstruct//定义稀疏矩阵
Tripledata[MAXSIZE+1];
//三元组表
intrpos[MAXMN+1];
intmu,nu,tu;
//稀疏矩阵行、列数、非零元个数
}RLSMatrix;
voidInputSMatrix(RLSMatrix*S)
inti,j,k,num;
k=1;
for(i=1;
i<
=S->
mu;
i++)
for(j=1;
j<
nu;
j++)
scanf("
\t%d"
if(num)
S->
data[k].i=i;
data[k].j=j;
data[k].e=num;
k++;
S->
tu=k-1;
intMultSMatrix(RLSMatrixM,RLSMatrixN,RLSMatrix*Q)
intbrow,ccol,p,q,tp,t,arow;
intctemp[10];
Q->
mu=M.mu;
nu=N.nu;
tu=0;
if(M.nu!
=N.mu)
if(M.tu*N.tu!
=0)//Q是非零矩阵
{
for(arow=1;
arow<
=M.mu;
++arow)
for(ccol=1;
ccol<
=N.nu;
ccol++)
ctemp[ccol]=0;
Q->
rpos[arow]=Q->
tu+1;
if(arow<
M.mu)
tp=M.rpos[arow+1];
returnERROR;
else
{
tp=M.tu+1;
for(p=M.rpos[arow];
p<
tp;
++p)
brow=M.data[p].j;
if(brow<
N.mu)
t=N.rpos[brow+1];
else
{
t=N.tu+1;
for(q=N.rpos[brow];
q<
t;
++q)
ccol=N.data[q].j;
//乘积元素在Q中列号
ctemp[ccol]+=M.data[p].e*N.data[q].e;
}//forq
}//求得Q中第crow(=arow)行的非零元
for(ccol=1;
ccol<
=Q->
++ccol)
if(ctemp[ccol])
if(++Q->
tu>
MAXSIZE)
returnERROR;
Q->
data[Q->
tu].i=arow;
tu].j=ccol;
tu].e=ctemp[ccol];
}//if
//处理M的每一行
}//forarow
}//if
returnOK;
}//MultSMatrix
)
vo