본문 바로가기

자작 프로그램

C++로 구현해 본 간단 스택

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

내가 처음 C++ 을 공부할때와는 문법이 약간 달라진듯 하다.
아래 소스는 현재 통용되는 C++ 문법에 따라 자료구조를 다시 공부하는 중에
한번 만들어 본 스택 구현이다.

BitField라던가, 예외처리, typeid 같은거는 처음 배울 당시 책에는 없었던 기능들이었는데...
최근에 나온 C++0x는 람다식도 지원한다니.

C++이 갈수록 막강해지는 것 같아 재밌다.

/* stdafx.h */
#ifndef __STDAFX_H
#define __STDAFX_H
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#endif
/* tnode.h */
#ifndef __TNODE_H
#define __TNODE_H
namespace Tapito
{
    template<typename T>
    class TNode
    {
    public:
    operator T()
    {
        return this->m_Data;
    }     operator T*()     {         return &(this->m_Data);     }     TNode()     {         this->m_lpFirstNode = NULL;         this->m_lpPrevNode = NULL;         this->m_lpNextNode = NULL;         this->m_lpLaseNode = NULL;     }     TNode(const T node)     {         this->m_lpFirstNode = NULL;         this->m_lpPrevNode = NULL;         this->m_lpNextNode = NULL;         this->m_lpLaseNode = NULL;         this->m_Data = node;     }     T GetData(void)     {         return this->m_Data;     }     void SetData(T data)     {         this->m_Data = data;     }     private:     TNode<T> * m_lpFirstNode;     TNode<T> * m_lpPrevNode;     TNode<T> * m_lpNextNode;     TNode<T> * m_lpLaseNode;     T m_Data;     }; }

 

- tstack.h -

#ifndef __TSTACK_H
#define __TSTACK_H

#include "stdlib.h"
#include "tnode.h"

using namespace std;

namespace Tapito
{
    template<typename T, int capacity = 255>
    class TStack
    {
    public:
    TStack()
    {
        this->m_iTop = -1;
    }
    TStack(TStack<T, capacity>& tstack)
    {
        const TNode<T> * temp = tstack.m_aStack;
        int count = 0;
        while(count < capacity)
        {
        this->m_aStack = *temp++;
        count++;
        }
    }
    int GetCapacity() const
    {
        return capacity;
    }
    int GetCount() const
    {
        return this->m_iTop + 1;
    }
    T Pop()
    {
        if(m_iTop < 0)
        {
            char msgBuffer[256];
            sprintf(msgBuffer, "Tapito::TSTack<T, capacity>::Pop # Stack is empty # 'm_iTop' = %d", this->m_iTop);
            string exceptionMessage = msgBuffer;
            throw out_of_range(exceptionMessage);
        }
        return (T)this->m_aStack[m_iTop--];
    }
    void Push(T element)
    {
        if(this->m_iTop >= capacity - 1)
        {
            char msgBuffer[256];
            sprintf(msgBuffer, "Tapito::TSTack<T, capacity>::Pop # Stack is full # 'capacity' = %d / 'm_iTop' = %d", capacity, this->m_iTop);
            string exceptionMessage = msgBuffer;
            throw out_of_range(exceptionMessage);
        }
        this->m_aStack[++(this->m_iTop)] = (TNode<T>)element;
    }
    T GetElement(int index)
    {
        if(!((0 <= index) && (index <= this->m_iTop)))
        {
            char msgBuffer[256];
            sprintf(msgBuffer, "Tapito::TStack<T, capacity>::GetElement # 'index' is out of range # 'm_iTop' = %d / 'index' = %d", this->m_iTop, index);
            string exceptionMessage = msgBuffer;
            throw out_of_range(exceptionMessage);
        }
        return (T)this->m_aStack[index];
    }
    void SetElement(int index, T element)
    {
        if(!((0 <= index) && (index <= this->m_iTop)))
        {
            char msgBuffer[256];
            sprintf(msgBuffer, "Tapito::TStack<T, capacity>::GetElement # 'index' is out of range # 'm_iTop' = %d / 'index' = %d", this->m_iTop, index);
            string exceptionMessage = msgBuffer;
            throw out_of_range(exceptionMessage);
        }
        this->m_aStack[index] = (TNode<T>)element;
    }
    private:
    TNode<T> m_aStack[capacity];
    int m_iTop;
    };
}
#endif

 

 /* main.cpp */
#include <iostream>
#include <iomanip>
#include <conio.h>
#include "tstack.h"

void printstack(Tapito::TStack<int>& tstack);

void main()
{
    try
    {
        Tapito::TStack<int> tstack;
        cout<<"== 1차 =="<<endl;
        printstack(tstack);
        tstack.push(1);
        cout<<"== 2차=="<<endl;"
        tstack.push(-1);
        tstack.push(100);
        tstack.push(200);
        tstack.push(300);
        cout<<"== 3차=="<<endl;
        tstack.pop();
        cout<<"== 4차=="<<endl;
    }
    catch(exception e)
    {
    
        cout<<e.what()<<endl;
        throw e;
    }
    catch(...)
    {
        cout<<"unknown exception"<<endl;
        throw;
    }
    cout<<"end"<<endl;
    _getch();
}
void printstack(tapito::tstack<int>& tstack)
{
    cout<<"STACK : CAPACITY = "<<tstack.getcapacity()<<", elements="<<tstack.GetCount()<<endl;
    for(int i = 0; i < tstack.GetCount(); i++)
    {
        cout<<" stack["<<setfill('0')<<setw(8)<<i<<"]== "<<tstack.getelement(i)<<endl;
    }
}

실행 결과