Previous Page of 3Next Page

danh sach lien ket

spinner.gif

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

template<class T>

class List

{

protected:

class node

{ public:

node(const T& data = T(), node* prev=0, node* next=0)

: _data(data), _prev(prev), _next(next)

{ if(_prev==0) _prev = this ;

if(_next==0) _next = this ;

}

T _data ;

node* _prev , * _next ;

};

node* _ ;

int _size ;

public:

class Iterator

{ friend class List ;

public:

Iterator(node* p) : _p(p) { }

T& operator*() { return _p->_data; }

void operator=(const Iterator& it) { _p = it._p; }

int operator==(const Iterator& it) { return _p==it._p; }

int operator!=(const Iterator& it) { return _p!=it._p; }

Iterator operator++(int)

{ Iterator it(_p);

_p = _p->_next ;

return it ;

}

Iterator operator++() { _p=_p->_next; return *this;}

Iterator operator--(int)

{ Iterator it(_p);

_p = _p->_prev ;

return it ;

}

Iterator operator--() { _p=_p->_prev; return *this;}

// private:

List<T>::node* _p ;

};

List() ;

~List() ;

int size() const ;

int empty() const ;

T& front() const ;

T& back() const ;

Iterator begin() { return Iterator(_->_next);}

Iterator end() { return Iterator(_->_prev);}

void push_front(const T&) ;

void push_back(const T&) ;

void printList() ;

void showList();

void clear() ;

Iterator insert(Iterator& it, const T& x)

{it._p->_prev = it._p->_prev->_next = new node(x, it._p->_prev, it._p);

Previous Page of 3Next Page

Comments & Reviews

Login or Facebook Sign in with Twitter
library_icon_grey.png Add share_icon_grey.png Share

Who's Reading

Recommended