Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MyList
//Title of this code #include <iostream> using namespace std; template<class T> class MyList { public: struct Node { Node(const T& e, Node* perv, Node* next) { this->e = new T(e); this->next = next; this->perv = perv; } ~Node() { delete e; } T* e; Node* perv; Node* next; }; public: MyList(); ~MyList(); void push_back(const T&); void remove(const T&); Node* get_first() const; Node* get_last() const; private: Node* first; Node* last; }; template<class T> MyList<T>::MyList() { first = NULL; last = NULL; } template<class T> MyList<T>::~MyList() { typename MyList<T>::Node* n = first; typename MyList<T>::Node* d = n; while (n != NULL) { d = n->next; delete n; n = d; } } template<class T> void MyList<T>::push_back(const T& e) { Node* new_node; if (first == NULL && last == NULL) { new_node = new Node(e, NULL, NULL); first = new_node; last = new_node; return; } else if (first == last) { new_node = new Node(e, first, NULL); last = new_node; first->next = last; return; } new_node = new Node(e, last, NULL); last->next = new_node; last = new_node; } template<class T> void MyList<T>::remove(const T& e) { typename MyList<T>::Node* n = first; typename MyList<T>::Node *tmp, *tmp2; while (n != NULL) { tmp2 = n->next; if (*(n->e) == e) { if (n == first && n == last) { delete first; first = NULL; last = NULL; } else if (n == first) { tmp = first->next; delete first; first = tmp; first->perv = NULL; } else if (n == last) { tmp = last->perv; delete last; last = tmp; last->next = NULL; } else { n->perv->next = n->next; n->next->perv = n->perv; delete n; } } n = tmp2; } } template<class T> typename MyList<T>::Node* MyList<T>::get_first() const{ return first; } template<class T> typename MyList<T>::Node* MyList<T>::get_last() const { return last; } template<class T> ostream& operator<< (ostream& out, const MyList<T>& l) { typename MyList<T>::Node* n = l.get_first(); while (n != NULL) { out << *(n->e) << " "; n = n->next; } return out; } int main() { MyList<int> l; l.push_back(0); for (int i = 0; i < 10; ++i) l.push_back(5); l.push_back(3); l.remove(5); l.push_back(6); cout << l << endl; }
run
|
edit
|
history
|
help
0
mergesort tree
Smalllest subarray wiith sum greater than x
reverse array
PriorQ2
pointer to template function
stackLinkedlist
Height of a binary tree
Segmented Sieve
<string> Indirect include of <errno.h> with gcc
For Hello World