Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <string>
- using namespace std;
- template <typename universum> class List {
- private:
- typedef struct node {
- node *next;
- node *prev;
- bool empty;
- universum value;
- };
- node *root;
- node *last;
- public:
- List() {
- node* current;
- root = new node;
- root->next = 0;
- root->prev = 0;
- root->empty = true;
- current = root;
- }
- class scanner {
- node *nodeptr;
- public:
- scanner(node* ptr) :nodeptr(ptr) {}
- scanner &operator++(int i)
- {
- nodeptr = nodeptr->next;
- return *this;
- }
- scanner &operator--(int i)
- {
- nodeptr = nodeptr->prev;
- return *this;
- }
- universum &operator*()
- {
- return nodeptr->value;
- }
- bool operator==(const scanner& ptr)
- {
- return nodeptr == ptr.nodeptr;
- }
- bool operator!=(const scanner& ptr)
- {
- return nodeptr != ptr.nodeptr;
- }
- };
- scanner begin() {
- scanner b(root);
- return b;
- }
- scanner end() {
- scanner e(last);
- return e;
- }
- void append(universum value) {
- node *current, *prev;
- current = root;
- prev = root;
- if (root->empty) {
- root->value = value;
- root->empty = false;
- return;
- }
- while (current) {
- prev = current;
- current = current->next;
- }
- current = new node;
- current->next = 0;
- current->prev = prev;
- prev->next = current;
- current->value = value;
- current->empty = false;
- //cout << "C = " << current<< " "<< current->prev << endl;
- }
- bool empty() {
- if ((root->next == 0) && (root->empty)) return true;
- else return false;
- }
- int size() {
- int count=0;
- node *current;
- current = root;
- while (current) {
- count++;
- current = current->next;
- }
- return count;
- }
- //universum *begin() {
- // int s = size();
- // int i;
- // //static universum m[s];
- // static int *ptr;
- // ptr = new int[s];
- // node *current;
- // current = root;
- // // m = ¤t->value;
- // for (i = 0; i < s;i++) {
- // ptr[i] = current->value;
- // current = current->next;
- // }
- // return ptr;
- //};
- //universum *end() {
- // node *current;
- // current = root;
- // while (current->next) {
- // current = current->next;
- // }
- // return ¤t->value;
- //}
- void tst() {
- cout << root << " " << root->next << endl;
- };
- };
- int main()
- {
- List <int> list;
- List <string> str;
- int r;
- cout << "List empty = " << list.empty() << endl;
- for (int i = 0; i<10; i++) {
- r = rand() % 50;
- cout << r << " ";
- list.append(r);
- }
- for (List<int>::scanner it = list.begin(); it != list.end(); it++) {
- cout << *it << " ";
- }
- cout << endl;
- //int *s, *e;
- //s = list.begin();
- //e = list.end();
- //cout << endl << "List empty = " << list.empty() << endl;
- //cout << endl << "List size = " << list.size() << endl;
- //for (int i = 0; i<list.size() + 1; i++) {
- // cout << (s + i) << " " << *(s + i) << endl;
- //}
- //cout << endl << endl;
- //cout << endl << s << " " << *s << endl;
- //cout << endl << e << " " << *e << endl;
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement