数据结构与算法实验指导书
} printf(\ } }
main(seqqueue *head)
{ int n,i,m,x,y,select,xq;
printf(\ sqinit(head);
printf(\ scanf(\ for (i=0;i {printf(\ scanf(\ enqueue(head,m);} printf(\ printf(\ display(head); printf(\ printf(\ printf(\ printf(\ printf(\ printf(\ scanf(\ switch(select) {case 1:{ printf(\ scanf(\ enqueue(head,x); display(head); break;} case 2:{ dequeue(head); display(head); break;} case 3:{ if(empty(head)) printf(\ else printf(\ case 4:{ y=gethead(head); printf(\ break;} case 5:{ display(head); break;} } } 实验五 串及数组的实验 一、实验目的 - 16 - 数据结构与算法实验指导书 1、了解串及数组的两种存储方法,掌握数组在作为存储结构中的地址计算方法。 2、了解稀疏矩阵的两种压缩存储方法的特点和适用范围,领会稀疏矩阵运算采 用的处理方法。 二、实验内容 1、顺序串的基本操作 #define MaxSize 100 typedef struct { char str[MaxSize]; int len; } strtype; void assign(strtype *s,char t[]) { int i=0; while (t[i]!='\\0') { s->str[i]=t[i]; i++; } s->str[i]='\\0'; s->len=i; } void strcopy(strtype *s,strtype t) { int i; for (i=0;i<=t.len;i++) s->str[i]=t.str[i]; } int length(strtype s) { return(s.len); } int equal(strtype s,strtype t) { int i=0; if (s.len!=t.len) return(0); else { for (i=0;i strtype concat(strtype s,strtype t) { strtype r; int i,j; for (i=0;i - 17 - 数据结构与算法实验指导书 r.str[i]=s.str[i]; for (j=0;j<=t.len;j++) r.str[s.len+j]=t.str[j]; r.len=i+j; return(r); } int index(strtype s,strtype t) { int i,j,k; for (i=0;s.str[i];i++) for (j=i,k=0;s.str[j]==t.str[k];j++,k++) if (!t.str[k+1]) return(i); return(-1); } strtype substr(strtype s,int i,int k) { strtype t; int j; for (j=i;j t.str[t.len]='\\0'; return(t); } void insert(strtype *s,int i,strtype t) { strtype r; int j; if (i>s->len) printf(\位置参数值错误\\n\ else { for (j=i;j void delete(strtype *s,int i,int k) { int j; if (i>s->len || i+k>s->len) printf(\位置参数值错误\\n\ - 18 - 数据结构与算法实验指导书 else { for (j=i+k;j strtype replace(strtype s,strtype t,strtype v) { int i; i=index(s,t); while (i>=0) { delete(&s,i,t.len); insert(&s,i,v); i=index(s,t); } return(s); } void display(strtype s) { printf(\字符串:%s\\n\ } main() { strtype s,t,r,v; assign(&s,\ assign(&t,\ assign(&v,\ display(s); display(t); display(v); printf(\长度=%d\\n\ printf(\与v连接\ display(concat(t,v)); printf(\中的t替换成v后的\ display(replace(s,t,v)); } 2、三元组稀疏矩阵的基本操作 #include typedef int smat[Max][3]; void display(); void creatmat(int A[M][N],smat B) /*A是一个稀疏矩阵,B是产生的相对应的三元组存储*/ - 19 - 数据结构与算法实验指导书 { int i,j,k=1; for (i=0;i B[0][0]=M;B[0][1]=N; B[0][2]=k-1; /*存入非0元素个数*/ } int findval(smat A,int x) { int i,t; t=A[0][2]; /*非0元素个数*/ i=1; while (i<=t && A[i][2]!=x) i++; /*查找等于x的元素值*/ if (i<=t) return(1); else return(0); } void trsmat(smat A,smat B) /*A是稀疏矩阵的三元组形式,B是存放A的转置矩阵的三元组*/ { int m,n,p,q,t,col; /* m:A中的行数; n:A中的列数; t:A的非0元素个数*/ /* q:B的下一个项位置; p:A的当前项*/ m=A[0][0]; n=A[0][1]; t=A[0][2]; B[0][0]=n; B[0][1]=m; B[0][2]=t; /*产生第0行的结果*/ if (t>0) /*非0元素才做转置*/ { q=1; for (col=0;col void matadd(smat A,smat B,smat C) { int i=1,j=1,k=1; while (i<=A[0][2] && j<=B[0][2]) /*若A的当前项的行号等于B的当前项的行号,则比较其列号,将较小列的项*/ /*存入C中,如果列号也相等,则将对应的元素值相加后存入C中*/ if (A[i][0]==B[j][0]) if (A[i][1] - 20 - 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库数据结构实验上机指导(4)在线全文阅读。
相关推荐: