Click here to Skip to main content
15,665,276 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
class TransactionalStack
{
private:
    stack<TransactionalStack*> transactions;
    stack<int> content;

public:
    TransactionalStack() : TransactionalStack(false)
    {}

    TransactionalStack(bool internal)
    {

        if(!internal)
        {
            transactions.push(this);
//            TransactionalStack* top = transactions.top();
//            top->content.push(5);
        }
    }

    void push(int value)
    {
        TransactionalStack* top = transactions.top();
        top->content.push(value);
    }

    int top()
    {
        stack<int> cn ;
        cn = transactions.top()->content;
        return cn.empty() ? 0 : cn.top();
    }

    int pop()
    {
        stack<int> cn;
        cn = transactions.top()->content;
        if(cn.empty())
            return 0;
        int t = cn.top();
        cn.pop();
        return t;
    }

    /*void begin()
    {
        TransactionalStack* newTransaction = new TransactionalStack();
        newTransaction->content = content; // assignment operator automatically copies
        transactions.push(newTransaction);
    }*/

    bool rollback()
    {
        TransactionalStack* lastTransaction = transactions.top();
        if(lastTransaction != this)
        {
//            lastTransaction = transactions.top();
            transactions.pop();
//            transactions.top()->content = lastTransaction->content;
            return true;
        }
        return false;
    }

    bool commit()
    {
        TransactionalStack* lastTransaction = transactions.top();
        if(lastTransaction != this)
        {
            lastTransaction = transactions.top();
            transactions.pop();
            transactions.top()->content = lastTransaction->content;
            return true;
        }
        return false;
    }
};

int main()
{
    TransactionalStack* ts = new TransactionalStack();
    /*ts->push(4);
    ts->begin();
    ts->push(7);
    ts->begin();
    ts->push(2);
    assert(ts->rollback() == true);
    assert(ts->top() == 7);
    ts->begin();
    ts->push(10);
    assert(ts->commit() == true);
    assert(ts->top() == 10);
    assert(ts->rollback() == true);
    assert(ts->top() ==4);
    assert(ts->commit() == false);
    return 0;*/


    ts->push(5);
    ts->push(2); // stack: [5,2]
    assert(ts->top() == 2);
    ts->pop(); // stack: [5]
    assert(ts->top() == 5);

    TransactionalStack* ts1 = new TransactionalStack();
    assert(ts1->top() == 0); // top of an empty stack is 0
    ts1->pop();
}


What I have tried:

Debugging fails for test case
ts->push(5);
    ts->push(2); // stack: [5,2]
    assert(ts->top() == 2);
    ts->pop(); // stack: [5]
    assert(ts->top() == 5);

    TransactionalStack* ts1 = new TransactionalStack();
    assert(ts1->top() == 0); // top of an empty stack is 0
    ts1->pop();
Posted
Updated 3-Mar-21 16:42pm

1 solution

I think I see your problem. It has to do with your methods :
C++
int top()
{
    stack<int> cn ;          // <----------------------
    cn = transactions.top()->content;
    return cn.empty() ? 0 : cn.top();
}

int pop()
{
    stack<int> cn;           // <----------------------
    cn = transactions.top()->content;
    if(cn.empty())
        return 0;
    int t = cn.top();
    cn.pop();
    return t;
}
The problem has to do with what the comments point at. You are creating a copy of the content stack in both of those. That is not a good idea at all. Plus, in the case of pop, you are popping from the copy, not your actual stack. It would be better to make that a reference so the operations are performed on the actual members of the object and not copies. Here is how that could look using references.
C++
using stackint = std::stack< int >;

int top()
{
    stackint & cn = transactions.top()->content;
    return cn.empty() ? 0 : cn.top();
}

int pop()
{
    stackint & cn = transactions.top()->content;
    if(cn.empty())
        return 0;
    int t = cn.top();
    cn.pop();
    return t;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900