资源描述:
《C语言算法大全(经典)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
C语言经典算法目录一、单元加61.erre62.erre263.数组完全单元74.栈单元加8二、底层编程91.asm92.C标志符命名源程序213.ping234.winsock2275.检测鼠标296.检出错误307.时间陷阱31三、汉诺塔331.非递归332.汉诺塔343.汉诺塔2354.换位递归365.诺汉塔画图版376.四塔1387.四塔240四、逆阵411.简单逆阵412.逆矩阵423.逆阵45五、经典程序47251 1.编程汉字问题472.编随机数483.递堆法494.汉字字模505.简单数据库546.冒泡法改进557.穷举搜索法578.扫描码589.傻瓜递归5910.神经元模型5911.试题6312.挽救软盘6413.小白鼠钻迷宫6514.小字库DIY6815.小字库DIY-69六、求进制711.16进制10进制712.二进制数723.二进制数272七、数据结构73一、单链表731.ww732.单链表743.单链表1764.单链表2775.单链表倒序786.单链表的处理全集797.建立链表1888.节点899.链表(递归)93251 10.链表十五人排序9611.冒泡排序9812.质因子99二、排列类数据结构1011大整数1012.递归车厢1033.队列1054.二叉排序树1075.二叉树实例1106.各种排序法1157.哈夫曼算法1248.哈慢树1339.建树和遍历13510.迷宫13711.迷宫问题13912.逆波兰计算器14213.排序法14414.数据结构115215.数据结构215315.数据结构315416.双链表正排序15517.推箱子15718.无向图15819.线索化二叉树16120.线性顺序存储结构16321.栈操作166八、数学问题169一、凉东问题1691.321692.re169251 3.数组递归退出1704.数组递归退出2171二、苹果纠纷1721.ff1722.苹果分法172三、数学算法1731.符号图形1732.绘制圆1763.余弦曲线1764.余弦直线177四、桃子猴问题1781.乘方函数桃子猴1782.递归桃猴1783.猴子和桃1794.桃子猴180五、小明买书1801.小明买书1802.小明买书C++181六、圆周率1831.狐狸圆周率1832.圆周率183七、运算类数学问题1841.阿姆斯特朗数1842.百鸡百钱1843.大加数1854.大小倍约1885.大整数1886.灯塔问题1907.递推1928.叠代整除193251 9.多位阶乘19410.多位阶乘219611.黑白19712.简单计算器19713.阶乘递归19914.逻辑移动19915.平方根20016.十五人排序20117.四分砝码20218.完数20319.小孩分糖果204九、数组2051.矩阵转换2052.螺旋数组12063.螺旋数组22074.数字移动2085.数组操作2106.桶排序2107.杨辉三角形212十、问题算法212一、骑士遍历2121.骑士遍历12122.骑士遍历22153.骑士遍历回逆223二、万年历2241.万年历2242.万年历的算法229三、其它问题算法2341.N皇后问题回溯算法2342.动态计算网络最长最短路线236251 3.货郎担分枝限界图形演示2394.货郎担限界算法2485.矩阵乘法动态规划256十一、小写数字转为大写数字2591.小写数字转换成大写数字12592.小写数字转换成大写数字22613.小写数字转换成大写数字3265十二、效验算法2661.Crctable266十三、硬币情况2681.for循环的2682.硬币分法268十四、字符2691.单词倒转2692.出字符2703.回文2704.字符编辑2715.字符编辑技术(插入和删除)2726.字符串查找272十五、数据算法2741.单链表2742.单循环链表2783.定长串2824.二叉树2865.二分查找12886.二分查找22907.链串2928.链队列2959.链栈29810.顺序队列299251 11.顺序栈30212.图305251 七、数据结构251 一、单链表1.ww/*我不知道我这样做合适不合适,好象自己在帮助别人干坏事我不可能给你说得很细,学习到的东西是自己的,难道对自己也可以作弊吗?好好努力吧*/#include#include#includestructf//数据结构{doubledata;structf*next;};main(){intn;scanf("%d",&n);fun(n);}fun(n){doubley=0.00,x=1.00;//这里用双精度实数定义可以容纳更大的数据structf*head,*cthis,*a;intnumber,i,j=0;do{a=(structf*)malloc(sizeof(structf));if(head==NULL)head=a;else{cthis=head;cthis=cthis->next;}cthis=a;cthis->data=j;//这里定义存处在各个链表里的数据//你可以用(int)rand()随机数来代替jnumber=cthis->data;for(i=0;i#includestructroommate{charname[20];longnum;intage;charbirthplace[20];structroommate*next;};structroommate*head,*cthis,*cnew;voidnew_record(void){charch;charnumstr[20];do{cnew=(structroommate*)malloc(sizeof(structroommate));if(head==NULL)head=cnew;else{cthis=head;while(cthis->next!=NULL)cthis=cthis->next;cthis->next=cnew;}cthis=cnew;printf(" entername:");gets(cthis->name);printf(" enternumber:");gets(numstr);cthis->num=atol(numstr);printf(" enterage:");gets(numstr);cthis->age=atoi(numstr);printf(" enterbirthplace:");251 gets(cthis->birthplace);cthis->next=NULL;printf(" print'e'toaddrecord:");ch=getchar();getchar();}while(ch=='e');cthis->next=head;}voidlistall(void){inti=0;cthis=head;do{printf(" recordnumber%d ",i);printf("name:%s ",cthis->name);printf("num:%ld ",cthis->num);printf("age:%d ",cthis->age);printf("birthplace:%s ",cthis->birthplace);cthis=cthis->next;}while(cthis!=head);}voidmain(){charch;intflag=1;head=NULL;while(flag){printf(" type'e'toenternewrecord,");printf("type'l'tolistallrecords,");printf("type'i'toinsertarecord:");ch=getchar();getchar();switch(ch){case'e':new_record();break;case'l':listall();break;default:flag=0;}}}3.单链表1#include251 #includestructroommate{charname;longnum;intage;charbirthplace;structroommate*next;};structroommate*head,*cthis,*cnew;voidins_record(void){inti,j=0;charnumstr,b;cthis=head;printf(" Inputanum:");scanf("%d",&i);while(jnext;++j;}if(j>i-1)return;cnew=(structroommate*)malloc(sizeof(structroommate));printf(" pleaseinputtheinformation:");printf(" entername:");scanf("%s",&cnew->name);printf(" enternumber:");scanf("%ld",&cnew->num);printf(" enterage:");scanf("%d",&cnew->age);printf(" enterbirthplace:");scanf("%s",&cnew->birthplace);cnew->next=cthis;}main(){ins_record();}251 4.单链表2#include#includestructnode{intkey;structnode*next;};voidcreat_link(structnode*);main(){structnode*head=NULL;creat_link(head);/*这里可以这么调用吗?*/}voidcreat_link(structnode*head_node){structnode*p,*q,*Temp;intnumber;printf("Pleaseinputdata:[-1isEnd] ");scanf("%d",&number);while(number!=-1){q=(structnode*)malloc(sizeof(structnode));q->key=number;if(head_node==NULL){head_node=q;p=q;}else{p->next=q;p=q;}scanf("%d",&number);}p->next=NULL;Temp=head_node;while(Temp!=NULL){printf("%d ",Temp->key);Temp=Temp->next;}}251 5.单链表倒序#includestructfsb{intdata;intflag;structfsb*next;};main(){structfsb*p,*head,*sta,*end;inti,cishu,j;end=(structfsb*)malloc(sizeof(structfsb));end->data=0;end->flag=0;end->next=NULL;head=p=end;for(i=2;i<=10;i++){end=(structfsb*)malloc(sizeof(structfsb));end->data=i-1;end->flag=0;end->next=NULL;p->next=end;p=end;}p->next=NULL;printf(" 倒序前:");p=head;for(i=1;i<=10;i++){printf("%d",p->data);p=p->next;}/*************************/p=NULL;while(head->next!=NULL){sta=head;head=head->next;sta->next=p;p=sta;251 }head->next=sta;printf(" 倒序后:");p=head;for(i=1;i<=10;i++){printf("%d",p->data);p=p->next;}/*************************/}6.单链表的处理全集/*谁有兴趣一起来丰富这个程序的的功能??*/#include#include#defineMAX20#defineELEMTPint#definev(*p)structnode{ELEMTPdata;structnode*next;};structnode*p,*q,*s,*head;intj=0,i,k;main(){intx,y,cord;voidoutlin(structnode*h);voidcreate();voidinsert(structnode*h,intx,inty);voiddeletes(structnode*h,intx);structnode*MaxCompare(structnode*h);structnode*MinCompare(structnode*h);intdelIterance(structnode*h);voidbatchInsert(structnode*h,intx);voidbatchDelete(structnode*h,intx,inty);voidCz(structnode*h);voidXg(structnode*h);printf("建立链表,输入-999完成链表: ");create();251 i=j;outlin(head);do{printf(" 主菜单 ");printf("1插入一个元素 ");printf("2删除一个元素 ");printf("3升序排序 ");printf("4降序排序 ");printf("5查找元素 ");printf("6修改元素 ");printf("7删除重复元素 ");printf("8批量加入元素 ");printf("9批量删除元素 ");printf("0结束程序运行 ");printf("----------------------------------------- ");printf("请输入您的选择(1,2,3,4,5,6,7,8,9,0)");scanf("%d",&cord);switch(cord){case1:{printf("请输入插入的位置i:");scanf("%d",&x);printf("请输入插入的数据y:");scanf("%d",&y);insert(head,x,y);i=j;outlin(head);}break;case2:{printf("x=?");scanf("%d",&x);deletes(head,x);i=j;outlin(head);}break;case3:{printf("链表由大到小是");s=MaxCompare(head);j=i;outlin(s);//outlin(head);251 }break;case4:{printf("链表由大到小是");s=MinCompare(head);j=i;outlin(s);}break;case5:{Cz(head);outlin(head);}break;case6:{Xg(head);outlin(head);}break;case7:{k=delIterance(head);i=i-k;j=i;outlin(head);}break;case8:{printf("请输入插入的位置i:");scanf("%d",&x);batchInsert(head,x);i=j;outlin(head);}break;case9:{printf("请输入删除的起始位置i:");scanf("%d",&x);printf("请输入删除的结束位置y:");scanf("%d",&y);batchDelete(head,x,y);i=j;outlin(head);}break;case0:251 {exit(0);}break;}}while(cord<=9&&cord>=0);}voidoutlin(structnode*h){p=h->next;while(p!=NULL){printf("data=%4d",p->data);p=p->next;}printf(" 输出结束 ");}voiddeletes(structnode*h,intx)//删除节点{p=h;while(p->next!=NULL&&p->next->data!=x)p=p->next;if(p->next==NULL)printf("x不存在!");else{q=p->next;p->next=q->next;free(q);--j;}}voidinsert(structnode*h,intx,inty){s=(structnode*)malloc(sizeof(structnode));s->data=y;q=h;p=h->next;while(p!=NULL&&p->data!=x){q=p;p=p->next;}q->next=s;s->next=p;++j;}251 voidcreate()//建立链表{intx;head=(structnode*)malloc(sizeof(structnode));head->next=NULL;p=head;printf("x=?");scanf("%d",&x);while(x!=-999){s=(structnode*)malloc(sizeof(structnode));s->data=x;s->next=NULL;p->next=s;p=s;printf("x=?");++j;scanf("%d",&x);}}/////////////以下函数由七绝玩家编写/////////////structnode*MaxCompare(structnode*h)//由大到小排序{structnode*t;intx;t=h;s=NULL;while(j!=0){x=t->next->data;q=t->next;while(q!=NULL){if(q->data<=x)x=q->data;elsex=x;q=q->next;}p=t;while(p->next!=NULL&&p->next->data!=x)p=p->next;q=p->next;p->next=q->next;t=p;t=h;p=q;p->next=s;251 s=p;j--;}t->next=s;head=t;return(t);}structnode*MinCompare(structnode*h)//由小到大排序{structnode*t;intx;t=h;s=NULL;while(j!=0){x=t->next->data;q=t->next;while(q!=NULL){if(q->data>=x)x=q->data;elsex=x;q=q->next;}p=t;while(p->next!=NULL&&p->next->data!=x)p=p->next;q=p->next;p->next=q->next;t=p;t=h;p=q;p->next=s;s=p;j--;}t->next=s;head=t;return(t);}intdelIterance(structnode*h)//删除重复元素{intx,y=0;--j;s=h->next;while(j>0)251 {x=s->data;p=s;while(p->next!=NULL){if(p->next==NULL){x=x;}elseif(p->next->data==x){q=p->next;p->next=q->next;free(q);--j;++y;}else{p=p->next;}}s=s->next;--j;}returny;}voidbatchInsert(structnode*h,intx)//批量加入{inty=0;q=h;p=h->next;while(p!=NULL&&p->data!=x){q=p;p=p->next;}printf("y=?");scanf("%d",&y);while(y!=-999){s=(structnode*)malloc(sizeof(structnode));s->data=y;q->next=s;s->next=p;251 q=s;printf("y=?");scanf("%d",&y);++j;}}voidbatchDelete(structnode*h,intx,inty)//批量删除{intk=0,w=0;structnode*t;p=h;q=h;while(p->next!=NULL&&p->next->data!=x){++k;p=p->next;}while(q->next!=NULL&&q->next->data!=y){++w;q=q->next;}if(p->next==NULL||q->next==NULL)printf("输入的位置不正确,请重新开始!");elseif(knext;while(p->next!=s){t=p->next;p->next=t->next;free(t);--j;}}elseif(w=k){printf("没有删除元素");}else{s=p->next;while(q->next!=s){251 t=q->next;q->next=t->next;free(t);--j;}}}//////////////七绝玩家编写结束////////////////////////////以下函数由lihk编写////////////voidCz(structnode*h)//查找//{structnode*num;inti;num=head;printf("输入您要查找的号码:");scanf("%d",&i);while(i!=num->data&&num->next!=NULL){num=num->next;}if(i==num->data)printf("号码:%d",num->data);elseprintf("该号码不在链表里.");}voidXg(structnode*h)//修改//{structnode*num;inti;num=head;printf("查找您要修改的号码:");scanf("%d",&i);while(i!=num->data&&num->next!=NULL){num=num->next;}if(i==num->data){printf(":%d ",num->data);printf("输入您要修改的新信息:");printf("号码:");scanf("%d",&num->data);printf(" 修改成功!");}elseprintf("该号码不在链表里!");}//////////////lihk编写结束/////////////////251 7.建立链表1/*链表建立程序*/#include"stdio.h"#include#defineNULL0#defineLENsizeof(structstudent)structstudent{longnum;intscore;structstudent*next;};intn;/*全局变量n*/structstudent*creat(){structstudent*head;structstudent*p1,*p2;n=0;p1=p2=(structstudent*)malloc(LEN);scanf("%ld%d",&p1->num,&p1->score);/*%d和%d之间不应该有逗号*/head=NULL;while(p1->num!=0){n=n+1;if(n==1)head=p1;elsep2->next=p1;p2=p1;p1=(structstudent*)malloc(LEN);scanf("%ld%d",&p1->num,&p1->score);}p2->next=NULL;return(head);}voidprint(head)structstudent*head;{structstudent*p;p=head;/*在这里付值才可以*/printf("Now,These%drecordsare: ",n);if(head!=NULL)do{printf("%ld%5d ",p->num,p->score);p=p->next;}while(p!=NULL);251 }voidmain(){structstudent*head;printf("inputrecords: ");head=creat();print(head);}8.节点#include#defineTYPEstructstu#defineLENsizeof(structstu)structstu{intnum;intage;structstu*next;};TYPE*creatlink(intn);TYPE*deletelink(TYPE*head,intnum);TYPE*insertlink(TYPE*head,TYPE*pi);voidprintlink(TYPE*head);voiddestroylink(TYPE*head);voidmain(void){TYPE*head=NULL,*pnum=NULL;intn=3,num;/*创建一个含n个节点的链表*/printf("inputnumberofnode:");head=creatlink(n);printlink(head);/*删除链表中值为num的节点*/printf("Inputthedeletednumber:");scanf("%d",&num);head=deletelink(head,num);printlink(head);/*在链表中插入一个节点*/printf("Insertarecord ");pnum=(TYPE*)malloc(LEN);if(pnum==NULL)printf("PointerisNULL--memoryallocfail!");printf("Inputtheinsertednumber:");251 scanf("%d",&pnum->num);printf("Inputtheinsertedage:");scanf("%d",&pnum->age);head=insertlink(head,pnum);printlink(head);/*销毁链表,释放动态分配的内存*/destroylink(head);}/*创建一个含n个节点的链表*/TYPE*creatlink(intn){TYPE*head=NULL,*pf,*pb;inti;for(i=0;inum);printf("inputage:");scanf("%d",&pb->age);if(i==0)pf=head=pb;elsepf->next=pb;pb->next=NULL;pf=pb;}return(head);}/*删除链表中值为num的节点*/TYPE*deletelink(TYPE*head,intnum){TYPE*pf,*pb;if(head==NULL){printf(" emptylist! ");returnNULL;}pb=head;while(pb->num!=num&&pb->next!=NULL){pf=pb;pb=pb->next;251 }if(pb->num==num){if(pb==head)head=pb->next;elsepf->next=pb->next;free(pb);printf("Thenodeisdeleted ");}elseprintf("Thenodenotbeenfound! ");returnhead;}/*在链表中插入一个节点*/TYPE*insertlink(TYPE*head,TYPE*pi){TYPE*pb,*pf;pb=head;if(head==NULL){head=pi;pi->next=NULL;}else{while((pi->num>pb->num)&&(pb->next!=NULL)){pf=pb;pb=pb->next;}if(pi->num<=pb->num){if(head==pb)head=pi;elsepf->next=pi;pi->next=pb;}else{pb->next=pi;pi->next=NULL;}251 }returnhead;}/*打印链表中各节点信息*/voidprintlink(TYPE*head){printf("NumberttAge ");while(head!=NULL){printf("%dtt%d ",head->num,head->age);head=head->next;}}/*销毁链表,释放动态分配的内存*/voiddestroylink(TYPE*head){TYPE*p,*q;p=head;while(p!=NULL){q=p->next;free(p);p=q;}}9.链表(递归)#include#includestructlistNode{intdata;structlistNode*nextPtr;};typedefstructlistNodeLISTNODE;typedefLISTNODE*LISTNODEPTR;LISTNODEPTRlist(LISTNODEPTR,int);//此处不同voidprintlist(LISTNODEPTR);voidfreelist(LISTNODEPTR);//增加main(){251 LISTNODEPTRnewPtr=NULL;inti,a;for(i=0;i<3;i++){printf("pleaseenteranumber ");scanf("%d,",&a);newPtr=list(newPtr,a);//此处注意}printlist(newPtr);freelist(newPtr);//此处return0;}LISTNODEPTRlist(LISTNODEPTRsPtr,inta){if(sPtr!=NULL)sPtr->nextPtr=list(sPtr->nextPtr,a);//递归,向后面的节点上加数据。else{sPtr=(LISTNODEPTR)malloc(sizeof(LISTNODE));//注意,是节点的尺寸,类型转换sPtr->nextPtr=NULL;sPtr->data=a;}returnsPtr;}voidfreelist(LISTNODEPTRsPtr){if(sPtr!=NULL){freelist(sPtr->nextPtr);//递归,先释放后面的节点free(sPtr);//再释放本节点}else//return;//此两行可不要}voidprintlist(LISTNODEPTRcurrentPtr){if(currentPtr==NULL)printf("Thelistisempty ");else{printf("Thislistis: ");while(currentPtr!=NULL){printf("%d-->",currentPtr->data);251 currentPtr=currentPtr->nextPtr;//这里不一样}printf("NULL ");}}你原程序错误如下:------------------------->list1.c>>#include>#include>>structlistNode{>intdata;>structlistNode*nextPtr;>};>>typedefstructlistNodeLISTNODE;>typedefLISTNODE*LISTNODEPTR;>>voidlist(LISTNODEPTR*,int);>voidprintlist(LISTNODEPTR);>>main()>{>LISTNODEPTRnewPtr=NULL;>>>inti,a;>for(i=0;i<3;i++){>printf("pleaseenteranumber ");>scanf("%d,",&a);>list(&newPtr,a);//此处给的是newPtr的地址,注意!>}>>printlist(newPtr);>>free(newPtr);//链表的释放不能这样写,这样,只释放了newPtr指向的一个节点。//可以先找到链表的尾,然后反向释放;或者,利用printlist的顺序释放,//改函数printlist,或在此函数里释放。>>return0;>}251 >>voidlist(LISTNODEPTR*sPtr,inta)>{>LISTNODEPTRnewPtr,currentPtr;>>newPtr=malloc(sizeof(LISTNODEPTR));//此处错,LISTNODEPTR是指针类型,不是结构类型,//malloc返回void指针,应该强制转换类型,此处会告警不报错,但应有良好的//编程风格与习惯。>if(newPtr!=NULL){>newPtr->data=a;>newPtr->nextPtr=NULL;>>currentPtr=*sPtr;>}>if(currentPtr==NULL){//此处条件不确切,因为currentPtr没有初始化,//如newPtr一旦为NULL,此句及以下就有问题。>newPtr->nextPtr=*sPtr;>*sPtr=newPtr;}//在第一个数来的时候,main里的newPtr通过sPtr被修改指向此节点。//在第二个数来的时候,main里的newPtr通过sPtr被修改指向此节点。//在第三个数来的时候,main里的newPtr通过sPtr被修改指向此节点。//最后,main里的newPtr指向第三个数。>}>>>>voidprintlist(LISTNODEPTRcurrentPtr)>{>if(currentPtr==NULL)>printf("Thelistisempty ");>else{>printf("Thislistis: ");>>while(currentPtr!=NULL){>printf("%d-->",currentPtr->data);//main里的newPtr指向第三个数。你先打印了最后一个数。RE第二个问题>currentPtr=currentPtr->nextPtr->data;//此句非法,类型不同,有可能让你只循环一次,如data为0。RE第一个问题>}>printf("NULL ");251 >}>}//对类似程序能运行,但结果似是而非的情况,应该多利用跟踪调试,看变量的变化。10.链表十五人排序/*发个半拉作品在这里,疑问很多,怎么才能判断一个人和其他的人见过面呢?累的我都头疼*/#include#includestructnet{intnember[3];structnet*next;}main(){intg=0,d=0,w=0,b=0,j,k,i=0,a[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//v=0,h[]={1,8,6,5,3,12,2,13,4,9,14,11,15,7,10};structnet*head=NULL,*trail,*body;while(b<7){for(j=0;j<5;j++){body=(structnet*)malloc(sizeof(structnet));for(k=0;k<3;k++){body->nember[k]=a[i%15];i+=1+w;/*if(b>3){body->nember[k]=h[v%15];v+=1+d;}else{body->nember[k]=a[i%15];i+=1+w;}*///(i>315)?(i-=1+w):(i+=1+w);}251 if(head==NULL){head=body;trail=body;}else{trail->next=body;trail=body;}}w+=6;//(b>3)?(d+=6):(w+=6);b++;}trail->next=NULL;while(head!=NULL){printf("");for(k=0;k<3;k++){g++;(g%15)?(printf("%d",head->nember[k])):(printf("%d ",head->nember[k]));}printf("");head=head->next;}}11.冒泡排序#include"stdio.h"#include"stdlib.h"structnode{intvalues;structnode*next;};structnode*create(int[],int);voidsort(structnode**);inttest_data[20000]={5,9,3,4,5,7,8};main()251 {structnode*h,*p;h=create(test_data,20000);for(p=h;p;p=p->next)printf("%2d",p->values);printf(" ");sort(&h);for(p=h;p;p=p->next)printf("%2d",p->values);printf(" ");}structnode*create(inta[],intn){structnode*h,*q;for(h=NULL;n;n--){q=(structnode*)malloc(sizeof(structnode));q->values=a[n-1];q->next=h;h=q;}returnh;}voidsort(structnode**h){structnode*q,*p,*r,*s,*h1;h1=p=(structnode*)malloc(sizeof(structnode));p->next=*h;while(p->next!=NULL){q=p->next;r=p;while(q->next!=NULL){if(q->next->valuesnext->values)r=q;q=q->next;}if(r!=p){s=r->next;r->next=s->next;s->next=p->next;p->next=s;}p=p->next;}*h=h1->next;/*问题:h1在程序中做了啥用?*/free(h1);}251 12.质因子#include#includestructnode{longfactorarray;structnode*next;};structnode*head,*q,*p;voidfactoring(longn){longi;for(i=2;i<=n/2;i++){if(n%i==0){q=(structnode*)malloc(sizeof(structnode));q->factorarray=i;q->next=NULL;if(head==NULL){head=q;p=q;}else{p->next=q;p=q;}}}}voiddeletes(){while(head!=NULL){p=head;head=p->next;free(p);}}voidmain()251 {longnum;head=NULL;printf("Pleaseinputthenumberyouwanttofindfactors:");while(scanf("%ld",&num)){printf("Thefactorsofthenumber%ldis: ",num);factoring(num);p=head;while(p!=0){printf("*%ld",p->factorarray);p=p->next;}printf(" Plearinputthenumberyouwanttofindfactors:");deletes();}}二、排列类数据结构1大整数#include#include#defineHUN10000typedefstructnode{intdata;structnode*next;}NODE;//定义链表结构NODE*insert(u,num)//在u节点之后插入一个新的NODE,其值为NUMNODE*u;intnum;{NODE*v;v=(NODE*)malloc(sizeof(NODE));//分配内存v->data=num;//赋值u->next=v;//在u后面加入一个NODEreturn(v);}NODE*addint(p,q)//完成加法操作返回指向*p+*q结果的指针251 NODE*p,*q;{NODE*pp,*qq,*r,*s,*t;inttotal,number,carry;pp=p->next;qq=q->next;s=(NODE*)malloc(sizeof(NODE));//建立存放和的链表表头s->data=-1;t=s;carry=0;//进位变量while(pp->data!=-1&&qq->data!=-1)//都不是表头{total=pp->data+qq->data+carry;//对应位和上次进位数求和number=total%HUN;//记录可以存入链表的数carry=total/HUN;//多出的为进位数t=insert(t,number);//把可存入的数插入链表pp=pp->next;//取后面的数qq=qq->next;//}r=(pp->data!=-1)?pp:qq;//两数大小不一取尚未处理的指针while(r->data!=-1)//判断另一个是否到头{total=r->data+carry;//与进位数相加number=total%HUN;//记录可存入的数carry=total/HUN;//记录进位数t=insert(t,number);//插入链表r=r->next;}if(carry)//如果还有进位数t=insert(t,1);//最后一次处理t->next=s;//完成链表return(s);//返回求和指针}NODE*inputint(void)//输入超大正整数{NODE*s,*ps,*qs;structnumber//定义临时中间结构{intnum;structnumber*np;}*p,*q;inti,j,k;longsum;charc;p=NULL;//链首为个位,链尾为高位251 while((c=getchar())!=' ')//按字符接受数字if(c>='0'&&c<='9')//若为数字就存入{q=(structnumber*)malloc(sizeof(structnumber));q->num=c-'0';//存入一位整数q->np=p;p=q;}s=(NODE*)malloc(sizeof(NODE));//建立正式链表的表头s->data=-1;//表头ps=s;while(p!=NULL)//临时链表不为空是{sum=0;i=0;k=1;while(i<4&&p!=NULL)//按四位合并临时链表{sum=sum+k*(p->num);i++;p=p->np;k=k*10;}qs=(NODE*)malloc(sizeof(NODE));//正式链表的主体qs->data=sum;ps->next=qs;ps=qs;}ps->next=s;//完成链表return(s);}printint(s)NODE*s;{if(s->next->data!=-1){printint(s->next);if(s->next->next->data==-1)printf("%d",s->next->data);else{inti,k=HUN;for(i=1;i<=4;i++,k/=10)putchar('0'+s->next->data%(k)/(k/10));}}}main(){251 NODE*s1,*s2,*s;NODE*inputint(),*addint(),*insert_after();s1=inputint();s2=inputint();printf("S1=");printint(s1);printf(" ");printf("S2=");printint(s2);printf(" ");s=addint(s1,s2);printf("S1+S2=");printint(s);printf(" ");}2.递归车厢/**********递归题改为非递归题实例车厢********/#include#defineMAX4intstack[MAX],p=-1;struct{intnum;intsign;}train[MAX];voidsub(){intinc;if(p==MAX-1){for(inc=0;inc<=p;inc++)printf("%3d",stack[inc]);printf(" ");}else{for(inc=0;inc#include#defineElemTypeint#defineQ(*qe)structquenode{ElemTypedata;structquenode*next;}*p,*s,*h;structquefr{structquenode*front,*rear;};main(){structquefr*que;intx,cord;voidOutlin(structquefrqq);voidcreat(structquefr*qe);voidinsert(structquefr*p,ElemTypex);ElemTypedeletes(structquefr*qe);do{printf(" ");251 printf("主菜单 ");printf("1建立链表队列 ");printf("2入队一个元素 ");printf("3出队一个元素 ");printf("4结束程序运行 ");printf("------------------------------- ");printf("请输入您的选择(1,2,3,4)");scanf("%d",&cord);switch(cord){case1:{que=(structquefr*)malloc(sizeof(structquefr));creat(que);Outlin(*que);}break;case2:{printf("x=?");scanf("%d",&x);insert(que,x);Outlin(*que);}break;case3:{printf("x=%d ",deletes(que));Outlin(*que);}break;case4:{exit(0);}}}while(cord<=4);}voidOutlin(structquefrqq){p=qq.front->next;/*指向第一个数据元素节点*/while(p!=NULL){printf("data=%d ",p->data);p=p->next;}printf(" outend ");251 }voidinsert(structquefr*qe,intx)/*入队x值的节点*/{s=(structquenode*)malloc(sizeof(structquenode));s->data=x;s->next=NULL;Q.rear->next=s;Q.rear=s;}ElemTypedeletes(structquefr*qe){ElemTypex;if(Q.front==Q.rear){printf("队列为空。 ");x=0;}else{p=Q.front->next;Q.front->next=p->next;if(p->next==NULL)Q.rear=Q.front;x=p->data;free(p);}return(x);}voidcreat(structquefr*qe){inti,n,x;printf("n=");scanf("%d",&n);h=(structquenode*)malloc(sizeof(structquenode));h->next=NULL;Q.front=h;Q.rear=h;for(i=1;i<=n;i++){scanf("%d",&x);insert(qe,x);}}251 4.二叉排序树#include#includestructnodb{intdata;structnodb*lch,*rch;};structnodb*root,*q,*p;voidinsert1(structnodb*s);voidcreat(){structnodb*s;inti,n,k;printf("n=?");scanf("%d",&n);for(i=1;idata=k;s->lch=NULL;s->rch=NULL;insert1(s);}}voidinsert1(structnodb*s){//非递归插入structnodb*p,*q;if(root==NULL)root=s;else{p=root;while(p!=NULL){q=p;//当p向子数节点移动时,q记录p的双亲的位置if(s->datadata)p=p->lch;elsep=p->rch;}if(s->datadata)251 q->lch=s;elseq->rch=s;//当p为空时,q就是可插入的地方}}voidprint(structnodb*t){if(t!=NULL){print(t->lch);printf("%6d",t->data);print(t->rch);}}voidbstsrch(structnodb*t,intk){intflag;p=NULL;q=t;flag=0;while((q!=NULL)&&(flag==0)){if(q->data==k){printf("发现%5d",q->data);flag=1;}elseif(kdata){p=q;q=q->lch;}else{p=q;q=q->rch;}}if(flag==0)printf("没有发现节点");}voidbstins(structnodb*t,intk){structnodb*r;bstsrch(root,k);251 if(q==NULL){r=(structnodb*)malloc(sizeof(structnodb));r->data=k;r->lch=NULL;r->rch=NULL;if(root==NULL)root=r;elseif(kdata)p->lch=r;elsep->rch=r;}}main(){intn;root=0;creat();print(root);printf("请出入关键值n=?");scanf("%d",&n);bstsrch(root,n);printf(" ");bstins(root,n);print(root);}5.二叉树实例#include#include#defineELEMTPintstructnode{ELEMTPdata;structnode*lc,*rc;};structnode*root;intm=0;main(){intcord;251 structnode*creat();voidpreorderz(structnode*t);voidinorder(structnode*t);voidpostorder(structnode*t);voiddeletes(structnode*t,structnode*p,structnode*f);do{printf(" 主菜单 ");printf("1建立二叉树 ");printf("2先序非递归遍历 ");printf("3中序递归遍历 ");printf("4后序递归遍历,求叶节点数 ");printf("5删除节点 ");printf("6结束程序运行 ");printf("--------------------------------------- ");printf("请输入您的选择(1,2,3,4,5,6)");scanf("%d",&cord);switch(cord){case1:{root=creat();//建立二叉树printf("建立二叉树程序以执行完, ");printf("请返回主菜单,用遍历算法验证程序的正确性 ");}break;case2:{preorderz(root);}break;case3:{inorder(root);}break;case4:{postorder(root);}break;case5:{//deletes(root)}case6:{printf("二叉树程序执行完,再见! ");251 exit(0);}}}while(cord<=6);}structnode*creat(){structnode*t,*q,*s[30];inti,j,x;printf("i,x=");scanf("%d%d",&i,&x);//i是按满二叉树编号,x节点应有的序号,x是节点的数据while((i!=0)&&(x!=0)){q=(structnode*)malloc(sizeof(structnode));q->data=x;q->lc=NULL;q->rc=NULL;s[i]=q;if(i==1)t=q;//t代表树根节点else{j=i/2;//双亲节点的编号if((i%2)==0)s[j]->lc=q;elses[j]->rc=q;}printf("i,x=");scanf("%d%d",&i,&x);}return(t);}/*voidpreorderz(structnode*p)//前序非递归算法{structnode*q,*s[30];//s辅助栈inttop,bools;q=p;top=0;//栈顶指针bools=1;//bools=1为真值继续循环;bools=0为假时栈空,结束循环do{while(q!=NULL){printf("%6d",q->data);//访问节点top++;251 s[top]=q;q=q->lc;}if(top==0)bools=0;else{q=s[top];top--;q=q->rc;}}while(bools);printf(" ");}//////////////////////////结束preorderz*/voidpreorderz(structnode*p)//前序递归遍历{if(p!=NULL){printf("%6d",p->data);inorder(p->lc);inorder(p->rc);}}voidinorder(structnode*p)//中序非递归遍历{structnode*s[30],*q;inttop,bools;q=p;top=0;bools=1;do{while(q!=NULL){top++;s[top]=q;q=q->lc;}if(top==0)bools=0;else{q=s[top];top--;251 printf("%6d",q->data);q=q->rc;}}while(bools);}/*voidinorder(structnode*p){if(p!=NULL){inorder(p->lc);printf("%6d",p->data);inorder(p->rc);}}//////////////////////////结束inorder*/voidpostorder(structnode*p){structnode*s[30],*s2[30],*q;inttop,bools;q=p;top=0;bools=1;do{while(q!=NULL){top++;s[top]=q;s2[top]=1;q=q->lc;}if(top==0)bools=0;else{if(s2[top]==1){s2[top]=2;q=s[top];q=q->rc;}else{q=s[top];s2[top]=0;top--;251 printf("%6d",q->data);q=NULL;}}}while(bools);}voiddeletes(structnode*t,structnode*p,structnode*f){structnode*s,*q;intbools=1;if(p->lc==NULL)s=p->rc;elseif(p->rc==NULL){s=p->rc;while(s->lc!=NULL){q=s;s=s->rc;}if(q==p)q->rc=s->rc;elseq->lc=s->rc;p->data=s->data;free(s);bools=0;}if(bools==1){if(f==NULL)t=s;elseif(f->lc==p)f->lc=s;elsef->rc=s;free(p);}}/*voidpostorder(structnode*p){if(p!=NULL){postorder(p->lc);251 postorder(p->rc);printf("%6d",p->data);if(p->lc==NULL&&p->rc==NULL)m++;//统计叶子节点}}//////////////////////////结束postorder*/6.各种排序法#include#includestructnode{intkey;}r[20];structrnode{intkey;intpoint;};main(){voidprint(structnodea[20],intn);intcreat();voidshell(structnodea[20],intn);inthoare(structnodea[20],intl,inth);voidquick1(structnodea[20],intn);voidquick2(structnodea[20],intl,inth);voidheap(structnodea[20],inti,intm);voidheapsort(structnodea[20],intn);voidmerges(structnodea[20],structnodea2[20],inth1,intmid,inth2);voidmergepass(structnodea[20],structnodea2[20],intl,intn);voidmergesort(structnodea[20],intn);intyx(intm,inti);intradixsort(structrnodea[20],intn);intnum,l,h,c;structrnodes[20];c=1;while(c!=0){printf("主菜单 ");printf("1输入关键字,以-9999表示结束。 ");printf("2希尔排序 ");251 printf("3非递归的快速排序 ");printf("4递归的快速排序 ");printf("5堆排序 ");printf("6归并排序 ");printf("7基数排序 ");printf("输入选择(1--7,0表示结束):");scanf("%d",&c);switch(c){case1:num=creat();print(r,num);break;case2:shell(r,num);print(r,num);break;case3:quick1(r,num);print(r,num);break;case4:l=0;h=num-1;quick2(r,l,h);printf("outputquick2sortresult: ");print(r,num);break;case5:heapsort(r,num);break;case6:mergesort(r,num);print(r,num);break;case7:radixsort(s,num);}}}//mainendvoidprint(structnodea[20],intn){inti;for(i=0;i=1;i--)a[i].key=a[i-1].key;k=n/2;while(k>=1){for(i=k+1;i<=n;i++){a[0].key=a[i].key;j=i-k;while((a[j].key>a[0].key)&&(j>=0)){a[j+k].key=a[j].key;j=j-k;}a[j+k]=a[0];}k=k/2;}for(i=0;i=x.key))j--;if(ia[j+1].key)j++;if(a[j].key0;i--)a[i].key=a[i-1].key;for(i=n/2;i>=1;i--)heap(a,i,n);printf("输出堆排序结果: ");for(v=n;v>=2;v--){printf("%5d",a[1].key);x.key=a[1].key;a[1].key=a[v].key;a[v].key=x.key;heap(a,1,v-1);}printf("%5d",a[1].key);for(i=0;i=2*l){h1=i;mid=h1+l-1;h2=i+2*l-1;251 merges(a,a2,h1,mid,h2);i=i+2*l;}if((n-i)<=l)for(j=i;j<=n;j++)a2[j].key=a[j].key;else{h1=i;mid=h1+l-1;h2=n-1;merges(a,a2,h1,mid,h2);}}//mergepassendvoidmergesort(structnodea[20],intn){intl;structnodea2[20];l=1;while(l#include#include#include#include/*声明两种链表结构----start*/structnode_a{/*链表1-----作用:用来统计文件中字符的个数和种类(通过count)*/chardata;intcount;structnode_a*next;};typedefstructnode_anode,*list;listhead=NULL;structnodeb{/*链表2-----作用:用来建立用相应的编码代替字符的新文件*/chardata;structnodeb*next;};typedefstructnodebnode_b,*list_b;/*jiangbianmaxieruwenjian*/list_bhead_b=NULL;/*声明两种链表结构----end*//*哈夫曼算法种相关的3种结构的声明-----start*/structforest{floatweight;introot;251 };structalphabet{charsymbol;floatprobability;intleaf;char*bianma;};structtree{intlchild;intrchild;intparent;};typedefstructtree*tree_ptr,tree_node;typedefstructforest*forest_ptr,forest_node;typedefstructalphabet*alphabet_ptr,alphabet_node;tree_ptrtree1;forest_ptrforest1;alphabet_ptralphabet1;intlasttree,lastnode;intleast,second;/*哈夫曼算法种相关的3种结构的声明-----end*//**************stackdifinationstart****************/structstacknode{char*bian_ma;structstacknode*next;};typedefstructstacknodestack_node;typedefstack_node*link;linktop=NULL;voidpush(char*item){linkp;if(top!=NULL){p=(link)malloc(sizeof(stack_node));if(p==NULL){printf("Memoryallocationerror!");return;}p->bian_ma=item;p->next=top;top=p;}else{top=(link)malloc(sizeof(stack_node));if(!top){251 printf("Memoryallocationerror!");return;}top->bian_ma=item;top->next=NULL;}}voidpop(void){linkp;p=top;top=top->next;free(p);}voidmakenull(void){while(top!=NULL)pop();}intempty(){if(top==NULL)return1;elsereturn0;}char*tops(void){return(top->bian_ma);}voiddisplay_stack(links){/*显示栈重的内容*/linkptr;ptr=s;while(ptr!=NULL){printf("%s",ptr->bian_ma);ptr=ptr->next;}}/***************************stack__difinationisend************************/voiddisplay(listh){/*显示链表1*/listptr;inti=1;ptr=h->next;while(ptr!=NULL){printf("%d,%c,%d ",i,ptr->data,ptr->count);i++;ptr=ptr->next;251 }}voiddisplay_b(list_bh){/*显示链表2*/list_bptr;inti=1;ptr=h->next;while(ptr!=NULL){printf("%d,%c ",i,ptr->data);i++;ptr=ptr->next;}}voidinsert(charitem){/*用于插入元素以建立链表1*/listtemp,ptr;intflag;ptr=head->next;if(ptr==NULL){head->next=(list)malloc(sizeof(node));head->next->data=item;head->next->count=1;head->next->next=NULL;}else{while(ptr!=NULL){if(ptr->data==item){ptr->count=ptr->count+1;flag=1;}ptr=ptr->next;}ptr=head;if(flag==1)return;else{temp=ptr->next;ptr->next=(list)malloc(sizeof(node));ptr->next->data=item;ptr->next->count=1;ptr->next->next=temp;}}}voidinsert_b(charitem){/*插入元素以建立链表2*/list_bptr_b,temp_b;251 ptr_b=head_b;if(ptr_b->next==NULL){head_b->next=(list_b)malloc(sizeof(node_b));head_b->next->data=item;head_b->next->next=NULL;}else{while(ptr_b->next!=NULL){ptr_b=ptr_b->next;}ptr_b->next=(list_b)malloc(sizeof(node_b));ptr_b->next->data=item;ptr_b->next->next=NULL;}}voidsearch(void){/*搜索文件并将文件中的数据分别存入作用不同的链表中*/FILE*fp;listptr;charch;if((fp=fopen("test.txt","r"))==NULL)printf("Readingerror! ");while(!feof(fp)){ch=getc(fp);if(ferror(fp)){printf("error! ");break;}if(ch==EOF)break;insert(ch);/*插入元素进链表1*/insert_b(ch);/*插入元素进链表2*/}printf(" ");fclose(fp);}voiddisplay_struct(intn){/*显示哈夫曼算法中的3个结构树组*/inti=0;printf(" ======================================= ");printf("FOREST_STRUCT_ARRY: ");for(i=0;i<=n;i++){printf("%f,%d ",forest1[i].weight,forest1[i].root);}getch();printf(" ALPHABET_STRUCT_ARRY: ");251 for(i=0;i<=n;i++){printf("%f,%d,%c ",alphabet1[i].probability,alphabet1[i].leaf,alphabet1[i].symbol);}getch();printf(" TREE_STRUCT_ARRY: ");for(i=0;i<=2*n-1;i++)printf("%d,%d,%d ",tree1[i].lchild,tree1[i].rchild,tree1[i].parent);printf(" ====================================== ");getch();}intinit_struct(void){/*初始化哈夫曼算法中3种结构数组*/listptr;floatcount=.0;inti=1,n=0;ptr=head->next;while(ptr!=NULL){count=ptr->count+count;n++;ptr=ptr->next;}ptr=head->next;forest1=(forest_ptr)malloc(sizeof(forest_node)*n+sizeof(forest_node));alphabet1=(alphabet_ptr)malloc(sizeof(alphabet_node)*n+sizeof(alphabet_node));tree1=(tree_ptr)malloc(sizeof(tree_node)*n*2-sizeof(tree_node));forest1[0].weight=alphabet1[0].probability=0.0;forest1[0].root=alphabet1[0].leaf=0;alphabet1[0].symbol='0';while(ptr!=NULL){forest1[i].weight=alphabet1[i].probability=ptr->count/count;forest1[i].root=alphabet1[i].leaf=i;alphabet1[i].symbol=ptr->data;i++;ptr=ptr->next;}for(i=0;i<=2*n-1;i++){tree1[i].lchild=0;tree1[i].rchild=0;tree1[i].parent=0;}returnn;}voidcreat(void){/*创建正文文件test.txt*/FILE*fp,*out;charch;251 if((fp=fopen("test.txt","r++"))==NULL)printf("Createrror! ");printf("Inputthedata: ");ch=getch();putch(ch);while(ch!='#'){putc(ch,fp);ch=getch();putch(ch);}fclose(fp);}voidcreat_bianma(intnumber){/*根据哈夫曼算法建立文件中各种字符对应的编码*/inti,j,n;intp;char*bm=malloc(sizeof(char)*number);for(n=1;n<=number;n++){j=i=n;makenull();p=tree1[i].parent;while(tree1[p].parent!=0){if(tree1[p].lchild==i)push("0");elsepush("1");i=p;p=tree1[p].parent;}if(tree1[p].lchild==i)push("0");elsepush("1");strcpy(bm,"");/*目的:使创建编码文件中的各编码中间存在间隔*/while(!empty()){strcat(bm,tops());pop();}alphabet1[j].bianma=malloc(sizeof(char)*number);strcpy(alphabet1[j].bianma,bm);printf(" %c:%s",alphabet1[j].symbol,alphabet1[j].bianma);/*打印出相应的编码*/getch();}}voidwrite_new_file(intnumber){/*根据相应的编码重新建立编码文件*/251 FILE*fp;list_bptr;inti;char*ch=malloc(sizeof(char)*number);ptr=head_b->next;if((fp=fopen("bianma.txt","w"))==NULL)printf("Writeinanewfileerror!");else{while(ptr!=NULL){for(i=1;i<=number;i++){if(ptr->data==alphabet1[i].symbol){strcpy(ch,alphabet1[i].bianma);fputs(ch,fp);}}ptr=ptr->next;}}fclose(fp);}voidmain(void){inti,num;charch;voidhuffman(void);voidlightones();head=(list)malloc(sizeof(node));head->next=NULL;head->data=' ';head->count=0;head_b=(list_b)malloc(sizeof(node_b));head_b->data=' ';head_b->next=NULL;do{system("cls");creat();search();printf(" lianbiao1: ");display(head);/*显示链表1*/getch();printf(" lianbiao2: ");display_b(head_b);getch();num=init_struct();printf(" ");251 printf("The3init_structofhuffman? ");display_struct(num);/*显示初始化的哈夫曼书的相关3个结构数组*/lastnode=num;lasttree=num;huffman();printf("Nowthe3structthroughhuffmanshuanfa ");display_struct(num);/*显示哈夫曼树中相关的3种结构(经过哈夫曼算法处理)*/printf(" Thebian_mais: ");creat_bianma(num);write_new_file(num);printf(" Doyouwanttore_try(y/n)?");ch=getchar();}while(ch=='y');}/*哈夫曼算法-----defination_start*/voidlightones(void){inti;if(forest1[1].weight<=forest1[2].weight){least=1;second=2;}else{least=2;second=1;}for(i=3;i<=lasttree;i++)if(forest1[i].weight1){lightones();i=least;j=second;newroot=creat_tree(i,j);forest1[i].weight=forest1[i].weight+forest1[j].weight;forest1[i].root=newroot;forest1[j]=forest1[lasttree];lasttree=lasttree-1;}}8.哈慢树#include#include#include#definen6#definem2*n-1typedefstruct{floatweight;intlchild,rchild,parent;}codenode;typedefcodenodehuffmantree[m];typedefstruct{charch;charbits[n+1];}code;typedefcodehuffmancode[n];voidinithf(huffmantreeT)//-哈夫曼树的初始化{inti;for(i=0;i