Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. #include<stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. template < typename Key, typename Info >
  8.  
  9. class Sequence {
  10.  
  11. private:
  12.  
  13. typedef struct SequenceList {
  14. Key id;
  15. Info information;
  16. SequenceList * next;
  17. };
  18.  
  19. SequenceList * head;
  20. SequenceList * tail;
  21.  
  22. int numberOfSequences;
  23.  
  24. void removeAll() {
  25.  
  26. if (head == NULL)
  27. return;
  28.  
  29. SequenceList * sequenceToDelete = head;
  30.  
  31. while (sequenceToDelete != NULL) {
  32. head = head -> next;
  33. delete sequenceToDelete;
  34. sequenceToDelete = head;
  35. }
  36.  
  37. tail = NULL;
  38. numberOfSequences = 0;
  39.  
  40. };
  41.  
  42. public:
  43.  
  44. Sequence() {
  45. head = NULL;
  46. tail = NULL;
  47. numberOfSequences = 0;
  48. }
  49.  
  50. ~Sequence() {
  51. removeAll();
  52. }
  53.  
  54. //add sequence in the end of the list
  55. void push_back(const Key & newid, const Info & newInfo) {
  56.  
  57. SequenceList * newSequence = new SequenceList();
  58. newSequence -> id = newid;
  59. newSequence -> information = newInfo;
  60. newSequence -> next = NULL;
  61.  
  62. if (head == NULL) {
  63. head = newSequence;
  64. tail = head;
  65. } else {
  66. tail -> next = newSequence;
  67. tail = tail -> next;
  68. }
  69.  
  70. numberOfSequences++;
  71. }
  72.  
  73. //add sequence in front of the list
  74. void push_front(const Key & newid, const Info & newInfo) {
  75.  
  76. SequenceList * newSequence = new SequenceList(newid, newInfo, head);
  77.  
  78. if (head == NULL) {
  79. head = newSequence;
  80. tail = head;
  81. } else
  82. head = newSequence;
  83.  
  84. numberOfSequences++;
  85. }
  86.  
  87. bool remove(const Key & where) {
  88.  
  89. if (head == NULL)
  90. return false;
  91.  
  92. SequenceList * sequenceToDelete = head;
  93. SequenceList * previous = NULL; // looking for position of sequence to be deleted
  94.  
  95. while (sequenceToDelete != NULL && sequenceToDelete -> id != where) {
  96. previous = sequenceToDelete;
  97. sequenceToDelete = sequenceToDelete -> next;
  98. } // if sequence is found
  99.  
  100. if (sequenceToDelete) { // if we want to delete the first sequence
  101. if (!previous) {
  102.  
  103. head = head -> next;
  104. delete sequenceToDelete;
  105. numberOfSequences--;
  106.  
  107. if (size() == 0)
  108. tail = NULL;
  109.  
  110. } else {
  111. previous -> next = sequenceToDelete -> next;
  112. delete sequenceToDelete;
  113.  
  114. numberOfSequences--; // if we have deleted tail sequence
  115. if (!previous -> next)
  116. tail = previous;
  117. }
  118.  
  119. return true;
  120.  
  121. } // sequence not found
  122.  
  123. else
  124. return false;
  125.  
  126. }
  127.  
  128. bool isKeyExisting(int place) const {
  129.  
  130. if (head != NULL && place >= 0) {
  131.  
  132. SequenceList * temp = head;
  133. int i = 0;
  134.  
  135. while (i < place && temp != NULL) {
  136. temp = temp -> next;
  137. i++;
  138. }
  139.  
  140. if (temp != NULL)
  141. return true;
  142. }
  143.  
  144. return false;
  145. }
  146.  
  147. Key & getKeyByIndex(int place) const {
  148.  
  149. SequenceList * temp = head;
  150.  
  151. int i = 0;
  152.  
  153. while (i < place && temp != NULL) {
  154. temp = temp -> next;
  155. i++;
  156. }
  157.  
  158. return temp -> id;
  159. }
  160.  
  161. bool isInfoExisting(int place) const {
  162.  
  163. if (head != NULL && place >= 0) {
  164. SequenceList * temp = head;
  165.  
  166. int i = 0;
  167.  
  168. while (i < place && temp != NULL) {
  169. temp = temp -> next;
  170. i++;
  171. }
  172.  
  173. if (temp != NULL)
  174. return true;
  175.  
  176. }
  177.  
  178. return false;
  179. }
  180.  
  181. Info & getInfoByIndex(int place) const {
  182.  
  183. SequenceList * temp = head;
  184.  
  185. int i = 0;
  186.  
  187. while (i < place && temp != NULL) {
  188. temp = temp -> next;
  189. i++;
  190. }
  191.  
  192. return temp -> information;
  193. }
  194.  
  195. void reset() {
  196. removeAll();
  197. }
  198.  
  199.  
  200. void print() const {
  201.  
  202. SequenceList * current = head;
  203.  
  204. cout << "Printing sequence!" << endl;
  205.  
  206. while (current != NULL) {
  207.  
  208. cout << "id: " << current -> id;
  209. cout << " Information: " << current -> information << endl;
  210. current = current -> next;
  211.  
  212. }
  213. cout << "______END______" << endl;
  214.  
  215. }
  216.  
  217.  
  218. int size() const {
  219. return numberOfSequences;
  220. }
  221.  
  222. bool isEmpty() const {
  223. return numberOfSequences == 0;
  224. }
  225.  
  226. void sortList() {
  227.  
  228. SequenceList * curr;
  229. SequenceList * prev;
  230.  
  231. for (bool didSwap = true; didSwap;) {
  232.  
  233. didSwap = false;
  234.  
  235. prev = head;
  236.  
  237. for (curr = head; curr -> next != NULL; curr = curr -> next) {
  238.  
  239. if (curr -> id > curr -> next -> id) {
  240.  
  241. if (head == curr) {
  242.  
  243. head = curr -> next;
  244. curr -> next = head -> next;
  245. head -> next = curr;
  246. prev = head;
  247.  
  248. } else {
  249. prev -> next = curr -> next;
  250. curr -> next = prev -> next -> next;
  251. prev -> next -> next = curr;
  252. }
  253.  
  254. didSwap = true;
  255.  
  256. } else if (head != curr) {
  257. prev = prev -> next;
  258. }
  259.  
  260. //cout << curr->next->id << endl; // <- this may cause crash if curr->next now points to NULL; (i,e last element)
  261. }
  262. }
  263. }
  264.  
  265. };
  266.  
  267. template < typename Key, typename Info >
  268. void split(Sequence < Key, Info > & result,
  269. int start1, int len1, int start2, int len2,
  270. Sequence < Key, Info > & r1, Sequence < Key, Info > & r2, int repeat) {
  271.  
  272. int start;
  273.  
  274. if (!r1.isEmpty()) {
  275. r1.reset();
  276. }
  277.  
  278. if (!r2.isEmpty()) {
  279. r2.reset();
  280. }
  281.  
  282. for (int i = 0; i < repeat; ++i) {
  283.  
  284. cout << "----------------------------------------" << endl;
  285. cout << "Current loop is " << (i + 1) << " repeat is " << repeat << endl;
  286.  
  287. start = start1;
  288.  
  289. if (start >= 0 && len1 >= 0) {
  290.  
  291. int arrayLength = result.size() - 1;
  292.  
  293. cout << "start " << start << " length " << len1 << " sequences list size " << arrayLength << endl;
  294.  
  295. int length = 0;
  296.  
  297. while (length < len1 && start <= arrayLength) {
  298.  
  299. cout << "length " << length << endl;
  300.  
  301. if (result.isKeyExisting(start) && result.isInfoExisting(start)) {
  302.  
  303. cout << "we are pushing back " << result.isKeyExisting(start) << " info " << result.getInfoByIndex(start) << " whos start is " << start << endl;
  304.  
  305. r1.push_back(result.getKeyByIndex(start), result.getInfoByIndex(start));
  306. result.remove(result.getKeyByIndex(start));
  307.  
  308. } else {
  309. start++;
  310. }
  311. length++;
  312. }
  313. }
  314.  
  315. cout << "------------------ Starting the other part ------------------" << endl;
  316.  
  317. start = start2;
  318.  
  319. if (start >= 0 && len2 >= 0) {
  320.  
  321. int arrayLength = result.size() - 1;
  322.  
  323. cout << "start " << start << " length " << len2 << " sequences list size " << arrayLength <<
  324. endl;
  325.  
  326. int length = 0;
  327.  
  328. while (length < len2 && start <= arrayLength) {
  329.  
  330. cout << "length " << length << endl;
  331.  
  332. if (result.isKeyExisting(start) && result.isInfoExisting(start)) {
  333.  
  334. cout << "we are pushing back " << result.isKeyExisting(start) << " info " << result.getInfoByIndex(start) <<
  335. " whos start is " << start << endl;
  336.  
  337. r2.push_back(result.getKeyByIndex(start),
  338. result.getInfoByIndex(start));
  339. result.remove(result.getKeyByIndex(start));
  340.  
  341. } else {
  342. start++;
  343. }
  344. length++;
  345. }
  346. }
  347. }
  348.  
  349. r1.sortList();
  350. r2.sortList();
  351.  
  352. }
  353.  
  354. int
  355. main() {
  356.  
  357. Sequence < int, int > sequence;
  358.  
  359. sequence.push_back(1, 1);
  360. sequence.push_back(2, 2);
  361. sequence.push_back(3, 3);
  362. sequence.push_back(10, 10); // where we start from
  363. sequence.push_back(20, 20); // this is over for the first sequence splitting
  364. sequence.push_back(4, 4);
  365. sequence.push_back(5, 5);
  366. sequence.push_back(6, 6);
  367. sequence.push_back(7, 7);
  368. sequence.push_back(30, 30); // index 9
  369. sequence.push_back(40, 40); //index 10
  370. sequence.push_back(8, 8);
  371. sequence.push_back(9, 9);
  372. sequence.push_back(10, 10);
  373. sequence.push_back(11, 11);
  374. sequence.push_back(50, 50);
  375.  
  376. puts("--------------------sequence-----------------------");
  377. sequence.print();
  378.  
  379. Sequence < int, int > r1;
  380. Sequence < int, int > r2;
  381.  
  382. split(sequence, 3, 2, 3, 4, r1, r2, 2);
  383.  
  384. //start 1 = 3 length1 = 2
  385. //start 2 = 3 length = 4
  386. //repetition = 2
  387.  
  388. puts("--------------------r1-----------------------");
  389. r1.print();
  390.  
  391. puts("--------------------r2----------------------");
  392. r2.print();
  393.  
  394. return 0;
  395.  
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement