栈和递归

最近想复习一下数据结构,写了一些常见问题的代码(VC6.0平台)

问题1:Hanoi塔问题

#include  < iostream.h >

void  Move( int  array[],  int  k,  char  source,  char  desti)
{
    
static count=1;
    cout
<<count++<<"th step:";
    cout
<<"Move the "<<array[k]<<"th plate from "<<source<<" to "<<desti<<endl;
}



// m为小头,k为大头,从上而下,从小到大
void  Hanoi( int  array[], int  m,  int  k, char  source,  char  desti, char  help)
{
    
if(k>m)
    
{
        Hanoi(array, m, k
-1,source,help,desti);
        Move(array, k, source,desti);
        Hanoi(array, m, k
-1, help , desti, source);
    }

    
else
        Move(array,k,source,desti);
}


void  main()
{
    
const int num=8;
    
int array[num];
    
for(int i=0; i<num; i++)
        array[i]
=i+1;
    Hanoi(array,
0,num-1,'A','B','C');
}

 

问题2:迷宫问题

先用模板实现了一个堆栈结构并定义了常见操作

Stack.h

 

#ifndef  STACK_HUANGYEQUAN_071013
#define   STACK_HUANGYEQUAN_071013

#include
" ElemType.h "
#include
< iostream.h >
#include 
< stdlib.h >


#define  OVERFLOW -2
#define  TRUE  1
#define  FALSE 0
#define  OK 1
#define  ERROR 0
#define  INFEASIBLE -1

#define  STACK_INIT_SIZE 100
#define  STACKINCREMENT  10


template
< class  ElemType = int >
class  CsqStack
{
    
//Attribute
public:
    ElemType 
*base;
    ElemType 
*top;
    
int stacksize;      //当前已分配的存储空间,以元素为单位

    
//construction and destruction
public:
    CsqStack();
    CsqStack(
const CsqStack & S);

    
~CsqStack();

    
//Operation
public:
    
bool ClearStack();
    
bool isStackEmpty();
    
int StackLength();
    ElemType GetTop();
    
bool Push(ElemType elem);  //插入新的栈顶元素,并返回当前的栈的长度
    ElemType Pop();
    

    
void print();

    
void StackTraverse();
}
;


template
< class  ElemType >
CsqStack
< ElemType > ::CsqStack()
{
    
base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
    
if(!base)
        exit(OVERFLOW);
    stacksize
=STACK_INIT_SIZE;
    top
=base;
}


template
< class  ElemType >
CsqStack
< ElemType > ::CsqStack( const  CsqStack  &  S)
{
    stacksize
=S.stacksize;
    
base=(ElemType*)malloc(stacksize*sizeof(ElemType));
    
int i=0;
    
while(S.top!=S.base+i)
    
{
        
*(base+i)=*(S.base+i);
        i
++;
    }

    top
=base+i;
}


template
< class  ElemType >
CsqStack
< ElemType > :: ~ CsqStack()
{
    free(
base);
}



// Operation
template < class  ElemType >
bool  CsqStack < ElemType > ::Push(ElemType elem)
{

    
if(top-base>=stacksize)
    
{
        
base=(ElemType*)realloc(base,(stacksize+STACKINCREMENT)*sizeof(ElemType));
        
if(!base)exit(OVERFLOW);
        top
=base+stacksize;
        stacksize
=stacksize+STACKINCREMENT;
    }

    
*top++=elem;
    
return true;
}


template
< class  ElemType >
void  CsqStack < ElemType > ::print()
{
    
int num=top-base;
    
if(!num)
        cout
<<endl<<"There is no element in the stack!"<<endl;
    
for(int i=0; i<num; i++)
    
{
        cout
<<*(base+i)<<' ';
        
if(!((i+1)%10))
            cout
<<endl;
    }

    cout
<<endl;
}


template
< class  ElemType >
ElemType CsqStack
< ElemType > ::Pop()
{
    ElemType elem;
    
if(base==top)
        
return    elem;      
    elem
=*(--top);
    
return elem;
}


template
< class  ElemType >
bool  CsqStack < ElemType > ::isStackEmpty()
{
    
if(base==top)return    true;

    
return false;
}


template
< class  ElemType >
ElemType CsqStack
< ElemType > ::GetTop()
{
    
if(base==top)return    ERROR;
    ElemType elem
=*(top-1);

    
return elem;
}


template
< class  ElemType >
bool  CsqStack < ElemType > ::ClearStack()
{
    
while(base!=top)
    
{
        Pop();
    }

    
return    true;
}


template
< class  ElemType >
int  CsqStack < ElemType > ::StackLength()
{
    
return    top-base;
}


#endif

此过程中,曾经错误的在析构函数中用delete导致了“_BLOCK_TYPE_IS_VALID”的错误

曾在Pop中,若栈为空,则return ERROR;根据宏定义,此时的返回值是0,一个int型,与ElemType是不一致的。

 

CElemType.h

 

