Question

In: Computer Science

C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove,...

C++ PROGRAMMING

In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove, and list. This assignment is to extend the functionalities of the bag with other operations average, min, and max, You need to extend the Bag class (under Wk 2, BagLinked_List.cpp) with the following methods:

-int Bag::min( ), is to find minimum of items of the bag.

-int Bag::max( ), is to find maximum of items of the bag

-float Bag::ave( ), is to find average of the items of the bag.

After expanding the Bag class with these functions, demonstrate their functionalities with a similar to:

Create a bag A with the items 8, 4, 5, 6, 1, 3; and create another bag, B, with the items 4, 6, 9, 2.

Then show that

cout << A.min() << “, ” << A.max() << “, ” << A.ave () ;

cout << B.min() << “, ” << B.max() << “, ” << B.ave () ;

Solutions

Expert Solution

Hi. I have answered this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include <iostream>

#include <stdio.h>

#include <string>

#include <stdlib.h>

using namespace std;

// A struct to keep items, modified to store int values other than string

struct item

    {

    int data;

    item* next;

    item(int x, item* t)

    { // constructor

        data = x;

        next = t;

    }

};

typedef item* link;

// A class to represent Bag, which can hold integers in an linked list

// The items (int) are not sorted in the list.

class Bag

    {

public:

    Bag(); // Construction

    bool add(int num); // Adds a new item to bag

    bool has(int num); // Check if Bag has this item

    bool remove(int num); // Delete the num from the bag

    unsigned short getCapacity(); // get the capacity of the Bag. Is there any capacity

    void ListBag(); // List all items of Bag.

    unsigned short getSize(); // How many items the Bag has

    //new methods

    int min();

    int max();

    float ave();

private:

    link first; // A pointer to show the first node

    link last; // A pointer to show the last node

    unsigned short size; // how many items Bag has

};

Bag::Bag() // Initialize an instance of the class

{

    size = 0;

    first = last = NULL; // At the beginning both, first and last are null

    cout << "A new bag with " << getCapacity() << " was created...!" << endl;

}

bool Bag::add(int num)

{

    link AddItem = new item(num, NULL); // dynamically create a new struct/item

    if (AddItem == NULL)

        return false; // not enought memory

    if (first == NULL)

        first = AddItem; // first item to be added

    else

        last->next = AddItem; // add to the end of array

    last = AddItem;

    size++;

    return true; // Yeah, added.

}

bool Bag::remove(int num) // Delete the num from the bag

{

    link back;

    link front;

    if (first == NULL)

        return false;

    back = front = first;

    if (front->data == num) // delete this

    {

        if (first == last) // there is only one item, which will be deleted

            first = last = NULL;

        else

            first = first->next; // first of many items is being deleted

        delete front;

        size--;

        return true;

    }

    front = front->next; // if reach this line, the first item of bag is not being deleted.

    while (front != NULL)

    {

        if (front->data == num)

        {

            back->next = front->next; // a mid-item is being deleted. By-pass the (front)

            if (front == last)

                last = back; // the last item is being deleted

            delete front;

            size--;

            return true;

        }

        back = front;

        front = front->next;

    }

    return false; // if reach this line, item is not found in the list.

}

void Bag::ListBag() // List, print out, all items in the arrays, // make 5 coloumns

{

    cout << size << ": ";

    link it = first; // it means iterator, visit all items one by one.

    int i = 0; // i is used to format the outpur

    while (it != NULL) {

        cout << it->data << " ";

        it = it->next;

        if ((i + 1) % 5 == 0)

            cout << endl;

        i++;

    }

    cout << endl;

}

// How many items the Bag has

unsigned short Bag::getSize()

{

    return size;

}

bool Bag::has(int num) // Check if the bag has item "num", // You can return bool too

{

    link it = first;

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

    {

        if (it->data == num)

            return true;

        it = it->next;

    }

    return false;

}

unsigned short Bag::getCapacity()

{

    return 10000; // A made-up number.

}

//implementations of new methods

int Bag::min(){

                int m=0;

                //taking reference to first link

                link it = first;

                //looping for size times

    for (int i = 0; i < size; i++){

                //if this is first number or if current number < m,

                //setting current number as m

                if(i==0 || it->data<m){

                                m=it->data;

                                }

                                //moving to next link

                                it=it->next;

                }

                return m;

}

int Bag::max(){

                int m=0;

                link it = first;

    for (int i = 0; i < size; i++){

                //if this is first number or if current number > m,

                //setting current number as m

                if(i==0 || it->data>m){

                                m=it->data;

                                }

                                it=it->next;

                }

                return m;

}

float Bag::ave(){

                //initializing sum to 0

                int sum=0;

                link it = first;

                //looping and summing integers of each node

    for (int i = 0; i < size; i++){

                sum+=it->data;

                                it=it->next;

                }

                float avg=0;

                //finding average if size>0 to prevent division by zero

                if(size>0){

                                avg=(float)sum/size;

                }

                return avg;

}

int main(int argc, const char* argv[])

{

                //creating two Bags A and B, adding some numbers as per the question

    Bag A;

    A.add(8);

    A.add(4);

    A.add(5);

    A.add(6);

    A.add(1);

    A.add(3);

   

    Bag B;

    B.add(4);

    B.add(6);

    B.add(9);

    B.add(2);

   

    //displaying min, max and ave of both bags

    cout<<A.min()<<", "<<A.max()<<", "<<A.ave()<<endl; //1, 8, 4.5

    cout<<B.min()<<", "<<B.max()<<", "<<B.ave()<<endl; //2, 9, 5.25

    return 0;

}

/*OUTPUT*/

A new bag with 10000 was created...!

A new bag with 10000 was created...!

1, 8, 4.5

2, 9, 5.25


Related Solutions

Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made with the fundamental functionalities: Visit - Description: It will display each of the data stored in the BST depending on the input parameter:Preorder Inorder Bidder Level by level Input - An integer (1-4) Exit - Nothing Precondition - A valid BST Postcondition - Nothing Height - Description:Will return the height of the BST Entry - Nothing Output - An integer with which to indicate...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in an array. NOT in linked List. Implement an array-based Linked List in your language. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from the input and insert them into the linked list. • If an input integer is 0, the program should print all the integers in the list(from tail to head) and then exit.
C++ Need to add the following functions to my templatized class linked list. (main is already...
C++ Need to add the following functions to my templatized class linked list. (main is already set to accommodate the functions below) --void destroy_list () deletes each node in the list, and resets the header to nullptr --bool search_list (key value) searches the list for a node with the given key. Returns true if found, false if not. --bool delete_node (key value) deletes the node which contains the given key. If there is more than one node with the same...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std; You must use this prototype: int remove_every_other(node *&rear)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT