Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. #define DEFAULT_YEAR 1900
  8.  
  9. typedef struct Movie
  10. {
  11. int id;
  12. char* title;
  13. int year;
  14. struct Movie* next;
  15.  
  16. Movie(int inId, char* inTitle, struct Movie* inNext = NULL)
  17. {
  18. id = inId;
  19. title = inTitle;
  20. year = DEFAULT_YEAR;
  21. next = inNext;
  22. }
  23. } Movie;
  24.  
  25. Movie* head = NULL;
  26.  
  27. int& access(int id, char* title)
  28. {
  29. title = new char[strlen(title) + 1];
  30. //CREATE IF HEAD IS NULL
  31. if (head == NULL)
  32. {
  33. head = new Movie(id, title);
  34. return head->year;
  35. }
  36.  
  37. Movie* ptr = head;
  38. //SEARCH
  39. while (ptr != NULL)
  40. {
  41. if (ptr->id == id)
  42. {
  43. if (ptr->title == NULL && title != NULL)
  44. {
  45. ptr->title = title;
  46. }
  47. return ptr->year;
  48. }
  49. ptr = ptr->next;
  50. }
  51.  
  52. //ADD
  53. Movie *prevPtr = head;
  54. ptr = head;
  55. while (ptr != NULL)
  56. {
  57. if (ptr->year >= DEFAULT_YEAR || ptr->next == NULL)
  58. {
  59. prevPtr->next = new Movie(id, title, prevPtr->next);
  60. for (Movie *iter = head, *prev = head; iter; prev = iter, iter = iter->next)
  61. {
  62. if (iter->title > title)
  63. {
  64. if (iter == head)
  65. {
  66. head = new Movie(id, title, head->next);
  67. }
  68. prev->next = new Movie(id, title, prev->next);
  69. break;
  70. }
  71. if (iter->next == NULL)
  72. {
  73. iter->next = new Movie(id, title);
  74. break;
  75. }
  76. }
  77. return prevPtr->next->year;
  78. }
  79. prevPtr = ptr;
  80. ptr = ptr->next;
  81. }
  82. delete[] title;
  83. }
  84.  
  85. void print()
  86. {
  87. cout << endl << "PRINT BEGIN" << endl;
  88. Movie* ptr = head;
  89. while (ptr != NULL)
  90. {
  91. cout << ptr->id << " " << ptr->year << " " << ptr->title << endl;
  92. ptr = ptr->next;
  93. }
  94. cout << endl << "PRINT END" << endl;
  95. }
  96.  
  97. int main(void) {
  98. int i = access(5, (char*)"Forrest Gump");
  99. print();
  100. access(7, (char*)"Forrest Gump") = 20 + i;
  101. print();
  102. access(5, (char*)"Forrest Gump")++;
  103. print();
  104. access(5, (char*)"Forrest Gump") = access(4, (char*)"M2") + access(-15, (char*)"M3");
  105. print();
  106.  
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement