Homework Solution: Ok so I need these following exercises completed in C++. The code that goes with them will foll…

    Ok so I need these following exercises completed in C++. The code that goes with them will follow after the exercises. The code is after two lines. In all needs to be completed in C++. Also each individual program will be separated by Asteriks(************) Exercise 2: Implementing a Doubly Linked List Modify the class Linked List in Exercise 1 to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test your DoublyLinkedList class. Exercise 3: Implementing a Bag Class With a Linked List Create a class Bag that uses a linked list to store the bag items. The item type must be char. The class should have the methods listed below. Create a main() that will store in a bag object a fixed number of characters entered by the program user. After the input is completed, the program should modify the bag content so that it does not contain any duplicate characters, if duplicates were entered. For example, if the user entered 'M' 'I' 'S' 'S' 'I' 'S' 'S' 'I' 'P' 'P' 'I', the characters remaining in the bag after the removal of duplicates would be 'M' 'I' 'S' 'P'. Bag(): default constructor ~Bag(): class destructor bool isEmpty(): determines whether the bag is empty void print(): prints the bag elements int getSize(): returns the number of items in the bag void clear(): removes all of the items from the bag void add(char item): adds an item to the bag void remove(char item): removes an item from the bag; only one occurrence of the item should be removed. int count(char item): counts the number of occurrences of an item in the bag. (Note that you can reuse the code in Exercise 1 for the LinkedList class to create your Bag class. It will help you to save development time.) Exercise 4: Using the C++ STL List Write a program that fills a STL list object with 10 random integers, each in the interval [0, 20], and prints how many times each integer in the interval [0, 20] appears in the list. ____________________________________________________________________________________________________________________________________________________________________________________________ main.cpp for list container /******************************* * Week 2 lesson: * * using the list container * *******************************/ #include <iostream> #include <list> using namespace std; int main() { list<int> numbers; for (int i=0; i<10; i++) numbers.push_back(rand()%100); while(!numbers.empty()) { int x = numbers.front(); cout << x << " "; numbers.pop_front(); } cout << endl; return 0; } ******************************************************************************* Main.cpp(linkedlists) /******************************* * Week 2 lesson: * * a simple LinkedList class * *******************************/ #include <iostream> #include "LinkedList.h" using namespace std; int main() { LinkedList myList; int x; //Add 5 random numbers to list for (int i=0; i < 5; i++) myList.add(rand()%20); cout << "1 - Display the list elements" << endl; cout << "2 - Is it empty?" << endl; cout << "3 - Add element" << endl; cout << "4 - Delete element" << endl; cout << "5 - Exit" << endl; int option; //Loop to test the LinkedList class methods do { cout << endl << "Enter your choice: "; cin >> option; switch(option) { case 1: cout << "List elements: "; myList.display(); break; case 2: if (myList.isEmpty()) cout << "List is empty"<< endl; else cout << "List is not empty" << endl; break; case 3: cout << "Enter an element to add at the beginning of the list: "; cin >> x; myList.add(x); break; case 4: cout << "Enter an element to delete from the list: "; cin >> x; myList.remove(x); break; case 5: cout << "All done!" << endl; break; default: cout << "Invalid choice!" << endl; } } while (option != 5); return 0; } Linkedlist .h file /******************************* * Week 2 lesson: * * a simple LinkedList class * *******************************/ /* * Linked list node. */ struct Node { int info; //element stored in this node Node *next; //link to next node }; /* * Class implementing a linked list. */ class LinkedList { public: LinkedList(); ~LinkedList(); bool isEmpty(); void display(); void add(int); void remove(int); private: Node *first; //pointer to header (dummy) node }; Linkedlist.cpp /******************************* * Week 2 lesson: * * a simple LinkedList class * *******************************/ #include <iostream> #include "LinkedList.h" using namespace std; /* * Initializes the list to empty creating a dummy header node. */ LinkedList::LinkedList() { first = new Node; first->next = NULL; } /* * Destructor. Deallocates all the nodes of the linked list, * including the header node. */ LinkedList::~LinkedList() { Node *temp; while (first != NULL) { temp=first; first=first->next; delete temp; } } /* * Determines whether the list is empty. * * Returns true if the list is empty, false otherwise. */ bool LinkedList::isEmpty() { return first->next == NULL; } /* * Prints the list elements. */ void LinkedList::display() { Node * current = first->next; while(current != NULL) { cout << current->info << " "; current = current->next; } cout << endl; } /* * Adds the element x to the beginning of the list. * * x: element to be added to the list. */ void LinkedList::add(int x) { Node *p = new Node; p->info = x; p->next = first->next; first->next = p; } /* * Removes the first occurrence of x from the list. If x is not found, * the list remains unchanged. * * x: element to be removed from the list. */ void LinkedList::remove(int x) { Node * old = first->next, * p = first; //Finding the address of the node before the one to be deleted bool found = false; while (old != NULL && !found) { if (old->info == x) found = true; else { p = old; old = p->next; } } //if x is in the list, remove it. if (found) { p->next = old->next; delete old; } } ********************************************************************************************** main.cpp for iterators /******************************* * Week 2 lesson: * * using iterators * *******************************/ #include <iostream> #include <list> using namespace std; int main() { list<int> numbers; for (int i=0; i<10; i++) numbers.push_back(rand()%100); list<int>::iterator it; for (it = numbers.begin(); it!=numbers.end(); ++it) { cout << *it << " "; } cout << endl; return 0; }

    Expert Answer

     
    #include <cassert> #include <cstdlib>

    Ok so I insufficiency these subjoined exercises completed in C++. The regulation that goes with them accomplish thrive behind the exercises.

    The regulation is behind brace lines. In every insufficiencys to be completed in C++. Also each idiosyncratic program accomplish be divided by Asteriks(************)

    Exercise 2: Implementing a Doubly Linked Catalogue

    Modify the arrange Linked Catalogue in Exercise 1 to fashion it a Doubly Linked Catalogue. Name your arrange DoublyLinkedList. Supplement a mode supplementPurpose to supplement an integer at the purpose of the catalogue and a mode vaultInReverse to imimprint the catalogue backwards.

    void supplementEnd(int x): fashion this mode to supplement x to the purpose of the catalogue.

    void vaultInReverse(): fashion this mode to vault the catalogue atoms from the last individual to the leading single.

    Fashion a ocean() business to touchstundivided your DoublyLinkedCatalogue arrange.

    Exercise 3: Implementing a Bag Arrange With a Linked Catalogue

    Fashion a arrange Bag that uses a linked catalogue to stock the bag individuals. The individual model must be char. The arrange should accept the modes catalogueed underneath. Fashion a ocean() that accomplish stock in a bag goal a unwandering sum of characters entered by the program user. Behind the input is completed, the program should modify the bag resigned so that it does referable inclose any duplicate characters, if duplicates were entered. Control stance, if the user entered ‘M’ ‘I’ ‘S’ ‘S’ ‘I’ ‘S’ ‘S’ ‘I’ ‘P’ ‘P’ ‘I’, the characters fostering in the bag behind the disruption of duplicates would be ‘M’ ‘I’ ‘S’ ‘P’.

    Bag(): defect constructor

    ~Bag(): arrange destructor

    bool isEmpty(): determines whether the bag is leisure

    void imprint(): imprints the bag atoms

    int getSize(): avail the sum of individuals in the bag

    void unclouded(): separates every of the individuals from the bag

    void supplement(char individual): supplements an individual to the bag

    void separate(char individual): separates an individual from the bag; singly single event of the individual should be separated.

    int estimate(char individual): estimates the sum of events of an individual in the bag.

    (Note that you can reuse the regulation in Exercise 1 control the LinkedCatalogue arrange to fashion your Bag arrange. It accomplish aid you to catch bud space.)

    Exercise 4: Using the C++ STL Catalogue

    Write a program that fills a STL catalogue goal with 10 casual integers, each in the gap [0, 20], and imprints how multifarious spaces each integer in the gap [0, 20] appears in the catalogue.

    ____________________________________________________________________________________________________________________________________________________________________________________________

    main.cpp control catalogue incloseer

    /*******************************

    * Week 2 lesson: *

    * using the catalogue incloseer *

    *******************************/

    #include <iostream>

    #include <list>

    using namespace std;

    int ocean()

    {

    list<int> sums;

    control (int i=0; i<10; i++)

    numbers.push_back(rand()%100);

    while(!numbers.empty())

    {

    int x = sums.front();

    cout << x << ” “;

    numbers.pop_front();

    }

    cout << purposel;

    return 0;

    }

    *******************************************************************************

    Main.cpp(linkedlists)

    /*******************************

    * Week 2 lesson: *

    * a homely LinkedCatalogue arrange *

    *******************************/

    #include <iostream>

    #include “LinkedList.h”

    using namespace std;

    int ocean()

    {

    LinkedCatalogue myList;

    int x;

    //Supplement 5 casual sums to catalogue

    control (int i=0; i < 5; i++)

    myList.add(rand()%20);

    cout << “1 – Vault the catalogue atoms” << purposel;

    cout << “2 – Is it leisure?” << purposel;

    cout << “3 – Supplement atom” << purposel;

    cout << “4 – Delete atom” << purposel;

    cout << “5 – Exit” << purposel;

    int liberty;

    //Loop to touchstundivided the LinkedCatalogue arrange modes

    do

    {

    cout << purposel << “Enter your choice: “;

    cin >> liberty;

    switch(option)

    {

    case 1:

    cout << “Catalogue atoms: “;

    myList.display();

    break;

    case 2:

    if (myList.isEmpty()) cout << “Catalogue is leisure”<< purposel;

    else cout << “Catalogue is referable leisure” << purposel;

    break;

    case 3:

    cout << “Enter an atom to supplement at the initiation of the catalogue: “;

    cin >> x;

    myList.add(x);

    break;

    case 4:

    cout << “Enter an atom to delete from the catalogue: “;

    cin >> x;

    myList.remove(x);

    break;

    case 5:

    cout << “Every done!” << purposel;

    break;

    default: cout << “Invalid choice!” << purposel;

    }

    } while (liberty != 5);

    return 0;

    }

    Linkedcatalogue .h file

    /*******************************

    * Week 2 lesson: *

    * a homely LinkedCatalogue arrange *

    *******************************/

    /*

    * Linked catalogue node.

    */

    struct Node

    {

    int info; //atom stockd in this node

    Node *next; //link to direct node

    };

    /*

    * Arrange implementing a linked catalogue.

    */

    arrange LinkedList

    {

    public:

    LinkedList();

    ~LinkedList();

    bool isEmpty();

    void vault();

    void supplement(int);

    void separate(int);

    private:

    Node *first; //pointer to header (dummy) node

    };

    Linkedlist.cpp

    /*******************************

    * Week 2 lesson: *

    * a homely LinkedCatalogue arrange *

    *******************************/

    #include <iostream>

    #include “LinkedList.h”

    using namespace std;

    /*

    * Initializes the catalogue to leisure creating a dummy header node.

    */

    LinkedList::LinkedList()

    {

    leading = upstart Node;

    first->direct = NULL;

    }

    /*

    * Destructor. Deallocates every the nodes of the linked catalogue,

    * including the header node.

    */

    LinkedList::~LinkedList()

    {

    Node *temp;

    while (leading != NULL)

    {

    temp=first;

    first=first->next;

    delete temp;

    }

    }

    /*

    * Determines whether the catalogue is leisure.

    *

    * Avail penny if the catalogue is leisure, mock incorrectly.

    */

    bool LinkedList::isEmpty()

    {

    return leading->direct == NULL;

    }

    /*

    * Imprints the catalogue atoms.

    */

    void LinkedList::display()

    {

    Node * popular = leading->next;

    while(popular != NULL)

    {

    cout << popular->info << ” “;

    popular = popular->next;

    }

    cout << purposel;

    }

    /*

    * Supplements the atom x to the initiation of the catalogue.

    *

    * x: atom to be supplemented to the catalogue.

    */

    void LinkedList::add(int x)

    {

    Node *p = upstart Node;

    p->info = x;

    p->direct = leading->next;

    first->direct = p;

    }

    /*

    * Separates the leading event of x from the catalogue. If x is referable establish,

    * the catalogue debris unnatural.

    *

    * x: atom to be separated from the catalogue.

    */

    void LinkedList::remove(int x)

    {

    Node * aged = leading->next,

    * p = leading;

    //Finding the supplementress of the node precedently the single to be deleted

    bool establish = mock;

    while (aged != NULL && !found)

    {

    if (old->info == x) establish = penny;

    else

    {

    p = aged;

    aged = p->next;

    }

    }

    //if x is in the catalogue, separate it.

    if (found)

    {

    p->direct = aged->next;

    delete aged;

    }

    }

    **********************************************************************************************

    main.cpp control iterators

    /*******************************

    * Week 2 lesson: *

    * using iterators *

    *******************************/

    #include <iostream>

    #include <list>

    using namespace std;

    int ocean()

    {

    list<int> sums;

    control (int i=0; i<10; i++)

    numbers.push_back(rand()%100);

    list<int>::iterator it;

    control (it = sums.begin(); it!=numbers.end(); ++it)

    {

    cout << *it << ” “;

    }

    cout << purposel;

    return 0;

    }

    Expert Reply

     

    #include <cassert>
    #include <cstdlib>
    #include <iostream>
    #include “node.h”
    #include “LinkedList.h”
    using namespace std;
    namespace assign1

    {

    LinkedList::LinkedList()
    {

    head_ptr = NULL,
    many_nodes = 0;
    }
    LinkedList::LinkedList(const LinkedList& fount)
    {
    node *tail_ptr; //needed control evidence of catalogue_copy
    list_copy(source.head_ptr, head_ptr, tail_ptr);
    many_nodes = fount.many_nodes;
    }
    LinkedList::~LinkedList()
    {
    list_clear(head_ptr);
    many_nodes = 0
    }
    size_model LinkedList::count(const value_type& target) const
    {
    size_model reply;
    const node *cursor;
    reply = 0;
    cursor = catalogue_search(head_ptr, target);
    while(cursor!=NULL)

    {

    ++answer;
    cursor = cursor->get_link();
    cursor = catalogue_search(cursor,target);
    }
    return reply;
    }
    void LinkedList::list_head_insert(node* head_ptr, const node::value_model note)
    53
    {
    list_head_insert(head_ptr,entry);
    ++many_nodes;
    }
    void LinkedList::list_tail_insert(node* tail_ptr, const node::value_type& note)
    {
    new_elem = upstart node();
    new_elem-> set_data(entry);
    new_elem->set_link(NULL);
    tail_ptr->set_link(new_elem);
    tail_ptr = upstart_elem;
    ++multifarious nodes;
    }
    void operator +=(const LinkedList& supplementend)
    {
    node *copy_head_ptr;
    node *copy_tail_ptr;
    if (addend.many_nodes > 0)
    {
    list_copy(Addend.head_ptr, copy_head_ptr, copy_tail_ptr);
    copy_tail_ptr->set_link(head_ptr);
    head_ptr = copy_head_ptr;
    many_nodes += supplementend.many_nodes;
    }
    }
    }