Question

In: Computer Science

Create an unsorted LIST class. Each list should be able to store 100 names.

Create an unsorted LIST class. Each list should be able to store 100 names.

Solutions

Expert Solution

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


Related Solutions

Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed array of Object references. Assert that the passed array is not null. Next, implement: 1)Object get(int), which takes an int index and returns the Object at that index 2)void set(int, Object), which takes an int index and an object reference and sets that value at the index to the passed reference Both your...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
Create a table of the different classes of neurotransmitters . List names of neurotransmitters, their main...
Create a table of the different classes of neurotransmitters . List names of neurotransmitters, their main functions and implications in human disease.
Create a table of the different classes of neurotransmitters. List names of neurotransmitters, their implications in...
Create a table of the different classes of neurotransmitters. List names of neurotransmitters, their implications in human disease,  and their main functions .
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Create an app that stores a set of names. The UI should contain a EditText widget...
Create an app that stores a set of names. The UI should contain a EditText widget and two Buttons (one for adding a name and one for editing a name).   When the app starts up, the first name of the set is used to initialize the EditText widget. The app uses ViewPager so that the user can swipe left to visit the previous name in the set, or swipe right to visit the next name in the set, similar to...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT