체인 대기열 클래스

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

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

template <class T>
class LinkQueue
{
    private:
        Node<T> *front,*rear; //front ,rear 
    public:
        LinkQueue()
        {
            Node<T> *p=new Node<T>;
            front=p;    rear=p;
        }
        bool push( T d );
        bool pop();
        T top();
        bool empty();
};

template <class T>
bool LinkQueue<T>::push( T d )
{
    Node<T> *p=new Node<T>;
    p->data=d;
    rear->next=p;
    rear=rear->next;
    return true;
}

template <class T>
bool LinkQueue<T>::pop()
{
    Node<T> *p;
    if( front==rear )   return false;
    p=front->next;
    front->next=p->next;
    if( p->next==NULL ) rear=front;
    delete p;
    return true;
}

template <class T>
T LinkQueue<T>::top()
{
    if( front==rear )   return false;
    return front->next->data;
}

template <class T>
bool LinkQueue<T>::empty()
{
    if( front==rear )   return true;
    return false;
}

int main()
{
    LinkQueue<char> test;
    test.push('a');
    test.push('b');
    test.push('c');
    test.push('d');
    while( !test.empty() )
    {
        cout<<test.top()<<" ";
        test.pop();
    }
    return 0;
}

좋은 웹페이지 즐겨찾기