Skip to main content

Program in c++1

 #include <iostream>

using namespace std;


const int MAX_SIZE = 100;


class List {

private:

    int arr[MAX_SIZE];

    int size;

public:

    List() {

        size = 0;

    }


    void insert(int element) {

        if (size < MAX_SIZE) {

            arr[size++] = element;

            cout << "Element inserted" << endl;

        } else {

            cout << "List is full" << endl;

        }

    }


    void display() {

        if (size == 0) {

            cout << "List is empty" << endl;

        } else {

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

                cout << arr[i] << " ";

            }

            cout << endl;

        }

    }


    void remove(int element) {

        int index = -1;

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

            if (arr[i] == element) {

                index = i;

                break;

            }

        }

        if (index != -1) {

            for (int i = index; i < size - 1; ++i) {

                arr[i] = arr[i + 1];

            }

            size--;

            cout << "Element deleted" << endl;

        } else {

            cout << element << " not found" << endl;

        }

    }

};


int main() {

    List list;

    int choice, element;


    do {

        cout << "1. Insert" << endl;

        cout << "2. Display" << endl;

        cout << "3. Delete" << endl;

        cout << "4. Exit" << endl;

        cout << "Enter your choice: ";

        cin >> choice;


        switch (choice) {

            case 1:

                cout << "Element to insert: ";

                cin >> element;

                list.insert(element);

                break;

            case 2:

                list.display();

                break;

            case 3:

                cout << "Element to delete: ";

                cin >> element;

                list.remove(element);

                break;

            case 4:

                cout << "Exiting..." << endl;

                break;

            default:

                cout << "Invalid choice" << endl;

        }

    } while (choice != 4);


    return 0;

}


Comments