체인 스택 클래스

1428 단어
/*
 
*/
#include <iostream>
#include <cstdio>
using namespace std;

template <class T>class LinkStack;
template <class T>
class Node
{
    private:
        friend class LinkStack<T>;
        Node<T> *next;
        T data;
    public:
        Node( T d=0,Node<T> *n=NULL ):data(d),next(n) {}
};

template <class T>
class LinkStack
{
    private:
        Node<T>* top;
    public:
        LinkStack():top(NULL) {}
        ~LinkStack();
        bool push( T d );
        T gettop();
        bool pop();
};

template <class T>
LinkStack<T>::~LinkStack()
{
    Node<T> *p;
    while( top!=NULL )
    {
        p=top;
        top=top->next;
        delete p;
    }
}

template <class T>
bool LinkStack<T>::push( T d )
{
    Node<T> *p=new Node<T>( d,top );
    top=p;
}

template <class T>
bool LinkStack<T>::pop()
{
    if( top==NULL ) return false;
    top=top->next;
}

template <class T>
T LinkStack<T>::gettop()
{
    if( top==NULL ) return NULL;
    return top->data;
}

int main()
{
    LinkStack<char> test;
    test.push('a');
    test.push('b');
    test.push('c');
    test.push('d');
    while( test.gettop()!=NULL )
    {
        cout<<test.gettop()<<" ";
        test.pop();
    }
    return 0;
}

좋은 웹페이지 즐겨찾기