




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构(c语言)入门代码本文档适用于数据结构初学者,前提是你要有c语言基础。作为刚接触编程的人,数据结构是比较蛋疼的。有参考书但是没源代码,一样蛋疼。这就是我这个文档存在的意义了。数据结构就是数据的一种组织结构。根据数据之间的关系,可以分为线性结构,树形结构和图。具体到机器中,就是定义一种数据对内存中的一组数据进行组织,然后编写对这种数据的一系列操作接口。以得到我们想要的数据结构。本人比较随性,书写可能不太规范。以下的代码可以编译运行数据结构1:线性表#include<stdio.h>#include<stdlib.h>#defineincrment10#definen10typedefcharelem;typedefstruct{数据结构(c语言)入门代码全文共33页,当前为第1页。数据结构(c语言)入门代码全文共33页,当前为第1页。intsize;intlenth;}list;intinit(list&l,ints){l.head=(elem*)malloc(s*sizeof(elem));if(!l.head)return-1;l.size=s;l.lenth=0;}intclear(list&l){l.lenth=0;return1;}intdestroy(list&l)数据结构(c语言)入门代码全文共33页,当前为第2页。数据结构(c语言)入门代码全文共33页,当前为第2页。free(l.head);return1;}intempty(listl){if(l.lenth)return0;elsereturn1;}intgetelem(listl,inti,elem&e){e=*(l.head+i-1);return1;}intlctelem(listl,eleme){inti=0;数据结构(c语言)入门代码全文共33页,当前为第3页。数据结构(c语言)入门代码全文共33页,当前为第3页。if(*(l.head+i)==e)return1;return0;}intinsert(list&l,inti,eleme){intj;l.lenth++;if(l.lenth>l.size){l.head=(elem*)realloc(l.head,(l.size+incrment)*sizeof(elem));}if(!l.head)return-1;if(i>l.lenth||i<0)return0;for(j=l.lenth;j>i;j--){*(l.head+j-1)=*(l.head+j-2);数据结构(c语言)入门代码全文共33页,当前为第4页。数据结构(c语言)入门代码全文共33页,当前为第4页。*(l.head+i-1)=e;}intdlt(list&l,inti){intj;if(!l.lenth)return-1;l.lenth--;if(i>l.lenth+1||i<0)return0;for(j=i;j<l.lenth+1;j++)*(l.head+j-1)=*(l.head+j);return1;}//以上就是数据结构的定义和操作部分voidfun1(){listl;inti;数据结构(c语言)入门代码全文共33页,当前为第5页。chare;数据结构(c语言)入门代码全文共33页,当前为第5页。init(l,20);insert(l,1,'a');insert(l,2,'b');insert(l,3,'d');for(i=1;i<=3;i++){getelem(l,i,e);printf("%c\n",e);}printf("\n");insert(l,1,'c');for(i=1;i<=4;i++){getelem(l,i,e);printf("%c\n",e);}数据结构(c语言)入门代码全文共33页,当前为第6页。数据结构(c语言)入门代码全文共33页,当前为第6页。insert(l,3,'e');for(i=1;i<=5;i++){getelem(l,i,e);printf("%c\n",e);}printf("\n");if(empty(l))printf("nowthelistisempty\n");insert(l,1,'a');insert(l,2,'b');insert(l,3,'d');for(i=1;i<=3;i++){getelem(l,i,e);printf("%c\n",e);}数据结构(c语言)入门代码全文共33页,当前为第7页。数据结构(c语言)入门代码全文共33页,当前为第7页。dlt(l,1);for(i=1;i<=2;i++){getelem(l,i,e);printf("%c\n",e);}printf("\n");if(lctelem(l,'b'))printf("bisinthelist\n");destroy(l);}voidpr_min(listl){inti;elemmin;min=*l.head;for(i=1;i<l.lenth;i++)数据结构(c语言)入门代码全文共33页,当前为第8页。数据结构(c语言)入门代码全文共33页,当前为第8页。min=*(l.head+i);printf("min=%d\n",min);}voidmain(){listl;inti;init(l,20);l.lenth=n;for(i=0;i<l.lenth;i++)*(l.head+i)=i+1;pr_min(l);}数据结构2:线性链表#include<stdlib.h>#include<stdio.h>数据结构(c语言)入门代码全文共33页,当前为第9页。数据结构(c语言)入门代码全文共33页,当前为第9页。typedefstructnode{elemdata;structnode*next;}node,*linklist;intcreat(linklist&l){l=(linklist)malloc(sizeof(node));l->next=NULL;if(!l)return0;return1;}intlenth(linklistl){intj=1;linklistgetl=l;while(getl->next)数据结构(c语言)入门代码全文共33页,当前为第10页。数据结构(c语言)入门代码全文共33页,当前为第10页。getl=getl->next;j++;}returnj;}intput(linklistl,inti,eleme){intj;linklistgetl=l;for(j=1;j<i;j++)getl=getl->next;//findtheinodeif(!getl)return0;getl->data=e;return1;}intaddone(linklistl,linklistb){数据结构(c语言)入门代码全文共33页,当前为第11页。数据结构(c语言)入门代码全文共33页,当前为第11页。//findthelastnodewhile(cur_node->next){cur_node=cur_node->next;}cur_node->next=b;b->next=NULL;return1;}intinsert(linklistl,inti,linklistb){intj;linklistgetl=l;linklistq;for(j=1;j<i-1;j++)getl=getl->next;//findthei-1node数据结构(c语言)入门代码全文共33页,当前为第12页。数据结构(c语言)入门代码全文共33页,当前为第12页。getl->next=b;b->next=q;return1;}intdlt(linklistl,inti){intj;linklistgetl=l;linklistq;for(j=1;j<i-1;j++)getl=getl->next;//findthei-1nodeq=getl->next;//findtheinodegetl->next=q->next;free(q);return1;}数据结构(c语言)入门代码全文共33页,当前为第13页。数据结构(c语言)入门代码全文共33页,当前为第13页。{linklistcur_node=l;while(cur_node){printf("%c\n",cur_node->data);cur_node=cur_node->next;}return1;voiddao_visit(linklistl){linklistrcv=l;linklistrcv2=rcv;elem*a;intlenth=0;inti=0;while(rcv)数据结构(c语言)入门代码全文共33页,当前为第14页。数据结构(c语言)入门代码全文共33页,当前为第14页。lenth++;rcv=rcv->next;}a=(elem*)malloc(lenth*sizeof(elem));while(rcv2){a[i]=rcv2->data;rcv2=rcv2->next;i++;}for(i=0;i<lenth;i++)printf("%c\n",a[lenth-1-i]);}//以上是数据结构定义,以及对它的操作voidmain(){linklistft;//a数据结构(c语言)入门代码全文共33页,当前为第15页。数据结构(c语言)入门代码全文共33页,当前为第15页。linklistb;//clinklistc;//i//char*a;//charb[10]="hello";//a=b;//printf("%s",a);creat(ft);creat(a);creat(b);creat(c);put(ft,1,'a');addone(ft,a);put(ft,2,'b');addone(ft,b);put(ft,3,'c');insert(ft,3,c);数据结构(c语言)入门代码全文共33页,当前为第16页。数据结构(c语言)入门代码全文共33页,当前为第16页。dlt(ft,2);visit(ft);dao_visit(ft);//printf("%d\n",lenth(ft));//visit(b);//printf("%d\n",lenth(b));}数据结构3:栈#include"stdlib.h"#include"stdio.h"typedefintelmtp;typedefstruct{elmtp*base;elmtp*top;intstksize;}stack;intinit(stack&s,intsize)数据结构(c语言)入门代码全文共33页,当前为第17页。数据结构(c语言)入门代码全文共33页,当前为第17页。s.base=(elmtp*)malloc(size*sizeof(char));if(!s.base)return0;s.top=s.base;s.stksize=size;return1;}intpush(stack&s,elmtpe){*s.top=e;s.top++;return1;}intgettop(stacks,elmtp&e){if(s.base==s.top)return0;e=*(s.top-1);数据结构(c语言)入门代码全文共33页,当前为第18页。数据结构(c语言)入门代码全文共33页,当前为第18页。intpop(stack&s,elmtp&e){if(s.base==s.top)return0;e=*(s.top-1);s.top--;return1;}//以上是。。。。。。。。。。。。。。。//下面这个main函数是将10进制数转为8进制voidmain(){intn;stacks;init(s,10);scanf("%d",&n);while(n){数据结构(c语言)入门代码全文共33页,当前为第19页。数据结构(c语言)入门代码全文共33页,当前为第19页。n=n/8;}while(pop(s,n)){printf("%d\t",n);}}数据结构4:树#include<stdio.h>#include<stdlib.h>#defineprprintftypedefcharelem;inta=48;structnode{structnode*parent;数据结构(c语言)入门代码全文共33页,当前为第20页。数据结构(c语言)入门代码全文共33页,当前为第20页。structnode*rchild;elemdata;};typedefstructnode*root;intcreat(root&r)//创建结点{r=(root)malloc(sizeof(node));if(!r)return0;r->parent=0;r->lchild=0;r->rchild=0;r->data=a++;return1;}//添加关系voidaddparent_lchild(rootparent,rootb){数据结构(c语言)入门代码全文共33页,当前为第21页。数据结构(c语言)入门代码全文共33页,当前为第21页。parent->lchild=b;}voidaddparent_rchild(rootparent,rootb){b->parent=parent;parent->rchild=b;}//先序遍历voidpre_traverse(rootr){printf("%c\n",r->data);if(r->lchild)pre_traverse(r->lchild);if(r->rchild)pre_traverse(r->rchild);//if(!(r->lchild||r->rchild))}数据结构(c语言)入门代码全文共33页,当前为第22页。数据结构(c语言)入门代码全文共33页,当前为第22页。voidmain(){inti;rootr[10];creat(r[0]);//pr("%c\n",r[0]->data);//pr("%d\n",r[0]);//536//pr("%d\n",&r[0]->parent);//536//pr("%d\n",&r[0]->lchild);//540//pr("%d\n",&r[0]->rchild);//544//pr("%d\n",&r[0]->data);//548random7;&r->data=r+12;creat(r[1]);creat(r[2]);creat(r[3]);creat(r[4]);creat(r[5]);数据结构(c语言)入门代码全文共33页,当前为第23页。addparent_lchild(r[0],r[1]);数据结构(c语言)入门代码全文共33页,当前为第23页。addparent_rchild(r[0],r[2]);addparent_lchild(r[1],r[3]);addparent_rchild(r[1],r[4]);addparent_lchild(r[2],r[5]);for(i=0;i<6;i++)pr("%c\n",r[i]->data);pre_traverse(r[0]);}附:一道程序题12345234563456745678从1开始到8结束,可以横向和纵向移动,每次一格,问,共有多少总路径代码:#include<stdio.h>#include"stdlib.h"数据结构(c语言)入门代码全文共33页,当前为第24页。//#include<stdio.h>数据结构(c语言)入门代码全文共33页,当前为第24页。#defineincrement10typedefstruct{intx;inty;}posion;typedefstruct{intid;intde;//posioncur;}array;typedefstruct{posioncur_p;arraycur_a;}elem;typedefstruct{elem*base;数据结构(c语言)入门代码全文共33页,当前为第25页。elem*top;数据结构(c语言)入门代码全文共33页,当前为第25页。intstksize;}stack;//staticintx=0,y=0;intinit(stack&s,intsize){s.base=(elem*)malloc(size*sizeof(elem));if(!s.base)exit(0);s.top=s.base;s.stksize=size;return1;}elemgettop(stacks){//if(s.top==s.base)return;return*(s.top-1);}数据结构(c语言)入门代码全文共33页,当前为第26页。intempty(stacks)数据结构(c语言)入门代码全文共33页,当前为第26页。{if(s.top==s.base)return0;elsereturn1;}intpush(stack&s,eleme){if(s.top-s.base>=s.stksize){s.base=(elem*)realloc(s.base,(s.stksize+increment)*sizeof(elem));}if(!s.base)exit(0);*s.top=e;s.top++;return1;}intpop(stack&s)数据结构(c语言)入门代码全文共33页,当前为第27页。{数据结构(c语言)入门代码全文共33页,当前为第27页。if(s.top==s.base)return0;//e=*(s.top-1);s.top--;return1;}voidmain(){arraya[5][6];inti=0,j=0;stacks;intcount=0;elemcur_e;//initpositionposionnow={0,0};//inita[5][6]for(i=0;i<4;i++)数据结构(c语言)入门代码全文共33页,当前为第28页。for(j=0;j<5;j++)数据结构(c语言)入门代码全文共33页,当前为第28页。{a[i][j].id=i+j+1;a[i][j].de=0;}for(i=0;i<4;i++){a[i][5].id=0;a[i][5].de=0;}for(j=0;j<6;j++){a[4][j].id=0;a[4][j].de=0;}//initstackinit(s,30);数据结构(c语言)入门代码全文共33页,当前为第29页。//for(i=0;i<5;i++)数据结构(c语言)入门代码全文共33页,当前为第29页。//{//for(j=0;j<6;j++)//printf("%d",a[i][j].de);//printf("\n");//}//i=j=0;/*cur_e.cur_p=now;cur_e.cur_a=a[now.x][now.y];push(s,cur_e
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 数字货币革命:2025年全球央行数字货币(CBDC)安全性与风险管理报告
- 数字货币行业目标用户需求洞察报告2025分析
- 水溶性绿色着色-洞察及研究
- 江西水箱保温施工方案
- 施工方案单页模板图片
- 滁州道路运输安全员培训课件
- 电焊工安全用电培训
- 电焊工安全培训教学课件
- 桥梁建筑方案设计图纸
- 建筑夹层效果评估方案设计
- 《穴位贴敷治疗》课件
- 临时施工围挡安全应急预案
- 2024年旧楼简易改造合同范本
- 电话客服服务流程与标准
- GB/T 33629-2024风能发电系统雷电防护
- 综合应用能力事业单位考试(综合管理类A类)试题及解答参考(2024年)
- 2024-2025学年中职数学拓展模块一 (上册)高教版(2021·十四五)教学设计合集
- 新苏教版六年级科学上册活动手册答案
- 新人教版七年级上册初中数学全册教材习题课件
- 《中小学生研学旅行实务》研学旅行指导课程全套教学课件
- 兼任宗教活动场所管理组织负责人备案表
评论
0/150
提交评论