I’ve been tasked with creating a print function that uses recursion to print the data of a singly linked list (stack). Here is my code thus far:
IntStack.h
#include <iostream>
struct NodeType
{
int data;
NodeType *next;
};
class IntStack
{
private:
NodeType root;
int count;
public:
IntStack(void);
~IntStack(void);
void push(int);
int pop(void);
bool isEmpty(void);
void print(NodeType&);
int getSize() const;
NodeType* getRoot();
};
IntStack.cpp
#include "IntStack.h"
IntStack::IntStack()
{
count = 0;
}
IntStack::~IntStack()
{
}
void IntStack::push(int num)
{
NodeType newNode;
newNode.data = num;
newNode.next = &root;
root = newNode;
++count;
}
int IntStack::pop(void)
{
// get root data
// set root equal to root.next
int num = root.data;
root = *root.next;
--count;
return num;
}
bool IntStack::isEmpty(void)
{
return (count == 0);
}
void print(NodeType *node)
{
if (node->next != NULL) {
std::cout << node->data << " " << std::endl;
print(node->next);
}
}
NodeType* IntStack::getRoot()
{
return &root;
}
int IntStack::getSize() const
{
return count;
}
main.cpp
#include <iostream>
#include "IntStack.h"
int main()
{
IntStack stack;
stack.push(7);
stack.push(10);
stack.push(13);
stack.push(43);
stack.push(23);
stack.push(5);
stack.push(32);
stack.push(8);
std::cout << stack.getSize() << " item(s) in the stack." << std::endl;
std::cout << "Pop item off stack: " << stack.pop() << std::endl;
std::cout << stack.getSize() << " item(s) in the stack." << std::endl;
stack.print(stack.getRoot());
return 0;
}
I’m receiving an error on the stack.print(stack.getRoot()) function in main.cpp:
main.cpp:28:17: Non-const lvalue reference to type ‘NodeType’ cannot bind to a temporary of type ‘NodeType *’
Obviously, I’m not sending a pointer to the function, but I’ve tried various ways to send the root node with no luck. Any information on how I should proceed is greatly appreciated. Thanks
Firstly, you should be allocating your nodes on the Heap not on the Stack. In short for something to be Heap allocated rather than Stack allocated, you have to use the “new” keyword.
Reason being, anything allocated to the stack expires(is deleted) when the function returns. In your case, you need to keep your nodes around beyond the end of the function.
so change
NodeType root;toNodeType *root;and set root to NULL in the constructor.You will have to alter your push function, among others, to get your program to work now.
to delete a node, you would do the reverse
Adding a constructor to NodeType to set the variable next to NULL would also be a good idea, becausewhen it comes to recursively printing the nodes, you will have to know when you have reached the last node, which will be indicated by the “next” pointer being NULL.edit:
I crossed out a line above. Since you initialize root to NULL, and
nextalways gets set to the value of root,NodeTypeshouldn’t need a constructor. But, the condition in your print function should beif (node != NULL)instead ofif (node->next != NULL). Consider the case where you have 0 or 1 node.