## C and C++

Moderators: None (Apply to moderate this forum)
Number of posts: 94611

This Forum Only

problem on stack Posted by cbrahmam on 2 Jun 2010 at 7:46 AM
hi,
i am a student of mca. i have a problem for my assignment. i need help from the esteemed members.

suppose the input to a stack is 1,2,3,4,5 in the order, i.e., 1 comes first,then 2 and so on. Which of the following rearrangements can be obtained in the output order? for those that can, explain how, giving the sequence of operations. for those outputs that cannot be so obtained, explaing why.
1) 1 2 4 5 3
2) 2 1 4 3 5
3) 2 1 5 3 4
4) 3 1 2 4 5

Re: problem on stack Posted by AsmGuru62 on 3 Jun 2010 at 3:34 AM
"Elements are removed from the stack in the reverse order to the order of their addition."

http://en.wikipedia.org/wiki/Stack_(data_structure)
Re: problem on stack Posted by cbrahmam on 3 Jun 2010 at 7:11 AM
thanks a lot
but my doubt is when we pop an element from the stack can we store it in a variable? if we can then all the four output order is possible. isnt it? when we pop an element its removed from the stack. can we re introduce the element to the same stack?
Re: problem on stack Posted by amit1487 on 19 Aug 2010 at 10:23 PM
Yes, friend
we can do it.....we can put all poped element again into the same stack..just create variable for each poped element n store...n as stack is empty push all those var. one by one into the stack...

have a nice weekend
Re: problem on stack Posted by donggo_cn on 11 Jun 2010 at 9:19 PM
1) and 2) are correct.

Re: problem on stack Posted by bilderbikkel on 21 Aug 2010 at 12:46 AM
: suppose the input to a stack is 1,2,3,4,5 in the order, i.e., 1
: comes first,then 2 and so on. Which of the following rearrangements
: can be obtained in the output order? for those that can, explain
: how, giving the sequence of operations. for those outputs that
: cannot be so obtained, explaing why.
: 1) 1 2 4 5 3
: 2) 2 1 4 3 5
: 3) 2 1 5 3 4
: 4) 3 1 2 4 5

Well, one can obtain any of these, when needed:

```//Creates the std::stack as described
const std::stack<int> s = CreateStack();

//Copy it to a std::vector
const std::vector<int> v(s);

//Shuffle it!
std::random_shuffle(v.begin(),v.end());
```

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic