Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- using namespace std;
- #define DEFAULT_YEAR 1990
- typedef struct Movie
- {
- int id;
- char* title;
- int year;
- struct Movie* next;
- Movie(int inId, char* inTitle, int inYear, struct Movie* inNext = NULL)
- {
- id = inId;
- title = inTitle;
- year = inYear;
- next = inNext;
- }
- ~Movie() {}
- } Movie;
- struct Movie* head = NULL;
- void add_Movie(int id, char *title, int year)
- {
- char *dst = new char[strlen(title) + 1];
- strcpy(dst, title);
- if (head == NULL) //create if head is NULL
- {
- head = new Movie(id, title, year);
- return;
- }
- for (Movie *iter = head, *prev = head; iter; prev = iter, iter = iter->next) //go through the list
- {
- if (iter->id == id)
- {
- if (iter->title == title) //if id appears more than 1 time, print
- {
- iter->year = year;
- cout << "We have movies with same id - "<<iter->id<<endl;
- }
- else
- {
- }
- break;
- }
- if (iter->year > year) // compare years - ascending order
- {
- if (iter == head)
- {
- head = new Movie(id, title, year, head->next);
- }
- prev->next = new Movie(id, title, year, prev->next);
- break;
- }
- if (iter->next == NULL)
- {
- iter->next = new Movie(id, title, year);
- break;
- }
- }
- delete[] dst;
- }
- int& access(int id, char* title)
- {
- //CREATE IF HEAD IS NULL
- 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);
- return prevPtr->next->year;
- }
- prevPtr = ptr;
- ptr = ptr->next;
- }
- }
- 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();
- cout << "Task 2 TEST" << endl ;
- cout << "Access empty list" << endl ;
- print();
- access(5,(char*)"Lama I");
- print();
- cout << " Creating new list, accessing the first element and:" << endl;
- cout << " Option A, title is the same" << endl ;
- add_Movie(3,(char*) "Lama", 1880);
- add_Movie(2,(char*) "Lama II", 1985);
- add_Movie(1,(char*) "Lama III", 1991);
- print();
- access(1,(char*)"Lama III");
- print();
- cout << " Option B, Different title" << endl ;
- access(1,(char*)"Changed title");
- print();
- cout << " Accessing element in the middle" << endl ;
- access(2,(char*)"Changed title X2");
- print();
- cout << "Accessing the last element" << endl ;
- access(5,(char*)"Changed title X3");
- print();
- cout << " Changing internal date" << endl ;
- print();
- add_Movie(100,(char*)"LOLOLO",2800);
- print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement