Question: trying to get my program to run and print a link list backwards. i know this is only part fo the ptogram chegg only lets
trying to get my program to run and print a link list backwards. i know this is only part fo the ptogram chegg only lets you put so much. please halp me print the link list backwards.
C++
#include
using namespace std; struct node { int data; node *next; };
class linklist { private: node *head, *tail;
public:
void printlist();
void reverse();
void linklist::printlist() { node *temp = new node; temp = head; if (head == NULL) { cout << "List is empty." << endl; } while (temp != NULL) { cout << temp->data; cout << "" << endl; temp = temp->next; } }
void linklist::reverse() { node *curr = head; node *prev = NULL, *next = NULL;
while (curr != NULL) { next = curr->next; curr->next = prev; prev = curr; curr = next; } head = prev; }
int main() {
linklist ll;
cout << "Reversing listing: " << endl; ll.reverse(); ll.printlist(); cout << endl; break;
system("pause"); return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
