Am having real problem understanding what the problem is. Here is the code :

Stack.h :

#ifndef STACK_H
#define STACK_H

typedef int Stack_entry;

class Stack
    enum Error_code{succes,overflow,underflow};

        int counter;

        //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();


        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(){


 /*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

    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
    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


     Stack_entry value;
     Node *p_next;

        Node(Stack_entry item);
        Node(Stack_entry item, Node *next);



Node.cpp :

#endif // NODE_H


#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 = 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

3 Years
Discussion Span
Last Post by tinstaafl

Firstly ... take a look at this in your 'main' function:

Stack outer_stack;

Stack inner_stack; // probably want this here ???

for( int i = 0; i < 100000; ++i )
    //Stack inner_stack; // really ???
    inner_stack.push(some_data); //WHAT is 'some_data' ... is it 'i' ? or some value depending in 'i' ?

    //inner_stack = outer_stack; //problem line !
    // ... do you really want to make a copy after each
    // new data item was added to inner_stack ? //

//maybe you want to make a COPY WHEN DONE ???
inner_stack = outer_stack;

I have NOT looked at the rest of your code ... but this, in main, looked very unusual!

Edited by David W


Just a quick perusal of the operator overload looks like you're assigning original_node to the next node before you're using its value. It seems to me that this would mean that you're skipping the first node

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.