ImageVerifierCode 换一换
格式:DOCX , 页数:49 ,大小:24.37KB ,
资源ID:2250230      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-2250230.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构代码c.docx)为本站会员(b****2)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

数据结构代码c.docx

1、数据结构代码c文件的操作:#include#includeint main() FILE*fp,*out,*in,*p; int i=0,a; fp=fopen(F:textintfile.dat,w); if(fp=NULL) printf(open the file error); exit(0); while(i1000) a=rand(); printf(%8d,a); fprintf(fp,%8d,a); i+; printf(n); fclose(fp); in=fopen(F:textintfile.dat,r); if(in=NULL) printf(open the file

2、 error); exit(0); while(!feof(in) fscanf(in, %8d,&a); fclose(in); out=fopen(F:textintfile.dat,r); if(out=NULL) printf(open the file error); exit(0); fseek(out,32L, SEEK_SET); fscanf(out,%8d,&a); printf(%8d,a); fseek(out,72L, SEEK_SET); fscanf(out,%8d,&a); printf(%8d,a); fseek(out,-40L, SEEK_END); fs

3、canf(out,%8d,&a); printf(%8d,a); fseek(out,-80L, SEEK_END); fscanf(out,%8d,&a); printf(%8d,a); system(pause); fclose(out); return 0;文件操作2:#include#includetypedef struct char name10;/ char num10;/ int sex; int grade3; STUDENT;STUDENT studinfo3;int main() FILE *in,*fp; int i,j; fp=fopen(F:textstudinfo

4、.dat,wb); if(fp=NULL) printf(open the file error); exit(0);printf(enter the information of students:n); for(i=0;i3;i+) scanf(%s,studinfoi.name); scanf(%s,studinfoi.num); scanf(%d,&studinfoi.sex); for(j=0;j3;j+) scanf(%d,&studinfoi.gradej);/ fwrite(studinfo,sizeof(STUDENT),3,fp); fclose(fp); in=fopen

5、(F:textstudinfo.dat,rb); if(in=NULL) printf(open the file error); exit(0); fread(studinfo,sizeof(STUDENT),3,in);/for(i=0;i3;i+) printf(%5s%5s,studinfoi.name,studinfoi.num); printf(%3d,studinfoi.sex); for(j=0;j3;j+) printf(%3d,studinfoi.gradej); printf(n); fclose(in); system(pause); return 0;排序:#incl

6、ude#include#define n 1000typedef struct int key;rectype;rectype Rn+1;void Bubblesort (rectype R) int i,j,swap; for(i=1;ii;j-) if(Rj-1.keyRj.key) R0=Rj-1; Rj-1=Rj; Rj=R0; swap=1; if(!swap) break; void Insertsort(rectype R) int i,j; for(i=2;i=n;i+) if (Ri.keyRi-1.key) R0=Ri; j=i-1; while(R0.keyRj.key)

7、 Rj+1=Rj-; Rj+1=R0; void Selectsort(rectype R,int s) /对R1Rn进行简单选择排序 int i,j,k; rectype t; for(i=1;in;i+) k=i; for(j=i+1;j=n;j+) /选择关键字最小的记录Rk if(Ri.keyRk.key) k=j; if(k!=i) t=Ri; Ri=Rk; Rk=t; int Partition(rectype R,int low,int high)/返回划分后被定位的基准记录的位置 rectype pivot=Rlow; while(lowhigh) while(low=pivo

8、t.key) high-; /自右向左扫描,查找第一个关键字大于R0.key的记录 if(lowhigh) Rlow+=Rhigh; /移动Rhigh至Rlow while(lowhigh&Rlow.key=pivot.key) low+; if(lowhigh) Rhigh-=Rlow; /移动 Rlow至Rhigh Rlow=pivot; return low; /快速排序void Quicksort(rectype R,int s,int t) int i; if(st)/只有一个记录或无记录时无需排序 i=Partition(R,s,t);/ 对Rs-Rt进行划分 Quicksort(

9、R,s,i-1);/ Quicksort(R,s,i-1);/递归处理左序列 Quicksort(R,i+1,t);/递归处理右序列 int main() FILE *fp; int i=0,a; fp=fopen(F:/text/intfile,w); if(fp=NULL) printf(error); exit(0); while(in) a=rand(); printf(%10d,a); fprintf(fp,%10d,a); i+; printf(n); fclose(fp); fp=fopen(F:/text/intfile,r); if(fp=NULL) printf(error

10、); exit(0); for(i=1;i=n;i+) fscanf(fp,%10d,&Ri.key); printf(冒泡排序:n); Bubblesort (R); for(i=1;i=n;i+) printf(%10d,Ri.key); printf(n); printf(直接插入排序:n); Insertsort (R); for(i=1;i=n;i+) printf(%10d,Ri.key); printf(n); printf(简单选择排序:n); Selectsort (R,n); for(i=1;i=n;i+) printf(%10d,Ri.key); printf(n); p

11、rintf(快速排序:n); Quicksort (R,1,n); for(i=1;i=n;i+) printf(%10d,Ri.key); printf(n); fclose(fp); system(pause); return 0; 排序2#include#include#includetypedef struct char name10;/ char num10;/ int sex; int grade4; STUDENT;STUDENT studinfo4;int main() FILE *in,*fp; int i,j;int aver4;int k,swap; fp=fopen(F

