Advertisement
Guest User

ав

a guest
Feb 14th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. template <typename universum> class List {
  7. private:
  8. typedef struct node {
  9. node *next;
  10. node *prev;
  11. bool empty;
  12. universum value;
  13. };
  14. node *root;
  15. node *last;
  16.  
  17. public:
  18. List() {
  19. node* current;
  20. root = new node;
  21. root->next = 0;
  22. root->prev = 0;
  23. root->empty = true;
  24. current = root;
  25. }
  26. class scanner {
  27. node *nodeptr;
  28. public:
  29. scanner(node* ptr) :nodeptr(ptr) {}
  30. scanner &operator++(int i)
  31. {
  32. nodeptr = nodeptr->next;
  33. return *this;
  34. }
  35. scanner &operator--(int i)
  36. {
  37. nodeptr = nodeptr->prev;
  38. return *this;
  39. }
  40. universum &operator*()
  41. {
  42. return nodeptr->value;
  43. }
  44. bool operator==(const scanner& ptr)
  45. {
  46. return nodeptr == ptr.nodeptr;
  47. }
  48. bool operator!=(const scanner& ptr)
  49. {
  50. return nodeptr != ptr.nodeptr;
  51. }
  52. };
  53.  
  54. scanner begin() {
  55. scanner b(root);
  56. return b;
  57. }
  58.  
  59. scanner end() {
  60. scanner e(last);
  61. return e;
  62. }
  63. void append(universum value) {
  64. node *current, *prev;
  65. current = root;
  66. prev = root;
  67.  
  68. if (root->empty) {
  69. root->value = value;
  70. root->empty = false;
  71. return;
  72. }
  73.  
  74. while (current) {
  75. prev = current;
  76. current = current->next;
  77. }
  78.  
  79. current = new node;
  80. current->next = 0;
  81. current->prev = prev;
  82. prev->next = current;
  83. current->value = value;
  84. current->empty = false;
  85. //cout << "C = " << current<< " "<< current->prev << endl;
  86. }
  87.  
  88. bool empty() {
  89. if ((root->next == 0) && (root->empty)) return true;
  90. else return false;
  91. }
  92.  
  93. int size() {
  94. int count=0;
  95. node *current;
  96. current = root;
  97. while (current) {
  98. count++;
  99. current = current->next;
  100. }
  101. return count;
  102. }
  103.  
  104. //universum *begin() {
  105. // int s = size();
  106. // int i;
  107. // //static universum m[s];
  108. // static int *ptr;
  109. // ptr = new int[s];
  110. // node *current;
  111. // current = root;
  112. // // m = &current->value;
  113. // for (i = 0; i < s;i++) {
  114. // ptr[i] = current->value;
  115. // current = current->next;
  116. // }
  117. // return ptr;
  118. //};
  119.  
  120. //universum *end() {
  121. // node *current;
  122. // current = root;
  123. // while (current->next) {
  124. // current = current->next;
  125. // }
  126. // return &current->value;
  127. //}
  128.  
  129. void tst() {
  130. cout << root << " " << root->next << endl;
  131. };
  132.  
  133.  
  134. };
  135.  
  136.  
  137.  
  138. int main()
  139. {
  140. List <int> list;
  141. List <string> str;
  142. int r;
  143.  
  144. cout << "List empty = " << list.empty() << endl;
  145.  
  146. for (int i = 0; i<10; i++) {
  147. r = rand() % 50;
  148. cout << r << " ";
  149. list.append(r);
  150. }
  151. for (List<int>::scanner it = list.begin(); it != list.end(); it++) {
  152. cout << *it << " ";
  153. }
  154. cout << endl;
  155.  
  156. //int *s, *e;
  157. //s = list.begin();
  158. //e = list.end();
  159.  
  160. //cout << endl << "List empty = " << list.empty() << endl;
  161. //cout << endl << "List size = " << list.size() << endl;
  162.  
  163. //for (int i = 0; i<list.size() + 1; i++) {
  164. // cout << (s + i) << " " << *(s + i) << endl;
  165. //}
  166. //cout << endl << endl;
  167. //cout << endl << s << " " << *s << endl;
  168. //cout << endl << e << " " << *e << endl;
  169.  
  170. system("PAUSE");
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement