Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <string.h>
- using namespace std;
- #define DEFAULT_YEAR 1900
- typedef struct Movie
- {
- int id;
- char* title;
- int year;
- struct Movie* next;
- Movie(int inId, char* inTitle, struct Movie* inNext = NULL)
- {
- id = inId;
- title = inTitle;
- year = DEFAULT_YEAR;
- next = inNext;
- }
- } Movie;
- Movie* head = NULL;
- int& access(int id, char* title)
- {
- title = new char[strlen(title) + 1];
- //CREATE IF HEAD IS NULL
- if (head == NULL)
- {
- head = new Movie(id, title);
- return head->year;
- }
- Movie* ptr = head;
- //SEARCH
- while (ptr != NULL)
- {
- if (ptr->id == id)
- {
- if (ptr->title == NULL && title != NULL)
- {
- ptr->title = title;
- }
- return ptr->year;
- }
- ptr = ptr->next;
- }
- //ADD
- Movie *prevPtr = head;
- ptr = head;
- while (ptr != NULL)
- {
- if (ptr->year >= DEFAULT_YEAR || ptr->next == NULL)
- {
- prevPtr->next = new Movie(id, title, prevPtr->next);
- for (Movie *iter = head, *prev = head; iter; prev = iter, iter = iter->next)
- {
- if (iter->title > title)
- {
- if (iter == head)
- {
- head = new Movie(id, title, head->next);
- }
- prev->next = new Movie(id, title, prev->next);
- break;
- }
- if (iter->next == NULL)
- {
- iter->next = new Movie(id, title);
- break;
- }
- }
- return prevPtr->next->year;
- }
- prevPtr = ptr;
- ptr = ptr->next;
- }
- delete[] title;
- }
- void print()
- {
- cout << endl << "PRINT BEGIN" << endl;
- Movie* ptr = head;
- while (ptr != NULL)
- {
- cout << ptr->id << " " << ptr->year << " " << ptr->title << endl;
- ptr = ptr->next;
- }
- cout << endl << "PRINT END" << endl;
- }
- int main(void) {
- int i = access(5, (char*)"Forrest Gump");
- print();
- access(7, (char*)"Forrest Gump") = 20 + i;
- print();
- access(5, (char*)"Forrest Gump")++;
- print();
- access(5, (char*)"Forrest Gump") = access(4, (char*)"M2") + access(-15, (char*)"M3");
- print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement