Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cstring>
  6.  
  7. template <typename Type> struct ListElement {
  8. ListElement <Type>* next;
  9. Type value;
  10.  
  11. ListElement() {
  12. next = NULL;
  13. }
  14.  
  15. ListElement(Type _value) {
  16. next = NULL;
  17. value = _value;
  18. }
  19. };
  20.  
  21. template <typename Type> struct List {
  22. ListElement <Type>* head;
  23. ListElement <Type>* tail;
  24. unsigned int length;
  25.  
  26. List() {
  27. length = 0;
  28. // limiter
  29. head = tail = new ListElement <Type>();
  30. }
  31.  
  32. ~List() {
  33. ListElement <Type>* curr = head;
  34.  
  35. while(length + 1 != 0) {
  36. ListElement <Type>* deleted = curr;
  37.  
  38. curr = curr->next;
  39.  
  40. length--;
  41. }
  42. }
  43.  
  44. void push(Type value) {
  45. tail->next = new ListElement <Type>(value);
  46. tail = tail->next;
  47. length++;
  48. }
  49.  
  50. Type pop() {
  51. if(length == 0) {
  52. throw "List is empty";
  53. }
  54.  
  55. Type returned = tail->value;
  56.  
  57. ListElement <Type>* new_tail = head;
  58.  
  59. while (new_tail->next != tail) {
  60. new_tail = new_tail->next;
  61. }
  62.  
  63. tail = new_tail;
  64. tail->next = NULL;
  65.  
  66. length--;
  67.  
  68. delete tail->next;
  69. return returned;
  70. }
  71.  
  72. void unshift(Type value) {
  73. ListElement <Type>* new_el = new ListElement <Type>(value);
  74. new_el->next = head->next;
  75. head->next = new_el;
  76. length++;
  77. }
  78.  
  79. Type shift() {
  80. if(length == 0) {
  81. throw "List is empty";
  82. }
  83.  
  84. ListElement <Type>* deleted = head->next;
  85.  
  86. Type returned = deleted->value;
  87.  
  88. head->next = deleted->next;
  89.  
  90. length--;
  91.  
  92. delete deleted;
  93. return returned;
  94. }
  95.  
  96. void insert(unsigned int index, Type value) {
  97. if(index > length) {
  98. throw "Can't insert to such index";
  99. }
  100.  
  101. ListElement <Type>* before = head;
  102.  
  103. for(int i = 0; i < index; i++) {
  104. before = before->next;
  105. }
  106.  
  107. ListElement <Type>* inserted = new ListElement <Type>(value);
  108.  
  109. inserted->next = before->next;
  110. before->next = inserted;
  111.  
  112. if(index == length) {
  113. tail = inserted;
  114. }
  115.  
  116. length++;
  117. }
  118.  
  119. Type remove(unsigned int index) {
  120. if(index >= length) {
  121. throw "Can't remove such index";
  122. }
  123.  
  124. ListElement <Type>* before = head;
  125.  
  126. for(int i = 0; i < index; i++) {
  127. before = before->next;
  128. }
  129.  
  130. ListElement <Type>* deleted = before->next;
  131.  
  132. before->next = deleted->next;
  133.  
  134. Type returned = deleted->value;
  135.  
  136. if(index == length - 1) {
  137. tail = before;
  138. }
  139.  
  140. length--;
  141.  
  142. delete deleted;
  143. return returned;
  144. }
  145.  
  146. int find(Type value) {
  147. int index = 0;
  148. bool found = false;
  149. ListElement <Type>* current = head->next;
  150.  
  151. while(current) {
  152. if(current->value == value) {
  153. found = true;
  154. break;
  155. }
  156.  
  157. index++;
  158. current = current->next;
  159. }
  160.  
  161. if(!found)
  162. index = -1;
  163.  
  164. return index;
  165. }
  166.  
  167. void forEach(std::function <void (Type, unsigned int)> func) {
  168. int index = 0;
  169. ListElement <Type>* current = head->next;
  170.  
  171. while(current) {
  172. func(current->value, index);
  173.  
  174. index++;
  175. current = current->next;
  176. }
  177. }
  178.  
  179. friend std::ostream& operator<< (std::ostream& out, List<Type> list) {
  180. out << "[ ";
  181.  
  182. list.forEach([&out, list](Type value, unsigned int i) {
  183. out << value << ((i < list.length - 1) ? ", " : " ");
  184. });
  185.  
  186. out << "]";
  187.  
  188. return out;
  189. }
  190. };
  191.  
  192. int main() {
  193. srand(time(NULL));
  194.  
  195. List <char> list;
  196.  
  197. for(int i = 0; i < 10; i++)
  198. list.push(rand() % ('Z' - 'A') + 'A');
  199.  
  200.  
  201. std::cout << "Available commands:\n"
  202. << " print\n"
  203. << " push [X]\n"
  204. << " pop\n"
  205. << " unshift [X]\n"
  206. << " shift\n"
  207. << " insert [i] [X]\n"
  208. << " remove [i]\n"
  209. << " find X\n"
  210. << " custom\n"
  211. << " exit\n"
  212.  
  213. << "\nInitial list: " << list
  214.  
  215. << "\n\n> ";
  216.  
  217. while(1) {
  218. char command[64];
  219.  
  220. std::cin >> command;
  221.  
  222. try {
  223. if(strcmp(command, "print") == 0) {
  224. std::cout << list;
  225. }
  226. else if(strcmp(command, "push") == 0) {
  227. char p;
  228. std::cin >> p;
  229. list.push(p);
  230. } else if(strcmp(command, "pop") == 0) {
  231. std::cout << list.pop();
  232. } else if(strcmp(command, "unshift") == 0) {
  233. char p;
  234. std::cin >> p;
  235. list.unshift(p);
  236. } else if(strcmp(command, "shift") == 0) {
  237. std::cout << list.shift();
  238. } else if(strcmp(command, "insert") == 0) {
  239. char p;
  240. int i;
  241. std::cin >> i >> p;
  242. list.insert(i, p);
  243. } else if(strcmp(command, "remove") == 0) {
  244. int i;
  245. std::cin >> i;
  246. std::cout << list.remove(i);
  247. } else if(strcmp(command, "find") == 0) {
  248. char p;
  249. std::cin >> p;
  250. std::cout << list.find(p);
  251. } else if(strcmp(command, "custom") == 0) {
  252. std::cout << "\nGreater than 'D':\n";
  253. list.forEach([](char c, int i) {
  254. if(c > 'D')
  255. std::cout << i << ": " << c << std::endl;
  256. });
  257. } else if(strcmp(command, "exit") == 0) {
  258. return 0;
  259. } else {
  260. std::cout << "Unknown command";
  261. }
  262. } catch(const char* err) {
  263. std::cout << err;
  264. }
  265.  
  266. std::cout << "\n> ";
  267. }
  268.  
  269. return 0;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement