[Linked_List]Book

//Created: May 26th, 2015
//Last Modified: July 2nd, 2015 Tue
//Linear List "BookList", implemented in Linked List (1-way)
//Linear List "BookArray", implemented in Dynamic Array
//Note: On June 29th, 2015, encountered a problem that I feel hard to resolve; The problem is that, when two adjacent items in a linked list are to be swapped, the enumerator pointer moves, which caused a problem when a list like of 9 8 100 6 5 4 is to be sorted.
//Note: On June 30th, 2015, the problem listed on June 29th, 2015 was resolved. The problem was how to locate the enumerator pointer. As swap moves this enumerator pointer, I added another pointer to be the preceder of the enumerator pointer. As this preceder won't involve the swap, it is stable all the time. Using this pointer to get next makes this process stable independent of swap
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

class Book
{
public:
    Book();
    Book(string name_par, int index_par);
    Book(Book& book_par);
    void copy(Book& book_par);
    void modify(string name_par, int index_par);
    void input();
    void output();
    void modify_next(Book* new_target_par);
    string get_name();
    int get_index();
    Book* get_next();
private:
    string name;
    int index;
    Book *next;
};

class BookList
{
public:
    BookList();
    //void init();
    //void destroy();
    //void clear();
    void list_push(Book& book_par); //add one item to the end
    void list_insert(int n_par, Book& book_par); //insert a book before n_th item
    void list_insert(Book& book_position_par, Book& book_par); //insert a book before book_position
    void list_insert_sorted(Book& book_par);
    void list_pop();    //delete the last item
    void list_delete(int n_par); //delete n_th item
    void list_delete(Book& book_par); //delete book "book_par"
    void list_union(BookList& book_list_par); //add another list's items that are "not" in this list to this list
    void list_union_sorted(BookList& book_list_par); //add another list's items sorted
    void list_combine(BookList& book_list_par); //add another list to this list, regardless of any duplication of items
    Book* item_read(int n_par);
    Book* list_preceder(Book& book_par);
    Book* get_head();
    Book* get_tail();
    bool item_contain(Book& book_par);
    void sort_swap(Book& book1_par, Book& book2_par);   //swap completely
    void sort_index();  //sort by index
    //void input();
    void output();  //display all books
    bool empty();
    int get_length();
private:
    int size;
    Book *head;
    Book *tail;
};

class BookArray
{
public:
    BookArray();
    BookArray(int size_par);
    BookArray(BookArray& book_list_array_par);
    BookArray(BookList& book_list_par);
    ~BookArray();
    void output();
    bool empty();
    bool full();
    int get_size();
    int get_length();
    int get_space();
    void resize();
    void resize(int size_add_par);
    void copy_to(Book *& base_par);
    void array_push(Book& book_par);
    void array_pop();
    void array_insert(Book& book_position_par, Book& book_par);
    void array_insert(int n_par, Book& book_par);
    void array_delete(int n_par);
    void array_delete(Book& book_par);
    Book* get_base();


    int insert_locate_sorted(Book& book_par);
    void array_insert_sorted(Book& book_par);
    int item_locate(Book& book_par);
    void sort_swap(Book& book1_par, Book& book2_par);
    void sort_swap(int n1_par, int n2_par);
    void sort_index();
    void array_union(BookArray& book_array_par);
    void array_union_sorted(BookArray& book_array_par);
    void array_intersect(BookArray& book_array_par);
private:
    int size;
    int length;
    Book *base;
};


void book_swap(Book& book1_par, Book& book2_par);
bool operator ==(Book& book1_par, Book& book2_par);
bool operator !=(Book& book1_par, Book& book2_par);

int main()
{

    Book *book = new Book[6];
    book[0].modify("With Fire & Sw", 1);
    book[1].modify("The Deluge", 2);
    book[2].modify("Brave Heart", 7);
    book[3].modify("With Fire & Sw", 4);
    book[4].modify("The Deluge", 100);
    book[5].modify("Brave Heart", 6);   

    //Book *book1 = new Book("With Fire & Sw", 1);
    //book1->output();
    //Book *book2 = new Book("The Deluge", 2);
    //book2->output();
    //Book *book3 = new Book("Brave Heart", 3);
    //book3->output();
    //Book *book4 = new Book("With Fire & Sw", 4);
    //book1->output();
    //Book *book5 = new Book("The Deluge", 5);
    //book2->output();
    //Book *book6 = new Book("Brave Heart", 6);
    //book3->output();

    //BookList book_list;
    /*
    book_list.output();


    book_list.list_push(*book1);
    book_list.list_push(*book2);
    book_list.list_insert(1, *book3);   
    book_list.output();


    BookList *book_list_2015;
    book_list_2015 = new BookList();
    book1->modify("Harry Potter", 1770);
    book2->modify("King of Ring", 0001);
    book3->modify("Brave Heart", 0007);
    book_list_2015->list_push(*book1);
    book_list_2015->list_push(*book2);
    book_list_2015->list_insert(0, *book3);
    cout << "Book List 2015\n"; 
    book_list_2015->output();
    book_list.list_union(*book_list_2015);
    book_list.output();
    */

    /*
    book_list.list_push(*book1);
    book_list.list_push(*book2);
    book_list.list_push(*book3);
    book_list.list_push(*book4);
    book_list.list_push(*book5);
    book_list.list_push(*book6);
    */



    /*

    BookList *book_list_2 = new BookList();

    book_list_2->list_push(*book1);
    book_list_2->list_push(*book2);
    book_list_2->list_push(*book3);
    book_list_2->list_push(*book4);
    book_list_2->list_push(*book5);
    book_list_2->list_push(*book6);

    book_list.list_union_sorted(*book_list_2);
    book_list.output();
    */

    /*
    BookArray book_array(book_list);
    book_array.output();
    book_array.array_insert(3, *book6);
    book_array.output();
    book_array.array_insert(*book1, *book1);
    book_array.output();
    book_array.array_push(*book1);
    book_array.array_push(*book3);
    book_array.output();
    book_array.array_pop();
    book_array.output();
    */

    BookArray* book_array = new BookArray(10);
    for(int i=0; i<6; i++)
    {
        book_array->array_push(book[i]);
    }
    book_array->output();
    book[0].modify("Harry Potter", 9);
    book[1].modify("King of Ring", 8);
    book[2].modify("Brave Heart", 7);
    book[3].modify("Harry Potter", 44);
    book[4].modify("King of Ring", 4);
    book[5].modify("Brave Heart", 3);
    BookArray* book_array_2 = new BookArray(2);
    for(int i=0; i<6; i++)
    {
        book_array_2->array_push(book[i]);
    }
    book_array_2->output();

    book_array_2->array_intersect(*book_array);
    book_array_2->output();
    book_array->output();
    book_array->sort_index();
    book_array->output();
    book_array->array_union_sorted(*book_array_2);
    book_array->output();

    delete [] book;
    book = NULL;
    delete book_array;
    book_array = NULL;
    delete book_array_2;
    book_array_2 = NULL;
    return 0;
}
//------------------------------------------Functions------------------------------------
void book_swap(Book& book1_par, Book& book2_par) //swap content
{
    cout << "Book Swapping....\n";
    Book book_tmp;
    book_tmp.copy(book1_par);
    book1_par.copy(book2_par);
    book2_par.copy(book_tmp);
    return;
}