12、:textstudinfo.dat,wb); if(fp=NULL) printf(open the file error); exit(0); printf(enter the information of students:n); for(i=1;i=3;i+) scanf(%s,studinfoi.name); scanf(%s,studinfoi.num); scanf(%d,&studinfoi.sex); for(j=1;j=3;j+) scanf(%d,&studinfoi.gradej);/ fwrite(studinfo,sizeof(STUDENT),3,fp); fclo

13、se(fp); in=fopen(F:textstudinfo.dat,rb); if(in=NULL) printf(open the file error); exit(0); fread(studinfo,sizeof(STUDENT),3,in);/ for(i=1;i=3;i+) printf(%5s%5s,studinfoi.name,studinfoi.num); printf(%3d,studinfoi.sex); for(j=1;j=3;j+) printf(%3d,studinfoi.gradej); printf(n); for(i=1;ii;j-) if(strcmp(

14、studinfoj-1.num,studinfoj.num)0) studinfo0=studinfoj-1; studinfoj-1=studinfoj; studinfoj=studinfo0; swap=1; if(!swap) break; printf(按学号排列后:n); for(i=1;i=3;i+) printf(%5s%5s,studinfoi.name,studinfoi.num); printf(%5d,studinfoi.sex); for(j=1;j=3;j+) printf(%5d,studinfoi.gradej); printf(n); printf(n); f

15、or(i=1;ii;j-) if(strcmp(studinfoj-1.name,studinfoj.name)0) studinfo0=studinfoj-1; studinfoj-1=studinfoj; studinfoj=studinfo0; swap=1; if(!swap) break; printf(按姓名排序后:n); for(i=1;i=3;i+) printf(%8s%8s,studinfoi.name,studinfoi.num); printf(%5d,studinfoi.sex); for(j=1;j=3;j+) printf(%10d,studinfoi.grade

16、j); printf(n); printf(n); printf(按平均成绩排序后:n); for(i=1;i=3;i+) averi=(studinfoi.grade1+studinfoi.grade2+studinfoi.grade3)/3; for(i=1;i=2;i+) if(averiaveri+1) studinfo0=studinfoi; studinfoi=studinfoi+1; studinfoi+1=studinfo0; for(i=1;i=3;i+) printf(%8s%8s,studinfoi.name,studinfoi.num); printf(%10d%10d

17、,studinfoi.sex,averi); for(j=1;j=3;j+) printf(%10d,studinfoi.gradej); printf(n); fclose(in); system(pause); return 0; 括号匹配#include#include#define Maxsize 100typedef struct char dataMaxsize; int top;SqStack;SqStack*InitList() SqStack*S=(SqStack*)malloc(sizeof(SqStack); S-top=0; return S; int correct(

18、char exp) char stMaxsize; int top=-1,i=0; int tag=1; while(expi!=0&tag) if(expi=(|expi=|expi=) top+; sttop=expi; if(expi=) if(sttop=() top-; else tag=0; if(expi=) if(sttop=) top-; else tag=0; if(expi=) if(sttop=) top-; else tag=0; i+; if(top!=-1)tag=0; if(tag=0)printf(no); else printf(yes);int main(

19、) SqStack*S; int correct(char exp); char n,expMaxsize; printf(输入表达式:n); gets(exp); correct(exp); system(pause); return 0;后缀表示法#include#include#include#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define N 100typedef struct int *base; int *top; int stacksize;SqStack;void InitStack (SqStack &S)

20、 /构造一个空栈S S.base =(int *)malloc(STACK_INIT_SIZE * sizeof(int); if(!S.base)exit(0); S.top =S.base; S.stacksize=STACK_INIT_SIZE; /InitStackvoid Push(SqStack &S,int e) /插入元素e为新的栈顶元素 if(S.top-S.base=S.stacksize) S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int); if(!S.base)exit(0); S

21、.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; *S.top+=e; void Pop(SqStack &S, int *e) /若栈不空,则删除S的栈顶元素,用e返回其值 if(S.base=S.top)return; *e=*-S.top; int main() char aN; char pN; int i,j,q; int c,b,d; SqStack S; InitStack(S); gets(a); for(i=0;ai!=0;i+) j=0; while(ai!= ) pj=ai; i+; j+; if(p0!=+)&(

22、p0!=-)&(p0!=*)&(p0!=/) q=atoi(p); Push(S,q); else Pop(S,&c); Pop(S,&b); switch(p0) case+: d=b+c;break; case-: d=b-c;break; case*: d=b*c;break; case/: d=b/c;break; default: printf(hehe); Push(S,d); memset(p,0,N); Pop(S,&d); printf(n答案为:%dn,d); system(pause); return 0;邻接矩阵的深度遍历:#include#include#define

23、 n 8#define e 10/图的边数 int visitedn;typedef int vextype;/顶点的数据结构 typedef int adjtype;/顶点权值数据结构typedef struct vextype vexn; adjtype arcnn;/邻接矩阵 graph;graph G;void CreateAdjArray(graph *G)/建立无向网络邻接矩阵 int i,j,k; for(i=1;ivexi=i; for(i=1;i=n;i+) for(j=1;jarcij=0; for(k=1;karcij=1; void DFSTraverse(graph

24、*G,int i) /深度遍历搜索 printf(深度优先遍历:顶点%dn, G-vexi); visitedi=1; for(int j=1;jarcij=1 & visitedj=0) DFSTraverse(G,j); void DFSA(graph* G) int i; for(i=1;i=n;i+) visitedi=0; for(i=1;i=n;i+) if(!visitedi) DFSTraverse(G,i);/DFSA int main() int i; graph *G=(graph *)malloc(sizeof(graph); CreateAdjArray(G); printf(input graph G:n); DFSA(

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2