}
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;
}
Because I can see your node struct only got a single pointer, that means you will Definetly end up with a singly linked list,
Printing your list in reverse order will involve a very costly process, probably if you list contain n values, that might need n (iterations) , however coz this is really necessary for you it is still achievable.
void reverseprint (node*& list)
{
node* temp=list;
///on these temp iterate to the end of your list and print the value.
while (temp-> next!=NULL) //you must have labeled the last pointer to null else this will cause you a
{ ///bug
temp=temp-> next;
}///when we are here temp points to the last node, we will print the number here and also mark this
/// point with another temporal pointer.
cout <<temp->number<<" ";
node* current=temp;
temp= list;
while (temp!=list&¤t! =list)
{
while (temp-> next!=current)
{
temp=temp-> next;
}///we got to the node before current
cout <<temp-> number <<" ";
current=temp; //reset current to point to temp
temp=list; ///reset temp to begin again.
}
///when we get here our temp and current point to the first node. Print first node content
cout <<temp-> number <<endl;
}
That will give an idea, there are other methods you could achieve the same results like creating a local lisy and calling your reverse function, then printing it using the function printlist and others though, I dint debug these so be aware that it might contain some bugs anyway good luck.