下面是西卡學院C語言漢諾塔演示程序。以前學習沒有了解那裡清楚,發現數據結構的應用很廣啊。顯示幾個棧底是復制的別人的代碼,再此表示感謝了。(VC++6.0下面可以運行)
stack.h內容
[cpp]
#define NULL 0
typedef int ElementType;
typedef struct
{ www.2cto.com
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內容
[cpp]
#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;
}
漢諾塔主體函數,沒有進行優化。
[cpp]
#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;
}
具體效果如下:
面向對象的實現,請大牛賜教。