当前位置:编程学习 > C/C++ >>

西卡C语言汉诺塔演示程序

下面是西卡学院C语言汉诺塔演示程序。以前学习没有了解那里清楚,发现数据结构的应用很广啊。显示几个栈底是复制的别人的代码,再此表示感谢了。(VC++6.0下面可以运行)
stack.h内容
[cpp] 
#define NULL 0 
 
typedef int ElementType; 
 
typedef struct 
{  www.zzzyk.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"); 
  &

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,