티스토리 뷰

공부 이야기

[C] Stack?

판다(panda) 2009. 1. 7. 00:00
Stack.h
// Stack.h: interface for the CStack class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_STACK_H__EC159708_EAB9_4CD6_BAC2_4312A0F48F05__INCLUDED_)
#define AFX_STACK_H__EC159708_EAB9_4CD6_BAC2_4312A0F48F05__INCLUDED_
#if    _MSC_VER > 1000
#pragma once
#endif
// _MSC_VER > 1000
#define            MAX_STACK          10
#define            STACK_EMPTY      -99999
class CStack
{
public:
                      CStack();
       virtual       ~CStack();
       int            Push(int nData);
       int            Pop();
private:
       int            m_Stack[MAX_STACK],
                      m_nIndex;
};
#endif // !defined(AFX_STACK_H__EC159708_EAB9_4CD6_BAC2_4312A0F48F05__INCLUDED_)
 
 
Stack.cpp
#include "Stack.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStack::CStack()
{
       m_nIndex       = -1;
}
CStack::~CStack()
{
}
int CStack::Push(int nData)
{
       if(m_nIndex >= MAX_STACK-1)
               return 0;
       m_nIndex++;
       m_Stack[m_nIndex]      = nData;
 
       return 1;
}
int CStack::Pop()
{
       if(m_nIndex < 0)
              return STACK_EMPTY;
       return m_Stack[m_nIndex--];
}
 
 
main.cpp
#include   <stdio.h>
#include   "Stack.h"
void main()
{
         CStack Stack;
         int  i, nPopData;
         for(i=0; i<MAX_STACK; ++i)
    {
         Stack.Push(i);
         printf("Push : %d \n", i);
    }
         for(i=0; i<MAX_STACK; ++i)
    {
         nPopData = Stack.Pop();
         printf("Pop : %d \n", nPopData);
    }
}
 
Result