0

So I understood how to print a single linked list in reverse order using recursion. I'm having trouble with doing it non member functions. For example in int print_reverse(IntSLList & list)) function how do you print reverse in an iterative way?

************************  .h  file **************************
class IntSLLNode {
public:
    IntSLLNode() {
        next = 0;
    }
    IntSLLNode(int el, IntSLLNode *ptr = 0) {
        info = el; next = ptr;
    }
    int info;
    IntSLLNode *next;
};

class IntSLList {
public:
    IntSLList() {
        head = 0;
    }
    ~IntSLList();
    int isEmpty() {
        return head == 0;
    }
    void addToHead(int);
    void addToTail(int);
    int  deleteFromHead(); // delete the head and return its info;
    int  deleteFromTail(); // delete the tail and return its info;
    bool isInList(int) const;
    void printAll() const;
    
private:
    IntSLLNode *head;
};

and here is the main

************************ main **************************
#include <iostream>
using namespace std;

#include "intSLList.h"

int print_reverse(IntSLList & list){


  if (head == NULL)  
     return;  
  printReverse(head->next); 

  cout << head->data << " ";  

 //How to compelete this in an iterative(or recursive if iterative is too much work)way ?
 //like this?       
}


int main() {

    IntSLList list;
    
    list.print_reverse(list);

}

Added the functions

ghf
  • 1
  • 1
  • Iterate forward and place each element in a stack. Print the contents of the stack by popping away until it's empty. – user4581301 Nov 02 '20 at 23:58
  • I assume you are not permitted to use a doubly linked list. If that is the case you have to put the elements into a container that you can iterate from the end to front. The recursive solution is simpler. – drescherjm Nov 02 '20 at 23:58
  • @drescherjm yes. I tried to do with recursion but kept running into errors thats why I'm trying in iterative but if its too unnecessarily complicated it would be great to see wiith recursion method – ghf Nov 03 '20 at 00:02
  • The usual problem with recursion is a long list will blow off the top of the program stack. But if you iterate and use your own stack... – user4581301 Nov 03 '20 at 00:04
  • @user4581301 it didn't work with a short one either :( – ghf Nov 03 '20 at 00:06
  • 1
    That said, if you post your recursive solution, odds are good that one of the smarty-pants we have running loose around here can show you how to fix it. It's almost always better to ask a question with an attempt, so long as it is a credible attempt, than it is to leave the function empty. – user4581301 Nov 03 '20 at 00:07
  • Side note: Your linked list class seems to be lacking a function to add items to the list. Makes it even harder to help you debug. – user4581301 Nov 03 '20 at 00:08
  • @user4581301 that is true my bad. I deleted my solution after getting too frustrated with it... kinda desperate won't do that again – ghf Nov 03 '20 at 00:10
  • Time to press undo on your editor. Note that we don't usually write code unless we see an attempt first. The rules tell us not to do so: [https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – drescherjm Nov 03 '20 at 00:13
  • What's wrong with recursion? Why can't we just make it a double-linked list if that's what we need? Within those limitations, the iterative way is just to put them all in a new container that meets the requirements (e.g. stack), then iterate in reverse. – Kenny Ostrom Nov 03 '20 at 00:13
  • Assuming print_reverse() is a free function the problem in `print_reverse` appears to be the `head` variable. Where does that come from? It should be list.head but you made that private. Also if print_reverse is a free function `list.print_reverse(list);` should be `print_reverse(list);` – drescherjm Nov 03 '20 at 01:18
  • `head = 0;` in `c++` we have nullptr since 2011. This should be `head = nullptr;` – drescherjm Nov 03 '20 at 01:23
  • @drescherjm the .h file is fixed so cant make it public – ghf Nov 03 '20 at 01:41
  • I don't think the problem is solvable then as far as my concerns about print_reverse – drescherjm Nov 03 '20 at 01:47
  • 1
    That's can't be true. The header posted here doesn't give any way to iterate forward either. Or do literally anything. – Kenny Ostrom Nov 03 '20 at 01:55
  • @KennyOstrom @drescherjm I added the functions I forgot they work correctly but there is nothing else other than that. But if the .h file was changeable all I would need to do is make `IntSLLNode *head;` public right? – ghf Nov 03 '20 at 02:10
  • Oh, I get it. It's going to be an awful hack, but posting ... nothing's implemented so I'm just going to write it with std::list, but using the interface provided by IntSLLNode. – Kenny Ostrom Nov 03 '20 at 02:24

1 Answers1

1

The header gives literally no way to access the contents of the list, other than by destroying it. So ... that's what we're going to do.

int  deleteFromTail(); // delete the tail and return its info;

Except we need to go the extra step and rebuild it, because nobody expects printing the container to destory its contents. See https://en.wikipedia.org/wiki/Principle_of_least_astonishment

#include <iostream>
#include <stack>
#include <list>

class IntSLList {
public:
    int isEmpty() { return m_list.empty(); }
    void addToHead(int i) { m_list.push_front(i); }
    void addToTail(int i) { m_list.push_back(i); }
    int  deleteFromHead() {
        int temp = m_list.front();
        m_list.pop_front();
        return temp;
    }
    int  deleteFromTail() { 
        int temp = m_list.back();
        m_list.pop_back();
        return temp;
    }

private:
    // no implementation given so I'm using std::list internally.
    std::list<int> m_list;
};

int print_reverse(IntSLList& mylist) {
    // store the data we are destroying in temp
    IntSLList temp;

    // literally the only way we can access the contents of the container is destructive so ... guess we're going there
    while (!mylist.isEmpty()) {
        int back = mylist.deleteFromTail();
        std::cout << back << std::endl;
        temp.addToHead(back);
    }

    // now rebuild the original list. I told you this would be bad.
    while (!temp.isEmpty()) {
        mylist.addToHead(temp.deleteFromTail());
    }

    // maybe this was supposed to be length, but not documented so I can return whatever I want.
    return -1;
}

int main() {
    IntSLList mylist;
    mylist.addToTail(1);
    mylist.addToTail(2);
    mylist.addToTail(3);
    print_reverse(mylist);
}

3
2
1

Kenny Ostrom
  • 4,598
  • 2
  • 16
  • 26
  • 1
    Notice that I used std::list to create a working example. Even if you're not allowed to use the standard library, always use the standard library. Then after you have working code, replace it with your own hand coded version (so you can deal with bugs in only one set of code at a time). – Kenny Ostrom Nov 03 '20 at 02:50