- UID
- 2198
注册时间2005-6-29
阅读权限255
最后登录1970-1-1
副坛主
该用户从未签到
|
汉诺塔源程序连接:https://www.chinapyg.com/viewthread.php?tid=46795
main.c 是汉诺塔的精简版(main.c and stack.h)
这个代码对我一点用也没有了 当逻辑有了之后 这堆东西就一文不值了
自己玩就行哈 有不懂的找小奇就行了
/********************** main.c ***********/
#include "stdio.h"
#include "stack.h"
#define num 5
Stack * sp[3];
ElementType str[]={5,4,3,2,1};
int x[]={10,20,30};
int y;
int Putch(ElementType * data,int i)
{
gotoxy(x[i],y--);
printf("%d",*data);
}
ShowStack()
{
int i;
clrscr();
for(i=0;i<3;i++)
{
y=10;
TraversStack(sp[i],Putch,i);
}
}
Change(int a,int b)
{
ElementType cc;
Pop(sp[a],&cc);
Push(sp[b],cc);
getchar();
ShowStack();
}
mov(int n,int a,int b,int c)
{
if(n<1)return;
mov(n-1,a,c,b);
Change(a,c);
mov(n-1,b,a,c);
}
main()
{
int i;
for(i=0;i<3;i++)
{
sp[i]=CreateStack(10);
if(!sp[i])return ;
}
for(i=0;i<num;i++)
{
Push(sp[0],str[i]);
}
ShowStack();
mov(num,0,1,2);
}
/*********************** main.c end ******************/
/* --------------------- stack.h Code By: Nisy/PYG ------------------- */
typedef int ElementType;
typedef struct
{
ElementType * buffer;
int top;
int max;
}Stack;
Stack * CreateStack(int);
int Push(Stack *,ElementType);
int Pop(Stack *,ElementType * );
int IsFull(Stack *);
int IsEmpty(Stack *);
int TraversStack(Stack *,int (*f)(ElementType *,int),int );
Stack * CreateStack(int n)
{
Stack *sp;
sp=(Stack *)malloc(sizeof(Stack));
if(!sp)return 0;
sp->buffer=(ElementType * )malloc(sizeof(ElementType)*n);
if(!sp->buffer)
{
free(sp);
return 0;
}
sp->top=-1;
sp->max=n;
return sp;
}
int Push(Stack *sp,ElementType data)
{
if(IsFull(sp))return 0;
sp->top++;
memcpy(sp->buffer+sp->top,&data,sizeof(ElementType));
return 1;
}
int Pop(Stack *sp,ElementType * data)
{
if(IsEmpty(sp))return 0;
memcpy(data,sp->buffer+sp->top,sizeof(ElementType));
sp->top--;
return 1;
}
int IsFull(Stack * sp)
{
return sp->top+1==sp->max;
}
int IsEmpty(Stack *sp)
{
return sp->top==-1;
}
int TraversStack(Stack *sp,int (*f)(ElementType *,int),int x)
{
int i;
if(IsEmpty(sp))return 0;
for(i=0;i<=sp->top;i++)
{
f(sp->buffer+i,x);
}
return 1;
}
/* --------------------- stack.h End ------------------- */ |
|