Sunday, October 23, 2005

list class implementation

The code shows the usage of the list class

 

 

#include <iterator>        // for std::ostream_iterator

#include <algorithm>      // for std::copy

#include <list>

#include <ostream>

#include <iostream>

 

// my own list container class

template< class T >

class TList

{

    std::list< T > m_list;

public:

    TList() : m_list() { }

    ~TList() { }

    void push_back(const T& t)

    {

        m_list.push_back(t);

    }

    void pop_back()

    {

        m_list.pop_back();

    }

    void display() const

    {

        std::cout << "list contents:\n ";

        std::copy( m_list.begin(),

                        m_list.end(),

                        std::ostream_iterator< T >( std::cout, " ") );

        std::cout << std::endl;

    }

};

 

// a templated, complex Node type

template< class N, class D, class L>

class Node

{

    N m_n;

    D m_d;

    L m_l;

public:

    Node(N n, D d, L l) : m_n(n), m_d(d), m_l(l) { }

    Node(const Node& copy)

    {

        m_n = copy.m_n;

        m_d = copy.m_d;

        m_l = copy.m_l;

    }

    ~Node() { }

    /* friends */

    friend std::ostream&

        operator<<(std::ostream& os, const Node& a)

    {

        os << a.m_n << ", " << a.m_d << ", " << a.m_l;

        return os << std::endl;

    }

};

 

int main()

{

    TList<int> nlist;        // a list of integers

    typedef Node<int, double, long> t_Node;

    TList< t_Node > nodelist;    // a list of complex nodes

 

    for ( int sz = 0; sz < 10; ++sz)

    {

        nlist.push_back(sz);

        nodelist.push_back( t_Node(sz, sz + 0.1, sz * 10) );

    }

 

    nlist.display();

    nodelist.display();

 

    return 0;

}

 

 

 

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India

 

No comments: