Am having real problem understanding what the problem is. Here is the code :
Stack.h :
#ifndef STACK_H
#define STACK_H
#include<Node.h>
typedef int Stack_entry;
class Stack
{
public:
enum Error_code{succes,overflow,underflow};
private:
int counter;
public:
Stack();
~Stack();
//Stack& operator=(const Stack &Stack_copy);
bool empty_stack()const;
Error_code push(const Stack_entry &item);
Error_code pop();
Error_code top(Stack_entry &item)const;
int size_of_stack();
protected:
Node *top_node; // upucuje na prvi elemenat u LL-u. Ono na sto on upucuje se pop-a ili pusha prvo
};
#endif // STACK_H
Stack.cpp :
#include "Stack.h"
#include <cstddef>
using namespace std;
Stack::Stack() // kreiramo prazan stack
{
top_node = NULL;
}
Stack ::~Stack(){
while(!empty_stack())
pop();
}
/*void Stack::operator=(const Stack &Stack_copy){
Node *new_top,*new_copy, *original_node = Stack_copy.top_node; // kreiramo novi top, novu kopiju i orginalni node
if(original_node == NULL) new_top = NULL; // ako je objekat prazan, novi top je prazan
else{
new_copy = new_top = new Node(original_node->value); // ??
while(original_node->p_next != NULL){
original_node = original_node->p_next;
new_copy->p_next = new Node(original_node->value); // kopiramo podatke u kopiju
new_copy = new_copy->p_next;
}
}
while(!empty_stack()) // obrisi stare entrije
pop();
top_node = new_top; // i zamjeni sa novim
}*/
Stack::Error_code Stack::push(const Stack_entry &item) {
Node *new_element = new Node(item,top_node); // kreiramo novi node, novi node upucuje tamo gdje je top_node upucivao
if(new_element == NULL) return overflow; // error check, blaah
top_node = new_element; // a top node upucuje na novi node
return succes;
}
Stack::Error_code Stack::pop(){
Node *old_top = top_node; // kreiramo novu referencu za top LL-a da se nebi izgubio pocetak LL-a
if(top_node == NULL) return underflow; // error check..
top_node = old_top->p_next; // top node sada upucuje na node koji se nalazi iza prvog u listi
delete old_top; // uklanjamo prvi node u listi
return succes;
}
int Stack::size_of_stack(){
Node.h :
//typedef int Stack_entry;
class Node
`{`
public:
Stack_entry value;
Node *p_next;
public:
Node();
Node(Stack_entry item);
Node(Stack_entry item, Node *next);
protected:
};
Node.cpp :
#endif // NODE_H
Node.cpp
#include "Node.h"
#include <cstddef>
using namespace std;
Node::Node() // default constructor
{
p_next = NULL;
}
Node::Node(Stack_entry item){ // korisnik daje samo vrijednost, p_next je NULL
value = item;
p_next = NULL;
}
Node::Node(Stack_entry item, Node *next){ // korisnik daje i value i gdje ce taj objekat upcivat nakon kreacije
value = item;
p_next = next;
}
main :
Stack outer_stack;
for(int i = 0;i<100000;i++){
Stack inner_stack;
inner_stack.push(some_data);
inner_stack = outer_stack;} // problem line !
Now, I get that I need to wright AO, because in case like this, inner_stack.top_node will be NULL, so data in inner_stack will become garbage, also destructor will always be called on outer_stack even thought its not out of scope. Take a look at the operator overload function and if you could explain to me what is does difrently from the default asignment in main. Thanks