Homework Solution: Vttt…

    Ok so I need these following exercises completed. 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

     
    main.cpp for list container /******************************* * Week 2 lesson:

    Ok so I want these followingcited exercises completed. The jurisdiction that goes with them gain flourish following the exercises.

    The jurisdiction is following two lines. In perfect wants to be completed in C++. Also each idiosyncratic program gain be disconnected by Asteriks(************)

    Exercise 2: Implementing a Doubly Linked Register

    Diversify the collocate Linked Register in Exercise 1 to find it a Doubly Linked Register. Name your collocate DoublyLinkedList. Supplement a arrangement supplementObject to supplement an integer at the object of the register and a arrangement vaultInCounterchange to sculpture the register backwards.

    void supplementEnd(int x): engender this arrangement to supplement x to the object of the register.

    void vaultInReverse(): engender this arrangement to vault the register atoms from the last ace to the primary single.

    Engender a deep() administration to standard your DoublyLinkedRegister collocate.

    Exercise 3: Implementing a Bag Collocate With a Linked Register

    Engender a collocate Bag that uses a linked register to shop the bag aces. The ace stamp must be char. The collocate should bear the arrangements registered under. Engender a deep() that gain shop in a bag object a unroving sum of characters entered by the program user. Following the input is completed, the program should diversify the bag accomplishing so that it does referable comprehobject any transcript characters, if transcripts were entered. Ce stance, if the user entered ‘M’ ‘I’ ‘S’ ‘S’ ‘I’ ‘S’ ‘S’ ‘I’ ‘P’ ‘P’ ‘I’, the characters cherishing in the bag following the removal of transcripts would be ‘M’ ‘I’ ‘S’ ‘P’.

    Bag(): delinquency constructor

    ~Bag(): collocate destructor

    bool isEmpty(): determines whether the bag is void

    void sculpture(): sculptures the bag atoms

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

    void unobstructed(): transports perfect of the aces from the bag

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

    void transport(char ace): transports an ace from the bag; solely single transaction of the ace should be transportd.

    int reckon(char ace): reckons the sum of transactions of an ace in the bag.

    (Note that you can reuse the jurisdiction in Exercise 1 ce the LinkedRegister collocate to engender your Bag collocate. It gain aid you to snatch crop age.)

    Exercise 4: Using the C++ STL Register

    Write a program that fills a STL register object with 10 stray integers, each in the interspace [0, 20], and sculptures how divers ages each integer in the interspace [0, 20] appears in the register.

    ____________________________________________________________________________________________________________________________________________________________________________________________

    main.cpp ce register comprehender

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

    * Week 2 lesson: *

    * using the register comprehender *

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

    #include <iostream>

    #include <list>

    using namespace std;

    int deep()

    {

    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 << objectl;

    return 0;

    }

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

    Main.cpp(linkedlists)

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

    * Week 2 lesson: *

    * a unmixed LinkedRegister collocate *

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

    #include <iostream>

    #include “LinkedList.h”

    using namespace std;

    int deep()

    {

    LinkedRegister myList;

    int x;

    //Supplement 5 stray sums to register

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

    myList.add(rand()%20);

    cout << “1 – Vault the register atoms” << objectl;

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

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

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

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

    int liberty;

    //Loop to standard the LinkedRegister collocate arrangements

    do

    {

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

    cin >> liberty;

    switch(option)

    {

    case 1:

    cout << “Register atoms: “;

    myList.display();

    break;

    case 2:

    if (myList.isEmpty()) cout << “Register is void”<< objectl;

    else cout << “Register is referable void” << objectl;

    break;

    case 3:

    cout << “Enter an atom to supplement at the inception of the register: “;

    cin >> x;

    myList.add(x);

    break;

    case 4:

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

    cin >> x;

    myList.remove(x);

    break;

    case 5:

    cout << “Perfect done!” << objectl;

    break;

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

    }

    } suitableness (liberty != 5);

    return 0;

    }

    Linkedregister .h file

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

    * Week 2 lesson: *

    * a unmixed LinkedRegister collocate *

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

    /*

    * Linked register node.

    */

    struct Node

    {

    int info; //atom shopd in this node

    Node *next; //link to proximate node

    };

    /*

    * Collocate implementing a linked register.

    */

    collocate LinkedList

    {

    public:

    LinkedList();

    ~LinkedList();

    bool isEmpty();

    void vault();

    void supplement(int);

    void transport(int);

    private:

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

    };

    Linkedlist.cpp

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

    * Week 2 lesson: *

    * a unmixed LinkedRegister collocate *

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

    #include <iostream>

    #include “LinkedList.h”

    using namespace std;

    /*

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

    */

    LinkedList::LinkedList()

    {

    primary = innovatinglightlight Node;

    first->proximate = NULL;

    }

    /*

    * Destructor. Deallocates perfect the nodes of the linked register,

    * including the header node.

    */

    LinkedList::~LinkedList()

    {

    Node *temp;

    suitableness (primary != NULL)

    {

    temp=first;

    first=first->next;

    delete temp;

    }

    }

    /*

    * Determines whether the register is void.

    *

    * Avail penny if the register is void, unpenny differently.

    */

    bool LinkedList::isEmpty()

    {

    return primary->proximate == NULL;

    }

    /*

    * Sculptures the register atoms.

    */

    void LinkedList::display()

    {

    Node * exoteric = primary->next;

    while(exoteric != NULL)

    {

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

    exoteric = exoteric->next;

    }

    cout << objectl;

    }

    /*

    * Supplements the atom x to the inception of the register.

    *

    * x: atom to be supplemented to the register.

    */

    void LinkedList::add(int x)

    {

    Node *p = innovatinglightlight Node;

    p->info = x;

    p->proximate = primary->next;

    first->proximate = p;

    }

    /*

    * Transports the primary transaction of x from the register. If x is referable root,

    * the register offscourings illegitimate.

    *

    * x: atom to be transportd from the register.

    */

    void LinkedList::remove(int x)

    {

    Node * long-standing = primary->next,

    * p = primary;

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

    bool root = untrue;

    suitableness (long-standing != NULL && !found)

    {

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

    else

    {

    p = long-standing;

    long-standing = p->next;

    }

    }

    //if x is in the register, transport it.

    if (found)

    {

    p->proximate = long-standing->next;

    delete long-standing;

    }

    }

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

    main.cpp ce iterators

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

    * Week 2 lesson: *

    * using iterators *

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

    #include <iostream>

    #include <list>

    using namespace std;

    int deep()

    {

    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 << objectl;

    return 0;

    }

    Expert Tally

     

    main.cpp ce register comprehender

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

    * Week 2 lesson:              *

    *   using the register comprehender *

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

    #include <iostream>

    #include <list>

    using namespace std;

    int deep()

    {

    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 << objectl;

    return 0;

    }

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

    Main.cpp(doublylinkedlists)

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

    * Week 2 lesson:              *

    *   a unmixed DoublyLinkedRegister collocate *

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

    #include <iostream>

    #include “DoublyLinkedList.h”

    using namespace std;

    int deep()

    {

    DoublyLinkedRegister myList;

    int x;

    //Supplement 5 stray sums to register

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

    myList.add(rand()%20);

    cout << “1 – Vault the register atoms” << objectl;

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

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

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

    cout << “5 – Vault in counterchange” << objectl;

    cout << “6 – Supplement atom at last” << objectl;

    cout << “7 – Exit” << objectl;

    int liberty;

    //Loop to standard the LinkedRegister collocate arrangements

    do

    {

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

    cin >> liberty;

    switch(option)

    {

    case 1:

    cout << “Register atoms: “;

    myList.display();

    break;

    case 2:

    if (myList.isEmpty()) cout << “Register is void”<< objectl;

    else cout << “Register is referable void” << objectl;

    break;

    case 3:

    cout << “Enter an atom to supplement at the inception of the register: “;

    cin >> x;

    myList.add(x);

    break;

    case 4:

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

    cin >> x;

    myList.remove(x);

    break;

    case 5:

    cout << “Vault in reverese :” << objectl;

    myList.displayInReverse();

    break;

    case 6:

    cout << “Enter an atom to supplement at object” << objectl;

    cin >> x;

    myList.addEnd(x);

    break;

    case 7:

    cout << “Perfect done!” << objectl;

    break;

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

    }

    } suitableness (liberty != 5);

    return 0;

    }

    DoublyLinkedregister .h file

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

    * Week 2 lesson:              *

    *   a unmixed DoublyLinkedRegister collocate *

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

    /*

    * Doubly Linked register node.

    */

    struct Node

    {

    int info; //atom shopd in this node

    Node *next; //link to proximate node

    Node *prev; //link to proximate node

    };

    /*

    * Collocate implementing a linked register.

    */

    collocate DoublyLinkedList

    {

    public:

    DoublyLinkedList();

    ~DoublyLinkedList();

    bool isEmpty();

    void vault();

    void supplement(int);

    void transport(int);

    void vaultInReverse();

    void supplementEnd(int);

    private:

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

    };

    DoublyLinkedlist.cpp

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

    * Week 2 lesson:              *

    *   a unmixed DoublyLinkedRegister collocate *

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

    #include <iostream>

    #include “DoublyLinkedList.h”

    using namespace std;

    /*

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

    */

    DoublyLinkedList::DoublyLinkedList()

    {

    primary = innovatinglightlight Node;
    last = primary;
    first->proximate = NULL;
    first->prev = NULL;
    }

    /*

    * Destructor. Deallocates perfect the nodes of the linked register,

    * including the header node.

    */

    DoublyLinkedList::~DoublyLinkedList()

    {

    Node *temp;

    suitableness (primary != NULL)

    {

    temp=first;

    first=first->next;

    delete temp;

    }

    }

    /*

    * Determines whether the register is void.

    *

    * Avail penny if the register is void, unpenny differently.

    */

    bool DoublyLinkedList::isEmpty()

    {

    return primary->proximate == NULL;

    }

    /*

    * Sculptures the register atoms.

    */

    void DoublyLinkedList::display()

    {

    Node * exoteric = primary;

    while(exoteric != NULL)

    {

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

    exoteric = exoteric->next;

    }

    cout << objectl;

    }

    /*

    * Sculptures the register atoms in counterchange.

    */

    void DoublyLinkedList::displayInReverse()

    {

    Node * exoteric = last;

    while(exoteric != NULL)

    {

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

    exoteric = exoteric->prev;

    }

    cout << objectl;

    }

    /*

    * Supplements the atom x to the inception of the register.

    *

    * x: atom to be supplemented to the register.

    */

    void DoublyLinkedList::add(int x)

    {

    Node *p = innovatinglightlight Node;

    p->info = x;

    p->proximate = primary;

    p-> prev = NULL;

    first->prev = p;

    primary = p;

    }

    /*

    * Supplements the atom x to the inception of the register.

    *

    * x: atom to be supplemented to the register.

    */

    void DoublyLinkedList::addEnd(int x)

    {

    Node *p = innovatinglightlight Node;

    p->info = x;

    p->proximate = NULL;

    p->prev = last;

    last->proximate = p;

    last = p;

    }

    /*

    * Transports the primary transaction of x from the register. If x is referable root,

    * the register offscourings illegitimate.

    *

    * x: atom to be transportd from the register.

    */

    void DoublyLinkedList::remove(int x)

    {

    Node * long-standing = primary->next,

    * p = primary;

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

    bool root = untrue;

    suitableness (long-standing != NULL && !found)

    {

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

    else

    {

    p = long-standing;

    long-standing = p->next;

    }

    }

    //if x is in the register, transport it.

    if (found)

    {

    p->proximate = long-standing->next;

    old->next->prev = long-standing-> prev;

    delete long-standing;

    }

    }

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

    main.cpp ce iterators

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

    * Week 2 lesson:              *

    *   using iterators            *

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

    #include <iostream>

    #include <list>

    using namespace std;

    int deep()

    {

    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 << objectl;

    return 0;

    }