i m just creating my on stack using linked list,but when i print elements using recursion it exit with random code in geany.
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
class Stack
{
public:
Node* Top;
void stack()
{
Top = NULL;
}
void pop()
{
if(Top == NULL)
{
cout<<"\nstack empty";
}
Top = Top->next;
}
void push(int data)
{
Node* newNode = new Node();
newNode->data = data;
newNode->next = Top;
Top = newNode;
}
bool isEmpty()
{
if(Top == NULL)
{
return true;
}
else
{
return false;
}
}
void print()
{
tem_print(Top);
}
void tem_print(Node* t_top)
{
if(t_top == NULL)
{
return;
}
tem_print(t_top->next);
cout<<t_top->data;
}
};
int main()
{
Stack s;
s.push(1);
s.push(2);
s.print();
return 0;
}
i used a print() function inside it i called another temp print function which is used to print elements,It works fine if i used iteration method to print
output: freeze of 3sec
and prints this
(program exited with code: -1073741819)
Press any key to continue . . .
Nodewithnextset toNULL, so your condition can never be truevoid stack()to be the constructorStack()insteadTopis never NULL in your code because you never set it to be so