Use C++ Convert your Set class in Lab #23 to the template-based class. That is, the template-based set must be able to store data of any data type, not just integers. Write the main function to test your class by creating different sets of different data types. main.cpp: #include #include "Set.h" using namespace std; int main() { Set s1; Set s2; s1.insert(1); s1.insert(2); s2.insert(2); s2.insert(3); s2.insert(4); Set result = s1 + s2; result.show(); s1 += s2; s1.show(); s1 = s1; s1.show(); cout << s1.getSize() << endl; cout << s1.getCapacity() << endl; s1.remove(2); s1.show(); s1.remove(5); s1.show(); return 0; } Set.h: #ifndef SET_H #define SET_H #include "Set.h" Set::Set() { size = 0; capacity = 10; items = new int[capacity]; } Set::Set(int cap) { size = 0; capacity = cap; items = new int[capacity]; } Set::Set(const Set &source) { size = source.size; capacity = source.capacity; items = new int[capacity]; for (int i = 0; i < size; i++) { items[i] = source.items[i]; } } Set::~Set() { delete[] items; } void Set::insert(int item) { if (size == capacity) { cout << "Set is full" << endl; } else { bool flag = false; for (int i = 0; i < size; i++) { if (items[i] == item) { flag = true; } } if (flag == true) { cout << "Duplicate data" << endl; } else { items[size] = item; size++; } } } void Set::remove(int item) { bool flag = false; int j; for (int i = 0; i < size; i++) { if (items[i] == item) { flag = true; j = i; } } if (flag == true) { for (int i = j; i < size - 1; i++) { items[i] = items[i + 1]; } size--; } else { cout << "No such data" << endl; } } bool Set::full() { if (size == capacity) { return true; } else { return false; } } bool Set::exist(int item) const { for (int i = 0; i < size; i++) { if (items[i] == item) { return true; } } return false; } void Set::operator+=(const Set &source) { for (int i = 0; i < source.size; i++) { bool flag = false; for (int j = 0; j < size; j++) { if (source.items[i] == items[j]) { flag = true; } } if (flag == false) { items[size] = source.items[i]; size++; } } } void Set::operator=(const Set &source) { if (this == &source) { return; } delete[] items; size = source.size; capacity = source.capacity; items = new int[capacity]; for (int i = 0; i < size; i++) { items[i] = source.items[i]; } } void Set::show() const { for (int i = 0; i < size; i++) { cout << items[i] << " "; } cout << endl; } int Set::getSize() const { return size; } int Set::getCapacity() const { return capacity; } Set operator+(const Set &s1, const Set &s2) { Set result; for (int i = 0; i < s1.size; i++) { result.insert(s1.items[i]); } for (int i = 0; i < s2.size; i++) { result.insert(s2.items[i]); } return result; } #endif
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
Use C++
Convert your Set class in Lab #23 to the template-based class. That is, the template-based set must be able to store data of any data type, not just integers. Write the main function to test your class by creating different sets of different data types.
main.cpp:
#include <iostream>
#include "Set.h"
using namespace std;
int main() {
Set s1;
Set s2;
s1.insert(1);
s1.insert(2);
s2.insert(2);
s2.insert(3);
s2.insert(4);
Set result = s1 + s2;
result.show();
s1 += s2;
s1.show();
s1 = s1;
s1.show();
cout << s1.getSize() << endl;
cout << s1.getCapacity() << endl;
s1.remove(2);
s1.show();
s1.remove(5);
s1.show();
return 0;
}
Set.h:
#ifndef SET_H
#define SET_H
#include "Set.h"
Set::Set() {
size = 0;
capacity = 10;
items = new int[capacity];
}
Set::Set(int cap)
{
size = 0;
capacity = cap;
items = new int[capacity];
}
Set::Set(const Set &source)
{
size = source.size;
capacity = source.capacity;
items = new int[capacity];
for (int i = 0; i < size; i++)
{
items[i] = source.items[i];
}
}
Set::~Set()
{
delete[] items;
}
void Set::insert(int item)
{
if (size == capacity)
{
cout << "Set is full" << endl;
}
else
{
bool flag = false;
for (int i = 0; i < size; i++)
{
if (items[i] == item)
{
flag = true;
}
}
if (flag == true)
{
cout << "Duplicate data" << endl;
}
else
{
items[size] = item;
size++;
}
}
}
void Set::remove(int item)
{
bool flag = false;
int j;
for (int i = 0; i < size; i++)
{
if (items[i] == item)
{
flag = true;
j = i;
}
}
if (flag == true)
{
for (int i = j; i < size - 1; i++)
{
items[i] = items[i + 1];
}
size--;
}
else
{
cout << "No such data" << endl;
}
}
bool Set::full() {
if (size == capacity)
{
return true;
}
else
{
return false;
}
}
bool Set::exist(int item) const
{
for (int i = 0; i < size; i++)
{
if (items[i] == item)
{
return true;
}
}
return false;
}
void Set::operator+=(const Set &source)
{
for (int i = 0; i < source.size; i++)
{
bool flag = false;
for (int j = 0; j < size; j++)
{
if (source.items[i] == items[j])
{
flag = true;
}
}
if (flag == false)
{
items[size] = source.items[i];
size++;
}
}
}
void Set::operator=(const Set &source)
{
if (this == &source)
{
return;
}
delete[] items;
size = source.size;
capacity = source.capacity;
items = new int[capacity];
for (int i = 0; i < size; i++)
{
items[i] = source.items[i];
}
}
void Set::show() const {
for (int i = 0; i < size; i++)
{
cout << items[i] << " ";
}
cout << endl;
}
int Set::getSize() const
{
return size;
}
int Set::getCapacity() const
{
return capacity;
}
Set operator+(const Set &s1, const Set &s2)
{
Set result;
for (int i = 0; i < s1.size; i++)
{
result.insert(s1.items[i]);
}
for (int i = 0; i < s2.size; i++)
{
result.insert(s2.items[i]);
}
return result;
}
#endif
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images