#ifndef  CELEMTNET_HUANGYEQUAN_071013
#define   CELEMTNET_HUANGYEQUAN_071013

class  CElemType
{
public:
    
int m_ord;
    
int m_x;
    
int m_y;
    
int m_dir;

public:
    CElemType();
    CElemType(CElemType 
&elem);
    CElemType(
int ord,int x,int y,int dir);
    
~CElemType();

public:
    
void Set(int ord,int x,int y,int dir);
}
;


CElemType::CElemType()
{
    m_ord
=0;
    m_x
=0;
    m_y
=0;
    m_dir
=0;

}


CElemType::CElemType(CElemType 
& elem)
{
    m_ord
=elem.m_ord;
    m_x
=elem.m_x;
    m_y
=elem.m_y;
    m_dir
=elem.m_dir;
}


CElemType::CElemType(
int  ord, int  x, int  y, int  dir)
{
    m_ord
=ord;
    m_x
=x;
    m_y
=y;
    m_dir
=dir;
}


CElemType::
~ CElemType()
{

}


void  CElemType::Set( int  ord, int  x, int  y, int  dir)
{
    m_ord
=ord;
    m_x
=x;
    m_y
=y;
    m_dir
=dir;
}

#endif

这是迷宫路径中的一点的位置和方向等的记录。

Maze.cpp

 

#include  " ElemType.h "
#include 
" Stack.h "

#define  EAST 1
#define  SOUTH 2
#define  WEST 3
#define  NORTH 4

const   int  height = 10 ;
const   int  width = 10 ;
int  map[height][width] =
{
    
{1,1,1,1,1,1,1,1,1,1},
    
{1,0,0,1,0,0,0,1,0,1},
    
{1,0,0,1,0,0,0,1,0,1},
    
{1,0,0,0,0,1,1,0,0,1},
    
{1,0,1,1,1,0,0,0,0,1},
    
{1,0,0,0,1,0,0,0,0,1},
    
{1,0,1,0,0,0,1,0,0,1},
    
{1,0,1,1,1,0,1,1,0,1},
    
{1,1,0,0,0,0,0,0,0,1},
    
{1,1,1,1,1,1,1,1,1,1},
}
;


bool  MazePath(CsqStack < CElemType >   & stack)
{
    CElemType elem;
    
int xStart=1;
    
int yStart=1;

    
int xEndPos=8;
    
int yEndPos=8;

    
int xCurPos=xStart;
    
int yCurPos=yStart;

    
int curStep=1;
    
do
    
{
        
if(!map[yCurPos][xCurPos]&&map[yCurPos][xCurPos]!=3)  //未曾走到过的通道块
        {
            map[yCurPos][xCurPos]
=3;   //留下足迹
            elem.Set(curStep,xCurPos,yCurPos,EAST);
            stack.Push(elem);
            
if(xCurPos==xEndPos&&yCurPos==yEndPos)
                
return TRUE;
            xCurPos
++;   //东邻
            curStep++;   //探索下一步
        }

        
else //当前位置不能通过
        {
            
if(!stack.isStackEmpty())
            
{
                elem
=stack.Pop();
                
while(elem.m_dir==NORTH&&!stack.isStackEmpty())
                    elem
=stack.Pop();
            }

            
if(elem.m_dir<NORTH)
            
{
                elem.m_dir
++;
                stack.Push(elem);
                xCurPos
=elem.m_x;
                yCurPos
=elem.m_y;
                
switch(elem.m_dir)
                
{
                
case 2:
                    yCurPos
++;
                    
break;
                
case 3:
                    xCurPos
--;
                    
break;
                
case 4:
                    yCurPos
--;
                    
break;
                
default:
                    
break;
                }

            }

        }


    }
while(!stack.isStackEmpty());
    
return false;
}


void  main()
{
    CsqStack
<CElemType> stack;
    MazePath(stack);
    CElemType elem;
    
for(int i=0; i<stack.StackLength(); i++)
    
{
        elem
=*(stack.base+i);
        
switch(elem.m_dir)
        
{
        
case 1:
            cout
<<"Move to east at ("<<elem.m_x<<','<<elem.m_y<<")"<<endl;
            
break;
        
case 2:
            cout
<<"Move to south at ("<<elem.m_x<<','<<elem.m_y<<")"<<endl;
            
break;
        
case 3:
            cout
<<"Move to west at ("<<elem.m_x<<','<<elem.m_y<<")"<<endl;
            
break;
        
case 4:
            cout
<<"Move to north at ("<<elem.m_x<<','<<elem.m_y<<")"<<endl;
            
break;
        
default:
            
break;
        }

    }

}
这是求解迷宫的主体,先定义了一个二维数组map作为迷宫。此处是一个全局数组,我曾尝试着以函数参数形式传给MazePath,指针和引用类型都试了,还有模板(将长高以非类型参数),好像都不行,需要进一步看一下。

你可能感兴趣的:(栈和递归)