Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. #define DEFAULT_YEAR 1990
  7.  
  8. typedef struct Movie
  9. {
  10. int id;
  11. char* title;
  12. int year;
  13. struct Movie* next;
  14.  
  15. Movie(int inId, char* inTitle, int inYear, struct Movie* inNext = NULL)
  16. {
  17. id = inId;
  18. title = inTitle;
  19. year = inYear;
  20. next = inNext;
  21. }
  22. ~Movie() {}
  23. } Movie;
  24.  
  25. struct Movie* head = NULL;
  26.  
  27. void add_Movie(int id, char *title, int year)
  28. {
  29. char *dst = new char[strlen(title) + 1];
  30. strcpy(dst, title);
  31. if (head == NULL) //create if head is NULL
  32. {
  33. head = new Movie(id, title, year);
  34. return;
  35. }
  36. for (Movie *iter = head, *prev = head; iter; prev = iter, iter = iter->next) //go through the list
  37. {
  38. if (iter->id == id)
  39. {
  40. if (iter->title == title) //if id appears more than 1 time, print
  41. {
  42. iter->year = year;
  43. cout << "We have movies with same id - "<<iter->id<<endl;
  44. }
  45. else
  46. {
  47.  
  48. }
  49. break;
  50. }
  51. if (iter->year > year) // compare years - ascending order
  52. {
  53. if (iter == head)
  54. {
  55. head = new Movie(id, title, year, head->next);
  56. }
  57. prev->next = new Movie(id, title, year, prev->next);
  58. break;
  59. }
  60. if (iter->next == NULL)
  61. {
  62. iter->next = new Movie(id, title, year);
  63. break;
  64. }
  65. }
  66. delete[] dst;
  67. }
  68.  
  69. int& access(int id, char* title)
  70. {
  71. //CREATE IF HEAD IS NULL
  72.  
  73. Movie* ptr = head;
  74. //SEARCH
  75. while (ptr != NULL)
  76. {
  77. if (ptr->id == id)
  78. {
  79. if (ptr->title == NULL && title != NULL)
  80. {
  81. ptr->title = title;
  82. }
  83. return ptr->year;
  84. }
  85. ptr = ptr->next;
  86. }
  87.  
  88. //ADD
  89. Movie *prevPtr = head;
  90. ptr = head;
  91. while (ptr != NULL)
  92. {
  93. if (ptr->year >= DEFAULT_YEAR|| ptr->next == NULL)
  94. {
  95. prevPtr->next = new Movie(id, title, prevPtr->next);
  96. return prevPtr->next->year;
  97. }
  98. prevPtr = ptr;
  99. ptr = ptr->next;
  100. }
  101. }
  102.  
  103. void print()
  104. {
  105. cout << endl << "PRINT BEGIN" << endl;
  106. Movie* ptr = head;
  107. while (ptr != NULL)
  108. {
  109. cout << ptr->id << " " << ptr->year << " " << ptr->title << endl;
  110. ptr = ptr->next;
  111. }
  112. cout << endl << "PRINT END" << endl;
  113. }
  114.  
  115.  
  116. int main(void) {
  117. int i = access(5, (char*)"Forrest Gump");
  118. print();
  119. access(7, (char*)"Forrest Gump") = 20 + i;
  120. print();
  121. access(5, (char*)"Forrest Gump")++;
  122. print();
  123. access(5, (char*)"Forrest Gump") = access(4, (char*)"M2") + access(-15, (char*)"M3");
  124. print();
  125. cout << "Task 2 TEST" << endl ;
  126. cout << "Access empty list" << endl ;
  127. print();
  128. access(5,(char*)"Lama I");
  129. print();
  130. cout << " Creating new list, accessing the first element and:" << endl;
  131. cout << " Option A, title is the same" << endl ;
  132. add_Movie(3,(char*) "Lama", 1880);
  133. add_Movie(2,(char*) "Lama II", 1985);
  134. add_Movie(1,(char*) "Lama III", 1991);
  135. print();
  136. access(1,(char*)"Lama III");
  137. print();
  138. cout << " Option B, Different title" << endl ;
  139. access(1,(char*)"Changed title");
  140. print();
  141. cout << " Accessing element in the middle" << endl ;
  142. access(2,(char*)"Changed title X2");
  143. print();
  144. cout << "Accessing the last element" << endl ;
  145. access(5,(char*)"Changed title X3");
  146. print();
  147. cout << " Changing internal date" << endl ;
  148. print();
  149.  
  150. add_Movie(100,(char*)"LOLOLO",2800);
  151. print();
  152.  
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement