#include <list> // std::list<>
#include <iostream> // std::cout, std::cin, std::endl
int main()
{
// Create a linked list
std::list<int> lst;
// Prompt an user to insert any number of elements
int element, choice;
do
{
// The user must input only integers, otherwise this code doesn't work
std::cout << "Do you want to insert a new element? 0 - No, 1 - Yes: ";
std::cin >> choice;
while (choice != 0 && choice != 1)
{
std::cout << "Invalid input, try once more: ";
std::cin >> choice;
}
if (choice == 1)
{
std::cout << "Enter an element: ";
std::cin >> element;
lst.push_back(element);
}
} while (choice != 0);
// Display the number of elements that the user will have decided to create
std::cout << lst.size() << std::endl;
}
Comments
Leave a comment