以下程序可通过编译,但是运行出错,怎么办
我的代码是用两个STACK模仿队列,一个是STACKIN,一个是STACKOUT,STACKOUT是做为队列输出的,_STAKCIN是辅助_STACKOUT调换元素顺序的。
我做的COMMENTS比较少,因为马上要去上课了,而且先前就没有做过什么COMMENTS。有点呦!不好意思!
/////Stack_Queue.h//////////////
#include <stdio.h>
struct _StackIn;
struct _StackOut;
/*typedef struct _AFX_INIT
{
_AFX_INIT(_StackIn *pIn,_StackOut *pOut);
}AFX_INIT;*/
/*Define two stacks*/
template <class _Stack>
int Pop(_Stack *pHead);template <class _Stack>
void Push(_Stack *pHead,int data);template <class _Stack>
int GetLength(_Stack *pHead);template <class _Stack>
void ClearStack(_Stack *pHead);template <class _Stack>
bool IsEmpty(_Stack *pHead);
//////////////////////////////////////////
//////////////////////////////////////////
typedef struct _StackIn
{
_StackIn();
int data;      //assume the date's type is int;
_StackIn *m_pNext;   //pointer to next element;
static _StackIn *m_pHead;
static _StackIn *m_pBase;}StackIn;
/////////////////////////////////////////
typedef struct _StackOut
{
_StackOut();
int data;
static _StackOut *m_pHead;
static _StackOut *m_pBase;
_StackOut *m_pNext;
}StackOut;
////////////////////////////////////////
typedef struct _Queue
{
_Queue();
StackIn *m_pIn;
StackOut *m_pOut;
int GetLength();
void ClearQueue();
StackOut *GetHead();
void InQueue(int);
int OutQueue();
bool IsEmpty();
~_Queue();
}Queue;
////////////////////////////////////
/////////Stack_Queue.cpp///////////
////////////////////////////////////
#include "Stack_Queue.h"
/*Initialize the static data members*/
_StackIn *_StackIn::m_pHead=new _StackIn;
_StackIn *_StackIn::m_pBase=_StackIn::m_pHead;
_StackOut *_StackOut::m_pHead=new _StackOut;
_StackOut *_StackOut::m_pBase=_StackOut::m_pHead;
////////////////////////////////////
_StackIn::_StackIn()
{}
//////////////////////////////////////
_StackOut::_StackOut()
{}
//////////////////////////////
template <class _Stack>
void ClearStack(_Stack *pHead)
{
if(!pHead || !_Stack::m_pBase)
return;
while(pHead!=_Stack::m_pBase)
{
_Stack *pTemp=pHead;
pHead=pHead->m_pNext;
delete pTemp;
}
pHead->data=0;  ///
_Stack::m_pHead=pHead;
return;
}template <class _Stack>
int GetLength(_Stack *pHead)
{
return (pHead-_Stack::m_pBase)/sizeof(_Stack)+1;
}template <class _Stack>
bool IsEmpty(_Stack *pHead)
{
return (_Stack::m_pBase==pHead && pHead->data==0);
}template <class _Stack>
void Push(_Stack * pHead,int data)
{
if(_Stack::m_pHead==_Stack::m_pBase && _Stack::m_pBase->data==0)
{
_Stack::m_pHead->data=data;
//delete pHead;
return;
}
pHead->data=data;
pHead->m_pNext=_Stack::m_pHead;
_Stack::m_pHead=pHead;
return;
}template <class _Stack>
int Pop(_Stack *pHead)
{
if(!_Stack::m_pBase || !pHead || _Stack::m_pBase==pHead && pHead->data==0)
return 0;
int tempdata=pHead->data;
_Stack *pTemp=_Stack::m_pHead;
pHead=pHead->m_pNext;
delete pTemp;
_Stack::m_pHead=pHead;
return tempdata;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
_Queue::_Queue()
{
m_pIn=StackIn::m_pHead;
m_pOut=StackOut::m_pHead;
}
StackOut *_Queue::GetHead()
{
return StackOut::m_pHead;
}
void _Queue::ClearQueue()
{
ClearStack(m_pIn);
ClearStack(m_pOut);
m_pIn=_StackIn::m_pHead;
m_pOut=_StackOut::m_pHead;
}
void _Queue::InQueue(int data)
{
m_pIn=new _StackIn;
while(m_pOut!=_StackOut::m_pBase)
{
Push(m_pIn,Pop(m_pOut));
m_pIn=_StackIn::m_pHead;
m_pOut=m_pOut->m_pNext;
}
Push(m_pIn,Pop(m_pOut));
m_pOut->data=0;
Push(m_pOut,data);
while(m_pIn!=_StackIn::m_pBase)
{
Push(m_pOut,Pop(m_pIn));
m_pOut=_StackOut::m_pHead;
m_pIn=m_pIn->m_pNext;
}
Push(m_pOut,Pop(m_pIn));
m_pOut=_StackOut::m_pHead;
return;
}
int _Queue::OutQueue()
{
int temp=Pop(m_pOut);
m_pOut=_StackOut::m_pHead;
return temp;
}
int _Queue::GetLength()
{
return ::GetLength(m_pOut);
}
bool _Queue::IsEmpty()
{
return ::IsEmpty(m_pOut);
}
_Queue::~_Queue()
{
}
////////////////////////////////////////
//////////////link.cpp(main() in it)/////////////////
////////////////////////////////////////
#include "Stack_Queue.h"void main()
{ _Queue queue;
for(i=1;i<=100;i++)
{
queue.InQueue(i);
while(!queue.IsEmpty())
printf("%d",queue.OutQueue());
}
////////////////////////////////////////////////////