[자료구조] Stack

자료 2015. 12. 5. 15:29
반응형

#include <iostream>

//

// Class template 생성을 위해서는 typename을 T로 해주면 된다.

//

using namespace std;


template<typename T>

class Stack;


template<typename nodetype = int>

class node{

    private:

        nodetype data;

        node<nodetype> *next;

    public:

        node(nodetype in, node<nodetype> *conn) : data(in), next(conn) { }

        friend class Stack<nodetype>;

};


template<typename stacktype = int>

class Stack{

    private:

        node<stacktype> *top;

    public:

        Stack() : top(NULL) { };

        ~Stack(){

            node<stacktype> *temp = top;

            while(temp->next != NULL){

                temp = top->next;

                delete temp;

            }

            delete top;

        }

        node<stacktype> *push(stacktype in);

        bool pop(stacktype &ret);

        friend ostream& operator<<(ostream &os, const stacktype &data){

            os << data;

            return os;

        }

};


template <typename stacktype>

node<stacktype> *Stack<stacktype>::push(stacktype in){

    top = new node<stacktype>(in, top);

    return top;

}


template <typename stacktype>

bool Stack<stacktype>::pop(stacktype &ret){

    if(top == NULL)

        return false;


    ret = top->data;

    top = top->next;

    return true;

}


template <>

bool Stack<string>::pop(string &ret){

    if(top == NULL)

        return false;


    ret = top->data;

    top = top->next;

    return true;

}

'자료' 카테고리의 다른 글

[Python] Rop Exploit  (0) 2015.12.05
[Python] Blind SQL Injection  (0) 2015.12.05
Double Free Bug  (1) 2015.10.03
힙(Heap)  (0) 2015.08.06
[C++] PE Viewer 미완  (0) 2015.07.05
블로그 이미지

KuroNeko_

KuroNeko

,