codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <ostream> template<typename T> class tlist { struct node { T data; node* next; }; private: node* head; node* tail; public: tlist(void): head(NULL), tail(NULL){} ~tlist() { this->clear(); } public: // добавление в хвост void add(const T& data) { node* n = new node(); n->data = data; n->next = NULL; if(head == NULL) head = tail = n; else { tail->next = n; tail = n; } } // слияние void merge(const tlist& l1, const tlist& l2) { this->clear(); const node* p1 = l1.head, *p2 = l2.head; while((p1 != NULL) || (p2 != NULL)) { if(p1 != NULL) { if((p2 == NULL) || (p1->data <= p2->data)) { this->add(p1->data); p1 = p1->next; } } if(p2 != NULL) { if((p1 == NULL) || (p2->data <= p1->data)) { this->add(p2->data); p2 = p2->next; } } } } // печать void print(std::ostream& hout) { for(const node* p = head; p != NULL; p = p->next) hout << p->data << ' '; hout << std::endl; } // чистка void clear(void) { node* tmp; while(head != NULL) { tmp = head; head = head->next; delete tmp; } tail = NULL; } }; int main(void){ tlist<float> l1, l2, l3; for(int i = 0; i < 10; i++) { l1.add((float)i*2.0f); l2.add((float)i*2.0f + 1.0f); } l1.print(std::cout); l2.print(std::cout); l3.merge(l1, l2); l3.print(std::cout); return 0; }
Private
[
?
]
Run code
Submit