help with linked list

i need help for the reverseprint function i dont know how to write it. Any help will b appraciated thanks




#include<cstdlib>
#include < iostream>
using namespace std;

struct node
{
int number;
node*next;

};
bool empty(node*head);
char menu();
void firstelement(node*&head, node*&last, int number);
void insert(node*&head, node*&last, int number);
void remove(node*&head, node*&last, int number);
void printlist(node*current);
//void reverse(node*&current);

bool isempty(node*head)
{
if (head == NULL)
return true;
else
return false;

}
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*&current)
{
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;
}
Last edited on
closed account (SECMoG1T)
What is that function supposed to do .
print the numbers of the linked list in order and reverse order i've already done the order way i just need help for the reverse order function
Topic archived. No new replies allowed.