西卡C语言汉诺塔演示程序
来源:互联网 发布:js haschildnodes 编辑:程序博客网 时间:2024/05/16 14:31
下面是西卡学院C语言汉诺塔演示程序。以前学习没有了解那里清楚,发现数据结构的应用很广啊。显示几个栈底是复制的别人的代码,再此表示感谢了。(VC++6.0下面可以运行)
stack.h内容
#define NULL 0typedef int ElementType;typedef struct{ ElementType *pbuffer; int max; int top;}Stack;Stack *InitStack(int n);int Push(Stack *sp,ElementType *pdata);int Pop(Stack *sp,ElementType *pdata);int DestroyStack(Stack *sp);int IsEmpty(Stack *sp);int IsFull(Stack *sp);int TravereStack(Stack *sp,int (*pfn)(ElementType *pdata,int ,int ),int x,int y);
stack.c内容
#include "stack.h"#include <malloc.h>#include <stdio.h>#include <string.h>Stack *InitStack(int n){ Stack *sp = NULL;sp = (Stack *)malloc(sizeof(Stack));if(!sp){ return sp;} sp->pbuffer = (ElementType *)malloc(sizeof(ElementType)*n);if(!sp->pbuffer){ free(sp); sp=NULL; return sp;}sp->max = n;sp->top = -1; return sp;}int Push(Stack *sp,ElementType *pdata){ if(IsFull(sp)) { return 0; } sp->top++; //sp->pbuffer[sp->top] = *pdata; memcpy(sp->pbuffer + sp->top,pdata,sizeof(ElementType)); return 1;}int Pop(Stack *sp,ElementType *pdata){ if(IsEmpty(sp)) { return 0; } *pdata = sp->pbuffer[sp->top]; sp->top--; return 1;}int DestroyStack(Stack *sp){ if(sp) { free(sp->pbuffer); free(sp); return 1; } return 0;}int IsEmpty(Stack *sp){ return sp->top == -1;}int IsFull(Stack *sp){ return sp->top == sp->max;}int TravereStack(Stack *sp,int (*pfn)(ElementType *pdata,int x,int y),int x,int y){ int i =0; for(i=0;i<sp->top+1;i++) { pfn(sp->pbuffer+i,x,y); y--; } printf("\n"); return 1;}
汉诺塔主体函数,没有进行优化。
#include "stack.h"#include <stdio.h>#include <malloc.h>#include <windows.h>#include <conio.h>int step = 0;typedef struct{ Stack * sp[3]; int x[3]; int y; int total;}Hannuota;void gotoxy(int x,int y){COORD C;C.X = x;C.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),C);}void FreeThreeStack(Hannuota *han){ int i =0;for(;i<3;i++){ if(han->sp[i]){ free(han->sp[i]);han->sp[i] =0;}}}int DestroyHannuota(Hannuota *han){ if(han){ FreeThreeStack(han); free(han); return 1;}return 0;}Hannuota * InitHannuota(int n){int i =0; Hannuota * han = NULL;han = (Hannuota *)malloc(sizeof(Hannuota)); if(!han){ return han;}han->total = n;han->y = 10; for(i=0;i<3;i++){ han->sp[i] = 0; han->x[i] = 10*i+10;} for(i=0;i<3;i++){ han->sp[i] = InitStack(han->total); if(!han->sp[i]) { DestroyHannuota(han); return han; }} return han;}int ShowElement(ElementType *pdata,int x,int y){ gotoxy(x,y); printf("%d",*pdata); return 1;}int ShowFace(Hannuota * ph){ int i ; gotoxy(8,3); printf("Hanio Game Ver 0.1"); for(i=0 ; i<3 ;i++) { gotoxy(ph->x[i] - 2 , ph->y + 1); printf("-----"); gotoxy(ph->x[i] , ph->y + 2); printf("%c",0x41 + i); } return 1;}int ShowHannuota(Hannuota *han){ int i =0; //clrscr();system("CLS");ShowFace(han); for(;i<3;i++) { TravereStack(han->sp[i],ShowElement,han->x[i],han->y); } gotoxy(8,14); printf("Step is No. %d ",step++);getch();return 1;}int ChangeData(Hannuota *han,int a,int b){ ElementType data;Pop(han->sp[a],&data);Push(han->sp[b],&data); ShowHannuota(han);//getchar();return 1;}void Mov(Hannuota *han,int num,int a,int b,int c){ if(num<1){ return;} Mov(han,num-1,a,c,b); ChangeData(han,a,c); Mov(han,num-1,b,a,c);}int GameStart(Hannuota *han){ int i = han->total; for(;i>0;i--) { Push(han->sp[0],&i); } ShowHannuota(han); Mov(han,han->total,0,1,2); return 1;}int main(){ Hannuota *han = InitHannuota(3);if(!han){ return 0;} GameStart(han);DestroyHannuota(han); return 1;}
具体效果如下:
面向对象的实现,请大牛赐教。
- 西卡C语言汉诺塔演示程序
- C 语言标准 I/O 函数介绍以及演示程序
- Linux下演示编辑和运行C语言程序
- 用C语言显示汉字的演示程序
- 关于C语言数组越界的演示程序
- Linux下演示编辑和运行C语言程序
- C语言位操做符代码演示
- 用java语言实现汉诺塔的演示程序
- 汉诺塔问题算法(c语言控制台动画演示版)
- c语言汉诺塔演示程序设计(基于堆栈、递归)
- C语言用图形函数实现电脑自动演示汉诺塔
- XFC演示-使用C语言通过WinAPI函数编写Windows程序
- 本程序演示了C语言中回调函数的使用方法
- C语言排序算法演示:冒泡法
- 单链表操作演示----C语言实现
- 数据结构之c语言栈的演示
- C语言排序算法的演示
- 哈夫曼编/译码演示系统的C程序
- Java菜鸟修习日志第0篇——一个错误的小程序Clamp
- [转载]iostream格式化输出
- mac 上的版本控制工具SmartSVN(破解版)
- linux系统内存怎么手工释放
- java 获取cookie里的值
- 西卡C语言汉诺塔演示程序
- P6800- 入手P6800 先root一下
- 神经网络编程入门
- Java日期操作(待补充)
- request对象中文编码转换
- IDC提供的负载均衡服务
- 感慨颇多:清华差生10年奋斗经历
- Windows下OpenSSL安装配置
- ios网络开源类