Custom list

// ideas Analysis: Given a linked list class members (node ​​iterator)

 

 

 
 #include <iostream>
 
using namespace std;
 
//node
template<typename U>
class Node
{
  public:
    Node(){pNext == NULL;}
    Node(U data):m_data(data){pNext = NULL;}
    U m_data;
    Node<U>* pNext;
};
// list is the underlying list
template <typename U>
class Mylist
{
public:
 
    // Mylist<int>::Myiterator iter;
    // iterator is a pointer to the underlying ++ - *
    struct Myiterator
    {
 
 
        Myiterator(Node<U> *node = NULL)
        {
            pNode = node;
        }
        Myiterator &operator++(int)
        {
            pNode= pNode->pNext;
            return *this;
 
        }
        bool operator !=(const Myiterator &other)
        {
            return pNode!=other.pNode;
 
        }
        U operator *()
        {
            return pNode->m_data;
 
        }
         Node<U> *pNode;
    };
    // list <int> list; list.begin (); returns an iterator
    // member function
    Myiterator start ()
    {
        Myiterator path (m_pfrist);
        return journey;
    }
    Myiterator end()
    {
       Myiterator path (NULL);
        return journey;
    }
     Mylist (): m_il the (0) {m_pfrist = NULL;}
    void insert(U data);
    void display();
 
private:
    int m_il that;
    // generates an object need to add <U>
    Node<U> *m_pfrist;
};
//insert
template<typename U>
void Mylist<U>::insert(U data)
{
    Node<U>* node= new Node<U>(data);
    if(NULL == m_pfrist)
    {
        m_pfrist=node;
 
    }else{
 
        node->pNext = m_pfrist;
        m_pfrist = node;
 
    }
m_il by ++;
}
//print
template<typename U>
void Mylist<U>::display()
{
    Node<U> *node= m_pfrist;
    while(node != NULL)
    {
        cout<<node->m_data<<endl;
        node = node->pNext;
 
    }
 
}
 
struct STU
{
    STU(const string name="STU",float score=0.0): m_strName(name),m_fscore(score){}
    string m_strName;
    float m_fscore;
 
};
ostream& operator<<(ostream &os,const STU stu)
{
    os<<stu.m_strName<<" "<<stu.m_fscore<<endl;
    return os;
 
}
int main ()
{
    Mylist<STU> stull;
   // ll.insert(88);
    stull.insert(STU("JACK",88.99));
    stull.insert(STU("ROSE",88.99));
    stull.insert(STU("Jim",33.99));
    stull.display();
    Mylist<STU>::Myiterator iter;
    guest stull.begin = ();
    for(;iter!=stull.end();iter++)
    {
        cout<<*iter<<endl;
 
    }
}
 
 

Guess you like

Origin www.cnblogs.com/countryboy666/p/10943318.html