bool operator ==(Book& book1_par, Book& book2_par)
{
    if(book1_par.get_name() == book2_par.get_name() && book1_par.get_index() == book2_par.get_index())
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool operator !=(Book& book1_par, Book& book2_par)
{
    if(book1_par == book2_par) 
    {
        return false;
    }
    else
    {
        return true;
    }   

}
//------------------------------------------Functions------------------------------------

//------------------------------------------Book-----------------------------------------
Book::Book()
{
    name = "NONE";
    index = 0;
    next = NULL;
}

Book::Book(string name_par, int index_par)
{
    name = name_par;
    index = index_par;
    next = NULL;
}

Book::Book(Book& book_par)
{
    name = book_par.get_name();
    index = book_par.get_index();
    next = NULL;
}

void Book::copy(Book& book_par)
{
    name = book_par.get_name();
    index = book_par.get_index();
    next = book_par.get_next();
    return;
}

void Book::modify(string name_par, int index_par)
{
    name = name_par;
    index = index_par;
    return;
}

void Book::input()
{
    cout << "Please enter the name and index of book\n";
    cout << "Name: ";
    getline(cin, name);
    cout << "Index: ";
    cin >> index;
    return;
}

void Book::output()
{
    cout << "This is book \"" << name << "\"\t (index: " << index << " )\n";
    return;
}

string Book::get_name()
{
    return name;
}

int Book::get_index()
{
    return index;
}

Book* Book::get_next()
{
    return next;
}

void Book::modify_next(Book* new_target_par)
{
    next = new_target_par;
    return;
}
//------------------------------------------Book-----------------------------------------


//----------------------------------------BookList---------------------------------------

/*
void BookList::init()
{
    vector<Book> head;
    size = head.size(); 
    return;
}
*/

/*
void BookList::clear()
{
    for(int i=0; i<size; i++)
    {
        head[i].name = "NONE";
        head[i].index = 0;
    }
    return;
}
*/

BookList::BookList()
{
    size = 0;
    head = NULL;
    tail = NULL;
}

void BookList::list_push(Book& book_par)
{
    //create a node for this new item
    Book *book_add = new Book(book_par.get_name(), book_par.get_index());
    book_add->modify_next(NULL);
    if(tail != NULL)
    {
        tail->modify_next(book_add);
    }
    tail = book_add;
    if(size == 0)
    {
        head = book_add;
    }
    size ++;    
    return;
}

void BookList::list_insert(int n_par, Book& book_par)
{
    //create a node for this new item
    if(n_par < 0 || n_par > size)
    {
        cout << "Insertion out of range\n";
        exit(1);
    }
    if(n_par == size || size == 0) //consider two occasions: 1. add to the tail 2. empty list
    {
        list_push(book_par);
    }
    else
    {
        Book *book_add = new Book(book_par.get_name(), book_par.get_index());
        if(n_par == 0)
        {
            book_add->modify_next(head);
            head = book_add;
        }
        else
        {
            Book *front, *back;
            int i;
            front = head;
            for(i=0; i < n_par-1; i++)
            {
                front = front->get_next();
            }
            back = front->get_next();
            front->modify_next(book_add);
            book_add->modify_next(back);
        }

        size ++;
    }

    return;
}

void BookList::list_insert(Book& book_position_par, Book& book_par)
{
    cout << "Trying to insert " <<  book_par.get_name() << " " << book_par.get_index() << " in front of " << book_position_par.get_name() << " " << book_position_par.get_index() << endl;
    if(item_contain(book_position_par) == false)
    {
        cout << "Insert Book_Position not in List\n";
    }
    else
    {
        cout << "Inserting....\n";
        Book *book_add = new Book(book_par.get_name(), book_par.get_index());
        if(book_position_par.get_name() == head->get_name() && book_position_par.get_index() == head->get_index())
        {
            book_add->modify_next(head);
            head = book_add;
        }
        else
        {
            Book *p;
            p = head;
            while( ( (p->get_next())->get_name() == book_position_par.get_name() && (p->get_next())->get_index() == book_position_par.get_index() ) == false && p->get_next() != tail ) 
            {
                p = p->get_next();
            }   
            book_add->modify_next(p->get_next());
            p->modify_next(book_add);
        }
        size ++;
    }
    return;
}

void BookList::list_insert_sorted(Book& book_par)
{
    cout << "List Insert Sorted....\n";
    Book *book_add = new Book(book_par);
    if(book_par.get_index() <= head->get_index())
    {
        book_add->modify_next(head);
        head = book_add;
    }
    else
    {
        Book *p;
        p = head;
        while(p!=tail)
        {
            if(book_par.get_index() <= (p->get_next())->get_index())
            {
                book_add->modify_next(p->get_next());
                p->modify_next(book_add);
                break;
            }
            p = p->get_next();
        }
        if(tail->get_index() <= book_par.get_index())
        {
            tail->modify_next(book_add);
            tail = book_add;
        }   
    }

    size ++;
    return;
}

void BookList::list_delete(int n_par)
{
    if(size == 0)
    {
        cout << "Empty List - Nothing to delete\n";
    }
    else if(n_par < 0 || n_par >= size)
    {
        cout << "Delete: out of range\n";
    }
    else
    {
        cout << "Deleting....\n";
        if(n_par == size-1)
        {
            cout << "POP....\n";
            list_pop();
        }
        else if (n_par == 0)
        {
            Book *i;
            i = head;
            head = head->get_next();
            delete i;
            size --;
        }
        else
        {
            Book *p, *n;
            int i;
            p = head;
            for(i=0; i<n_par-1; i++)
            {
                p = p->get_next();
            }
            cout << " i = " << i << endl;
            n = p->get_next();
            p->modify_next(n->get_next());
            delete n;
            size --;
        }

    }
}

void BookList::list_delete(Book& book_par)
{
    if(item_contain(book_par) == false)
    {
        cout << "Book to delete not in List\n"; 
    }
    else
    {
        if(*head == book_par)
        {
            Book *p;
            p = head;
            head = head->get_next();
            delete p;
        }
        else if(*tail == book_par)
        {
            list_pop();
        }
        else
        {
            Book *p, *n;
            p = head;
            while( ( *(p->get_next()) == book_par ) == false && p->get_next() != tail)
            {
                p = p->get_next();
            }
            n = p->get_next();
            p->modify_next(n->get_next());
            delete n;
        }

        size --;
    }

    return;
}

void BookList::list_pop()
{
    if(size == 0)
    {
        cout << "Empty List - Nothing to POP\n";
    }
    else if(size == 1)
    {
        delete head;
        size --;
    }
    else 
    {
        Book *i;
        i = head;
        while(i->get_next() != tail)
        {
            i = i->get_next();
        }
        i->modify_next(NULL);
        delete tail;
        tail = i;
        size --;
    }

    return;
}

Book* BookList::item_read(int n_par)
{
    if(n_par < 0 || n_par >= size)
    {
        cout << "item_read: out of range\n";
        exit(1);
    }

    Book *p;
    p = head;
    int i = 0;
    for(i=0; i<n_par; i++)
    {
        p = p->get_next();
    }
    return p;
}

Book* BookList::list_preceder(Book& book_par)
{
    Book *preceder, *p;
    p = NULL;
    preceder = head;
    if(item_contain(book_par) == false)
    {
        cout << "No this Book in List\n";
        exit(1);
    }
    if(book_par == *head)
    {
        cout << "This Book is Head -- No Preceder\n";
        return NULL;
    }
    while(preceder != tail)
    {
        if(*(preceder->get_next()) == book_par)
        {
            p = preceder;
            break;
        }
        preceder = preceder->get_next();
    }

    if(p == NULL)
    {
        exit(1);
    }
    else
    {
        return p;
    }

}

Book* BookList::get_head()
{
    return head;
}

Book* BookList::get_tail()
{
    return tail;
}

bool BookList::item_contain(Book& book_par)
{
    Book *i;
    bool contain(false);
    i = head;
    while(i->get_next()!=NULL)
    {
        if(i->get_name() == book_par.get_name() && i->get_index() == book_par.get_index())
        {
            contain = true;
            break;
        }
        i = i->get_next();
    }
    if(i->get_name() == book_par.get_name() && i->get_index() == book_par.get_index())
    {
        contain = true;
    }
    return contain;
}


void BookList::sort_swap(Book& book1_par, Book& book2_par)
{
    if( ( item_contain(book1_par) == false ) || ( item_contain(book2_par) == false ) )
    {
        cout << "Book to Swap not in List\n";
    }
    else
    {       
        if(book1_par.get_next() == &book2_par || book2_par.get_next() == &book1_par)
        {

            if(book1_par == *tail || book2_par == *tail)    //tail involved
            {
                cout << "Sort Swapping....Adjacent....Tail Involed\n";  //Adjacent Items -- considered separately
                if(book1_par.get_next() == &book2_par)  //-- book1_par -- book2_par --
                {
                    if( (book2_par == *tail) == false )
                    {
                        cout << "Attention\n";
                        exit(1);
                    }
                    Book book1, book2;

                    book1.copy(book1_par);
                    book2.copy(book2_par);
                    list_delete(book1_par);
                    list_delete(book2_par);
                    list_push(book2);
                    list_push(book1);
                }

                if(book2_par.get_next() == &book1_par)  //-- book2_par -- book1_par --
                {
                    if( (book1_par == *tail) == false )
                    {
                        cout << "Attention\n";
                        exit(1);
                    }
                    Book book1, book2;

                    book1.copy(book1_par);
                    book2.copy(book2_par);
                    list_delete(book1_par);
                    list_delete(book2_par);
                    list_push(book1);
                    list_push(book2);
                }

            }
            else
            {
                cout << "Sort Swapping....Adjacent\n";  //Adjacent Items -- considered separately
                if(book1_par.get_next() == &book2_par)  //-- book1_par -- book2_par --
                {
                    /*
                    Book book_tmp;
                    book_tmp.copy(book2_par);
                    list_delete(book2_par);
                    list_insert(book1_par, book_tmp);   
                    */
                    Book *insert_position1, *insert_position2;
                    Book book1, book2;
                    insert_position1 = book2_par.get_next();
                    insert_position2 = book1_par.get_next();
                    book1.copy(book1_par);
                    book2.copy(book2_par);
                    list_delete(book1_par);
                    list_delete(book2_par);
                    list_insert(*insert_position1, book1); //
                    list_insert(book1, book2);
                }

                if(book2_par.get_next() == &book1_par)  //-- book2_par -- book1_par --
                {
                    /*
                    Book book_tmp;
                    book_tmp.copy(book1_par);
                    list_delete(book1_par);
                    list_insert(book2_par, book_tmp);
                    */
                    Book *insert_position1, *insert_position2;
                    Book book1, book2;
                    insert_position1 = book2_par.get_next();
                    insert_position2 = book1_par.get_next();
                    book1.copy(book1_par);
                    book2.copy(book2_par);
                    list_delete(book1_par);
                    list_delete(book2_par);
                    list_insert(*insert_position2, book2);
                    list_insert(book2, book1); //

                }           

            }

        }
        else
        {

            bool book1_position_is_tail(false), book2_position_is_tail(false);
            if(book1_par == *tail)
            {
                cout << "Sort Swapping....Tail Involved\n";
                book2_position_is_tail = true;
            }
            else if(book2_par == *tail)
            {
                cout << "Sort Swapping....Tail Involved\n";
                book1_position_is_tail = true;
            }
            else
            {
                cout << "Sort Swapping....\n";//The method to swap two non-adjacent items is to 1.copy the info 2.delete items 3.insert copies to appropriate positions 
            }
            Book *insert_position1, *insert_position2;
            Book book1, book2;
            insert_position1 = book2_par.get_next();
            insert_position2 = book1_par.get_next();
            book1.copy(book1_par);
            book2.copy(book2_par);
            list_delete(book1_par);
            list_delete(book2_par);
            if(book2_position_is_tail)
            {
                list_insert(*insert_position1, book1);
                list_push(book2);
            }
            else if(book1_position_is_tail)
            {
                list_insert(*insert_position2, book2);
                list_push(book1);
            }
            else
            {
                list_insert(*insert_position1, book1);
                list_insert(*insert_position2, book2);
            }
        }

    }

    return;
}

void BookList::sort_index()
{
    cout << "Sorting Index ....\n";
    Book *p;
    Book *e;    //used to know exactly where p points to and should points to
    p = head;
    while(p!=tail)
    {
        cout << "Target: " << p->get_index() << endl;;
        Book *n, *target;
        n = p->get_next();
        target = p;
        while(n->get_next()!=NULL)
        {           
            cout << "Compare :" << n->get_index() << endl;;
            if(target->get_index() > n->get_index())
            {
                target->output();
                n->output();
                cout << "New Target:" << n->get_index() << endl;
                target = n;
            }
            n = n->get_next();
        }
        cout << "Compare :" << n->get_index() << endl;;
        if(target->get_index() > tail->get_index())
        {
            target->output();
            tail->output();
            cout << "New Target:" << tail->get_index() << "----Tail\n";
            target = tail;
        }
        cout << "----Time to decide swap or not----\n";
        cout << "Target:\t";
        target->output();
        cout << "P:\t";
        p->output();
        if(target != p)
        {
            cout << "----Swap is Expected----\n";
            e = list_preceder(*p);  //
            if(e == NULL)
            {
                cout << "e:\t Head" << head->get_index() << endl;
            }
            else
            {
                cout << "e:\t" << e->get_index() << endl;
            }
            sort_swap(*target, *p);
            if(e == NULL)
            {
                p = head;
            }
            else
            {
                p = e->get_next();      //
            }
            cout << "p:\t" << p->get_index() << endl;
            cout << "Implemented One Swap\n";
            output();
        }
        else
        {
            cout << "----No Swap----\n";
        }
        cout << "Current P:\t";
        p->output();    //????? How does P point correctly?
        p = p->get_next();
        cout << "---------------Finished One round of Sorting---------------\n";
        if(p == tail)
        {
            cout << "It is done\n";
        }
        cout << "Current Tail:\t";
        tail->output();
        cout << "Current P:\t";
        p->output();
    }
    cout << "...............After Sorting...............\n";
    output();
    return;
}

void BookList::list_union(BookList& book_list_par)
{
    Book *p;
    cout << "Union....\n";
    for(int i=0; i<book_list_par.get_length(); i++)
    {
        p = book_list_par.item_read(i);
        if(item_contain(*p))
        {
            cout << "In List\n";    
        }
        else
        {
            cout << "New: Pushing....\n";
            list_push(*p);
        }
    }

    return;
}

void BookList::list_union_sorted(BookList& book_list_par)
{
    sort_index();
    Book *p;
    cout << "Union Sorted....\n";
    for(int i=0; i<book_list_par.get_length(); i++)
    {
        p = book_list_par.item_read(i);
        if(item_contain(*p))
        {
            cout << "In List\n";
        }
        else
        {
            cout << "New: " << p->get_name() << " (" << p->get_index() << ") Insert Sorted....\n";
            list_insert_sorted(*p);
        } 
    }

    return;
}

void BookList::list_combine(BookList& book_list_par)
{
    tail->modify_next(book_list_par.get_head());
    tail = book_list_par.get_tail();
    return;
}

void BookList::output()
{
    Book *i;
    i = head;
    cout << "Book List (" << size << ")\n";
    if(size == 0)
    {
        cout << "----Empty Book List----\n";
    } 
    else
    {
        cout << "-------Start-------\n";
        while(i!=NULL)
        {
            i->output();
            i = i->get_next();
        }
        cout << "-------End-------\n";
    }

    return; 
}

bool BookList::empty()
{
    return (size==0);
}

int BookList::get_length()
{
    return size;
}
//----------------------------------------BookList---------------------------------------

//------------------------------------------BookArray------------------------------------
BookArray::BookArray()
{
    size = 100;
    length = 0;
    base = new Book [size];
    for(int i=0; i<size; i++)
    {
        base[i].modify("NONE", 0);
        base[i].modify_next(NULL);
    }
}

BookArray::BookArray(int size_par)
{
    size = size_par;
    length = 0;
    base = new Book [size_par];
    for(int i=0; i<size_par; i++)
    {
        base[i].modify("NONE", 0);
        base[i].modify_next(NULL);
    }
}

BookArray::BookArray(BookArray& book_list_array_par)
{
    size = book_list_array_par.get_length();
    length = size;
    base = new Book [size];
    for(int i=0; i<size; i++)
    {
        base[i].modify( (book_list_array_par.base)[i].get_name(), (book_list_array_par.base)[i].get_index() );
        base[i].modify_next(NULL);
    }
}

BookArray::BookArray(BookList& book_list_par)
{
    size = book_list_par.get_length();
    length = size;
    base = new Book [size];
    Book *p = book_list_par.get_head();
    for(int i=0; i<size; i++)
    {
        base[i].modify(p->get_name(), p->get_index());
        base[i].modify_next(NULL);
        p = p->get_next();
    }
}

BookArray::~BookArray()
{
    delete [] base;
}


Book* BookArray::get_base()
{
    return base;
}

bool BookArray::empty()
{
    return length==0 ;
}

bool BookArray::full()
{
    return length==size ;
}

int BookArray::get_size()
{
    return size;
}

int BookArray::get_length()
{
    return length;
}

int BookArray::get_space()
{
    return size-length;
}

void BookArray::output()
{
    cout << "Book Array (" << length << "/" << size << ")\n";
    for(int i=0; i<length; i++)
    {
        base[i].output();
    }
    return;
}

void BookArray::resize()
{   
    Book *new_base;
    int new_size = size * 2;
    new_base = new Book [new_size];
    copy_to(new_base);
    delete [] base;
    base = new_base;
    size = new_size;
    return;
}

void BookArray::resize(int size_add_par)
{
    Book *new_base;
    int new_size = size + size_add_par;
    new_base = new Book [new_size];
    copy_to(new_base);
    delete [] base;
    base = new_base;
    size = new_size;
    return;
}

void BookArray::copy_to(Book *& base_par)
{
    for(int i=0; i<length; i++)
    {
        base_par[i] = base[i];
    }

    //memcpy(book_array_par, base, length*sizeof(Book));
    return;
}

void BookArray::array_push(Book& book_par)
{
    if(full())
    {
        cout << "Array is Full....Trying to resize the array\n";
        resize();
    }

    base[length].copy(book_par); 
    length ++;
    return;
}

void BookArray::array_pop()
{
    base[length].modify("NONE", 0); 
    length --;
    return;
}

void BookArray::array_insert(Book& book_position_par, Book& book_par)
{
    int i = 0;
    for(i=0; i<length; i++)
    {
        if(base[i].get_name() == book_position_par.get_name() && base[i].get_index() == book_position_par.get_index())
        {
            break;
        }
    }
    if(i == length)
    {
        cout << "Exit: Array Insert: No this Book Position in Array....\n";
        exit(1);
    }
    array_insert(i, book_par);
    return;
}

void BookArray::array_insert(int n_par, Book& book_par)
{
    cout << n_par << endl;
    if(n_par >= size || n_par < 0)
    {
        cout << "Exit: Array Insert: Inserting Position n out of size range\n";
        exit(1);
    }
    if(full())
    {
        cout << "Array is Full....Trying to resize the array\n";
        resize();
    }
    for(int j=length; j>n_par; j--)
    {
        base[j].copy(base[j-1]);
    }
    base[n_par].copy(book_par);
    length ++;
    return;
}

void BookArray::array_insert_sorted(Book& book_par)
{
    array_insert(insert_locate_sorted(book_par), book_par);
    return;
}

int BookArray::insert_locate_sorted(Book& book_par)
{
    int i(-1);
    for(i=0; i<length; i++)
    {
        if(base[i].get_index() > book_par.get_index())
        {
            break;
        }
    }
    return i;
}

void BookArray::array_delete(Book& book_par)
{
    int i = 0;
    for(i=0; i<length; i++)
    {
        if(base[i].get_name() == book_par.get_name() && base[i].get_index() == book_par.get_index())
        {
            break;
        }
    }
    if(i == length)
    {
        cout << "Exit: Array Delete: No this Book in Array....\n";
        exit(1);
    }
    array_delete(i);
    return;
}

int BookArray::item_locate(Book& book_par)
{
    int i = 0;
    for(i=0; i<length; i++)
    {
        if(base[i].get_name() == book_par.get_name() && base[i].get_index() == book_par.get_index())
        {
            break;
        }
    }
    if(i == length)
    {
        cout << "Item Locate: No this Book in Array....\n";
        return -1;
    }
    return i;

}

void BookArray::array_delete(int n_par)
{
    for(int i=n_par; i<length-1; i++)
    {
        base[i].copy(base[i+1]);
    }
    length --;
    return;
}


void BookArray::sort_swap(Book& book1_par, Book& book2_par)
{
    sort_swap( item_locate(book1_par), item_locate(book2_par) );
    return;
}

void BookArray::sort_swap(int n1_par, int n2_par)
{
    Book tmp;
    tmp.copy(base[n1_par]);
    base[n1_par].copy(base[n2_par]);
    base[n2_par].copy(base[n1_par]);
    return;
}

void BookArray::sort_index()
{
    for(int i=0; i<length; i++)
    {
        for(int j=i; j<length; j++)
        {
            if(base[i].get_index() > base[j].get_index())
            {
                swap(base[i], base[j]);
            }
        }
    }
    cout << "Sort Index Finished....\n";
    return;
}

void BookArray::array_union(BookArray& book_array_par)
{
    for(int i=0; i<book_array_par.get_length(); i++)
    {
        if(item_locate( (book_array_par.get_base())[i] ) != -1)
        {
            cout << "Item In Array....No Push....\n";
        }
        else
        {
            cout << "New....Pushing\n";
            array_push( (book_array_par.get_base())[i] );
        }
    }
    return;
}

void BookArray::array_union_sorted(BookArray& book_array_par)
{
    for(int i=0; i<book_array_par.get_length(); i++)
    {
        if(item_locate( (book_array_par.get_base())[i] ) != -1)
        {
            cout << "Item In Array....No Insert....\n";
        }
        else
        {
            cout << "New....Inserting\n";
            array_insert_sorted( (book_array_par.get_base())[i] );
        }
    }
    return;
}

void BookArray::array_intersect(BookArray& book_array_par)
{
    for(int i=0; i<length; i++)
    {
        if(book_array_par.item_locate(base[i]) != -1)
        {
            cout << "Item In Array....No Delete....\n";
        }
        else
        {
            cout << "Item Not in Array....Deleting....\n";
            array_delete(base[i]);
        }
    }

    return;
}
//------------------------------------------BookArray------------------------------------
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资源 5来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资 5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值