Homework Solution: Ok so I need these following exercises completed in C++. The code that…

    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

     
    // exercise 2 #include <iostream> using namespace std;

    Ok so I want these thriveing exercises completed in C++. The legislation that goes with them accomplish thrive succeeding the uses.

    The legislation is succeeding two lines. In complete wants to be completed in C++. Also each specific program accomplish be divided by Asteriks(************)

    Use 2: Implementing a Doubly Linked Roll

    Dissimilate the adjust Linked Roll in Use 1 to effect it a Doubly Linked Roll. Name your adjust DoublyLinkedList. Appsight a manner appendSight to appsight an integer at the sight of the roll and a manner paradeInReverse to imimimprint the roll backwards.

    void appendEnd(int x): generate this manner to appsight x to the sight of the roll.

    void paradeInReverse(): generate this manner to parade the roll components from the laproof individual to the ceemost single.

    Generate a ocean() discharge to proof your DoublyLinkedRoll adjust.

    Use 3: Implementing a Bag Adjust With a Linked Roll

    Generate a adjust Bag that uses a linked roll to treasury the bag individuals. The individual expression must be char. The adjust should enjoy the manners rolled underneath. Generate a ocean() that accomplish treasury in a bag sight a urban sum of characters entered by the program user. Succeeding the input is completed, the program should dissimilate the bag resigned so that it does referable hantiquated any likeness characters, if likenesss were entered. Ce in, if the user entered ‘M’ ‘I’ ‘S’ ‘S’ ‘I’ ‘S’ ‘S’ ‘I’ ‘P’ ‘P’ ‘I’, the characters cherishing in the bag succeeding the resistance of likenesss would be ‘M’ ‘I’ ‘S’ ‘P’.

    Bag(): lapse constructor

    ~Bag(): adjust destructor

    bool isEmpty(): determines whether the bag is void

    void imprint(): imprints the bag components

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

    void unobstructed(): separates complete of the individuals from the bag

    void append(char individual): appends an individual to the bag

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

    int sum(char individual): sums the sum of adventures of an individual in the bag.

    (Note that you can reuse the legislation in Use 1 ce the LinkedRoll adjust to generate your Bag adjust. It accomplish acceleration you to obviate outgrowth season.)

    Use 4: Using the C++ STL Roll

    Write a program that fills a STL roll sight with 10 purposeless integers, each in the gap [0, 20], and imprints how multifarious seasons each integer in the gap [0, 20] appears in the roll.

    ____________________________________________________________________________________________________________________________________________________________________________________________

    main.cpp ce roll holder

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

    * Week 2 lesson: *

    * using the roll holder *

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

    #include <iostream>

    #include <list>

    using namespace std;

    int ocean()

    {

    list<int> sums;

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

    numbers.push_back(rand()%100);

    while(!numbers.empty())

    {

    int x = sums.front();

    cout << x << ” “;

    numbers.pop_front();

    }

    cout << sightl;

    return 0;

    }

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

    Main.cpp(linkedlists)

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

    * Week 2 lesson: *

    * a sickly LinkedRoll adjust *

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

    #include <iostream>

    #include “LinkedList.h”

    using namespace std;

    int ocean()

    {

    LinkedRoll myList;

    int x;

    //Appsight 5 purposeless sums to roll

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

    myList.add(rand()%20);

    cout << “1 – Parade the roll components” << sightl;

    cout << “2 – Is it void?” << sightl;

    cout << “3 – Appsight component” << sightl;

    cout << “4 – Delete component” << sightl;

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

    int discretion;

    //Loop to proof the LinkedRoll adjust manners

    do

    {

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

    cin >> discretion;

    switch(option)

    {

    case 1:

    cout << “Roll components: “;

    myList.display();

    break;

    case 2:

    if (myList.isEmpty()) cout << “Roll is void”<< sightl;

    else cout << “Roll is referable void” << sightl;

    break;

    case 3:

    cout << “Enter an component to appsight at the preface of the roll: “;

    cin >> x;

    myList.add(x);

    break;

    case 4:

    cout << “Enter an component to delete from the roll: “;

    cin >> x;

    myList.remove(x);

    break;

    case 5:

    cout << “Complete done!” << sightl;

    break;

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

    }

    } suitableness (discretion != 5);

    return 0;

    }

    Linkedroll .h file

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

    * Week 2 lesson: *

    * a sickly LinkedRoll adjust *

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

    /*

    * Linked roll node.

    */

    struct Node

    {

    int info; //component treasuryd in this node

    Node *next; //link to present node

    };

    /*

    * Adjust implementing a linked roll.

    */

    adjust LinkedList

    {

    public:

    LinkedList();

    ~LinkedList();

    bool isEmpty();

    void parade();

    void append(int);

    void separate(int);

    private:

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

    };

    Linkedlist.cpp

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

    * Week 2 lesson: *

    * a sickly LinkedRoll adjust *

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

    #include <iostream>

    #include “LinkedList.h”

    using namespace std;

    /*

    * Initializes the roll to void creating a dummy header node.

    */

    LinkedList::LinkedList()

    {

    foremost = uprouse Node;

    first->present = NULL;

    }

    /*

    * Destructor. Deallocates complete the nodes of the linked roll,

    * including the header node.

    */

    LinkedList::~LinkedList()

    {

    Node *temp;

    suitableness (foremost != NULL)

    {

    temp=first;

    first=first->next;

    delete temp;

    }

    }

    /*

    * Determines whether the roll is void.

    *

    * Profits gentleman if the roll is void, bogus incorrectly.

    */

    bool LinkedList::isEmpty()

    {

    return ceemost->present == NULL;

    }

    /*

    * Imprints the roll components.

    */

    void LinkedList::display()

    {

    Node * running = ceemost->next;

    while(running != NULL)

    {

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

    running = running->next;

    }

    cout << sightl;

    }

    /*

    * Appends the component x to the preface of the roll.

    *

    * x: component to be appended to the roll.

    */

    void LinkedList::add(int x)

    {

    Node *p = uprouse Node;

    p->info = x;

    p->present = ceemost->next;

    first->present = p;

    }

    /*

    * Separates the ceemost adventure of x from the roll. If x is referable set-up,

    * the roll offal unchanged.

    *

    * x: component to be separated from the roll.

    */

    void LinkedList::remove(int x)

    {

    Node * antiquated = ceemost->next,

    * p = ceemost;

    //Finding the appendress of the node anteriorly the single to be deleted

    bool set-up = bogus;

    suitableness (antiquated != NULL && !found)

    {

    if (old->info == x) set-up = gentleman;

    else

    {

    p = antiquated;

    antiquated = p->next;

    }

    }

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

    if (found)

    {

    p->present = antiquated->next;

    delete antiquated;

    }

    }

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

    main.cpp ce iterators

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

    * Week 2 lesson: *

    * using iterators *

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

    #include <iostream>

    #include <list>

    using namespace std;

    int ocean()

    {

    list<int> sums;

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

    numbers.push_back(rand()%100);

    list<int>::iterator it;

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

    {

    cout << *it << ” “;

    }

    cout << sightl;

    return 0;

    }

    Expert Reply

     

    // use 2

    #include <iostream>

    using namespace std;

    struct Node

    {

    int info; //component treasuryd in this node

    Node *next;

    Node *prev;//link to present node

    };

    adjust DoublyLinkedList

    {

    public:

    DoublyLinkedList();

    void appendEnd(int x);

    void paradeInReverse();

    private:

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

    };

    DoublyLinkedList::DoublyLinkedList()

    {

    laproof = uprouse Node;

    last->present = NULL;

    last->prev= NULL;

    }

    ////// appendSight discharge

    void DoublyLinkedList::addEnd(int x)

    {

    Node *p= uprouse Node;

    p->info = x;

    p->prev = NULL;

    if(last->prev != NULL)

    {

    p->prev = latest->prev;

    last->prev->present = p;

    }

    last->prev = p;

    p->present = latest;

    }

    // Parade Inverse discharge

    void DoublyLinkedList::displayInReverse()

    {

    Node *p = latest;

    while(p->prev != NULL)

    {

    p=p->prev;

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

    }

    cout<<endl;

    }

    int ocean()

    {

    DoublyLinkedRoll D;

    D.addEnd(5);

    D.addEnd(6);

    D.addEnd(7);

    D.displayInReverse();

    return 0;

    }

    // use 3

    #include <iostream>

    using namespace std;

    struct Node

    {

    char info; //component treasuryd in this node

    Node *next;

    };

    adjust Bag

    {

    public:

    Bag();

    ~Bag();

    bool isEmpty();

    void parade();

    void append(char);

    void separate(char);

    void dissimilate();

    private:

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

    };

    Bag::Bag()

    {

    foremost = uprouse Node;

    first->present = NULL;

    }

    /*

    * Destructor. Deallocates complete the nodes of the linked roll,

    * including the header node.

    */

    Bag::~Bag()

    {

    Node *temp;

    suitableness (foremost != NULL)

    {

    temp=first;

    first=first->next;

    delete temp;

    }

    }

    /*

    * Determines whether the roll is void.

    *

    * Profits gentleman if the roll is void, bogus incorrectly.

    */

    bool Bag::isEmpty()

    {

    return ceemost->present == NULL;

    }

    /*

    * Imprints the roll components.

    */

    void Bag::display()

    {

    Node * running = ceemost->next;

    while(running != NULL)

    {

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

    running = running->next;

    }

    cout << sightl;

    }

    /*

    * Appends the component x to the preface of the roll.

    *

    * x: component to be appended to the roll.

    */

    void Bag::add(char x)

    {

    Node *p = uprouse Node;

    p->info = x;

    p->present = ceemost->next;

    first->present = p;

    }

    /*

    * Separates the ceemost adventure of x from the roll. If x is referable set-up,

    * the roll offal unchanged.

    *

    * x: component to be separated from the roll.

    */

    void Bag::remove(char x)

    {

    Node * antiquated = ceemost->next,

    * p = ceemost;

    //Finding the appendress of the node anteriorly the single to be deleted

    bool set-up = bogus;

    suitableness (antiquated != NULL && !found)

    {

    if (old->info == x) set-up = gentleman;

    else

    {

    p = antiquated;

    antiquated = p->next;

    }

    }

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

    if (found)

    {

    p->present = antiquated->next;

    delete antiquated;

    }

    }

    void Bag::modify()

    {

    int decline[26];

    for(int i=0;i<26;i++)

    {

    flag[i] = 0;

    }

    Node *rouse = ceemost -> present;

    Node *prev= rouse,*temp;

    while(rouse != NULL)

    {

    if(flag[start->info-‘A’] == 0)

    {

    flag[start->info-‘A’] = 1;

    prev = rouse;

    rouse = rouse->next;

    }

    else

    {

    prev->present = rouse->next;

    delete(start);

    rouse = prev->next;

    }

    }

    }

    int ocean()

    {

    Bag B;

    string s=”MISSISSIPPI”;

    for(int j=0;j<s.length();j++)

    {

    B.add(s[j]);

    }

    B.display();

    B.modify();

    B.display();

    return 0;

    }

    /// use 4

    #include <iostream>

    #include <list>

    using namespace std;

    int ocean()

    {

    list<int> sums;

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

    numbers.push_back(rand()%21);

    int consideration[21];

    for(int i=0;i<21;i++)

    table[i] = 0;

    list<int>::iterator it;

    for(it=numbers.begin();it!=numbers.end();it++)

    {

    table[*it]++;

    }

    for(int i=0;i<21;i++)

    {

    cout<<i<<” “<<table[i]<<endl;

    }

    return 0;

    }