}
char menu() // function to output the menu and ask the user to chose what to do
{
char choice;
cout << "Menu\n";
cout << "1. add an item.\n";
cout << "2. Remove an item.\n";
cout << "3. show the list in order.\n";
cout << "4. show the list in reverse order. \n";
cout << "5. Exit.\n";
cin >> choice;
return choice;
}
void firstelement(node*&head, node*&last, int number)// fuction to insert the first element of the list
{
node*temp = new node;
temp->number = number;
temp->next = NULL;
head = temp;
last = temp;
}
void insert(node*&head, node*&last, int number)// function to add an element in the list
{
if (isempty(head))
firstelement(head, last, number);
else
{
node*temp = new node;
temp->number = number;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void remove(node*& head, node*& last, int number)// function to remove an element in the list
{
if (isempty(head))
cout << "the list is already empty.\n";
else if (head == last)
{
delete head;
head == NULL;
last == NULL;
}
else
{
node*temp = head;
head = head->next;
delete temp;
}
}
void printlist(node*current) // function to print the list forward
{
if (isempty(current))
cout << "the list is empty\n";
else
{
cout << " the list contains: \n";
while (current != NULL)
{
cout << current->number << endl; // print the numbers of the list
current = current->next; // moving to the next value
}
}
}
void reverse(node*¤t)
{
if (isempty(current))
cout << "the list is empty\n";
else
{
cout << " the list contains: \n";
while (current != NULL)
{
cout << current->number << endl;
current = current->next;
}
}
}
int main()// main function
{
node*head = NULL; // pointers initialised to 0 because there is nothing in the list
node*last = NULL;
char choice;
int number; // declarations
do {
choice = menu(); // function call
switch (choice)
{
case '1': cout << "enter a number: ";// ask the user to enter a number to be added at the list
cin >> number;
insert(head, last, number);//function call
break;
case '2': remove(head, last,number);//function call
break;
case'3': printlist(head); // function call
break;
case'4': reverse(last);
default: cout << "system exit\n"; // exit the program
}
} while (choice != '5');
return 0;
}