In: Computer Science
Create an unsorted LIST class. Each list should be able to store 100 names.
C++ code to implement an unsorted LIST class:
#include <iostream>
using namespace std;
class LIST {
private:
string names[100];
int size; // Number of elements in the list
int capacity; // Total capacity of the list
public:
LIST();
string get(int index);
void add(string name);
void print();
};
// Constructor to initialize variables
LIST::LIST() : capacity(100), size(0) {}
// Function to get a name at particular index
string LIST::get(int index) {
return names[index];
}
// Function to add a name
void LIST::add(string name) {
if (size >= capacity)
return; // Addition failed
names[size] = name;
size++;
}
// Function to print the list
void LIST::print() {
for (int i = 0; i < size; i++) {
if (i == size - 1)
cout << names[i] << endl;
else
cout << names[i] + ", ";
}
}
int main() {
LIST names;
names.add("John");
names.add("Jack");
names.add("Holly");
cout << "Name at index 2: " << names.get(2) << endl;
cout << "Names: ";
names.print();
return 0;
}
Output:

Kindly rate the answer and for any help just drop a comment