So the task is to implement stack using single linked list. But without defining the actual node(the only private member: Single_List list and public functions). Single_List were implemented by me before and have private head pointer(requirement of teacher). So I made all of the reqiered functions exept for int& top() that returns pointer to the data of head.
Here is header files for list and stack
While not efficient, you can do it this way (while keeping your list code the same) is to just use your size() function in your list class as an iterator, then perform loop operations in your stack class to get to the head of your list class.
If Stack::top() needs to return a reference then you'll have to change something in Single_List(). As booradley60 suggested, I think you should change get_nth() to return a reference.
Also, why does Stack contain a pointer to Single_List? It seems easier for it to contain an instance of Single_List instead. Otehrwise you have to worry about memory management.
Because you're creating a new integer on the heap and assigning a value to it. You're changing nothing about the stack itself.
However, what's wrong with keskiverto's suggestion? If you want to replace the value on top of the stack, pop the stack then push the new value. That's the interface that the stack exposes. It isn't supposed to let you fiddle with the internals directly.
I don't think it's possible to implement int& Stack::top() if you are using Single_List as the implementation. You must change Single_List for this to work. All you have to do is change get_nth() to return a reference instead of a value.