In: Computer Science
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 () ;
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