Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help ASAP! C ++ please. Write the function reverse() in valid C++ code to

ID: 3672197 • Letter: P

Question

Please help ASAP! C ++ please.

Write the function reverse() in valid C++ code to reverse a linked list using a stack. You must use list iterator to traverse the linked list. Assume that the linked list is implemented by the class IntList with the following interface:

class IntList {

private:

struct Node {

int data;

Node* next;

};

Node* head;

unsigned int size;

public:

class iterator {

int& operator*() const;

iterator& operator++();

};

class const_iterator {

const int& operator*() const;

const_iterator& operator++();

};

// returns an iterator associated with the first element in the list iterator begin();

// returns an iterator associated with the last element in the list iterator end();

// returns the last element in the list int pop_back();

// returns the first element in the list int pop_front();

// adds x to the end of the list void push_back(const int& x);

// adds x to the beginning of the list void push_front(const int& x);

// returns the number of elements in the list unsigned int size();

};

I have other questions also, if you want to try to solve those, let me know and I will post the links to those as well.

Thank you

Explanation / Answer


#include <stack>

using namespace std;

class IntList {
private:
struct Node {
int data;
Node* next;
};
Node* head;
unsigned int count;
public:
class iterator {
   public:
int& operator*() const;
iterator& operator++();
   };
   class const_iterator {
   const int& operator*() const;
   const_iterator& operator++();
   };
   void reverse();
   iterator begin();
   iterator end();
   int pop_back();
   int pop_front();
   void push_back(const int& x);
   void push_front(const int& x);
   unsigned int size();
};

void IntList::reverse(){
   stack<int> s;
   int i = 0;
   for(iterator itr = begin(); i < size(); itr++){
       s.push(*itr);
       ++i;
   }
   head = NULL;
   while(!s.empty()){
       push_back(s.top());
       s.pop();
   }
}

int main(){
   IntList lst;

   stack<int> s;
  
   return 0;
}