Reverse a Stack using an empty Stack and a variable v -
i trying reverse stack using empty stack , variable v. not getting answer should get. can please me ?
void reverse() { s.push(1); s.push(2); s.push(3); int v; for(int i=1;i<s.size();i++) { v=s.top(); s.pop(); for(int j=0;j<s.size()-i;j++ ) { t.push(s.top()); s.pop(); } s.push(v); while(!t.empty()) { s.push(t.top()); t.pop(); } } }
stack last-in-first-out (lifo) non-generic collection of objects (see http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx ). thus, simplest way reverse stack content create stack, pop entries original , push new one, in following example (pertinent .net/c# implementation):
stack _stackoriginal = new stack(); _stackoriginal.push(1); _stackoriginal.push(2); _stackoriginal.push(3); stack _stackreverse = new stack(); while (_stackoriginal.count>0) { _stackreverse.push(_stackoriginal.pop()); }
there solution found on site (does stack<> constructor reverse stack when being initialized other one?), following:
stack<int> _stackreverse = new stack<int>(new stack<int>(_stackoriginal));
rgds,
Comments
Post a Comment