Sunday 15 June 2014

c++ - how to reverse a stack -


I have an assignment where I anticipate taking a stack, show the output and then reverse it to show the output.

Looks Like

  Stack: 262 115 74 26 34 243 22 734 113 121 Stack Reversal: 121 113 734 22 243 34 26 74 115 262  Instead of my coming like this   
  Stack: 262 115 74 26 34 243 22 734 113 121 121 113 734 22 243 34 26 74 115 262 Stack Inverted: Can anyone see my code and see what's going on. I have tried a lot, but nothing can be found to work  
  #include & lt; Stdio.h & gt; # Include & lt; Iostream & gt; #include "linkedStack.h" using namespace std; Template & lt; Class type & gt; Zero Print Stacks (Linked Stack Type & Lt; Type & gt; Stack); Template & lt; Class type & gt; Zero Riversstakes (Linked Stack Type & Lt; Type & gt; Stack); Int main (int argc, char ** argv) {// declare stack variable linked stack type & lt; Int & gt; Pile; // Add some data to Stack Stack Peash (121); Stack.push (113); Stack.push (734); Stack.push (22); Stack.push (243); Stack.push (34); Stack.push (26); Stack.push (74); Stack.push (115); Stack.push (262); Cout & lt; & Lt; "Stack \ n \ n"; PrintStack (pile); ReverseStack (pile); Cout & lt; & Lt; "\ N Stack reversed: \ n"; PrintStack (pile); Cout & lt; & Lt; "\ N \ n ** Press any key to continue ** \ n"; Getchar (); Return 0; } Template & lt; Class type & gt; Zero print stock (linked stack type & lieutenant; type & gt; stack) {type item; LinkedStackType & LT; Type & gt; Tmpstack = stack; While (stack.isEmptyStack () == incorrect) {item = stack.top (); Stack.pop (); Cout & lt; & Lt; Item & lt; & Lt; ""; } Stack = tmpStack; } Template & lt; Class type & gt; Zero Riversstakes (Linked Stack Type & Lt; Type & gt; Stack) {Type Item; LinkedStackType & LT; Type & gt; TmpStack; While (stack.isEmptyStack () == incorrect) {item = stack.top (); Stack.pop (); TmpStack.push (item); } While (tmpstack.isEmptyStack () == incorrect) {item = tmpStack.top (); TmpStack.pop (); Stack.push (item); Cout & lt; & Lt; item; } Stack = tmpStack; Return; }    

I'm not 100%, but I think if your code will work Riverstacks .

  template & lt; Class type & gt; Delete the second loop of Zero Riversstakes (Linked Stack Type & Lt; Type & gt; Stack) {Type Item; LinkedStackType & LT; Type & gt; TmpStack; While (stack.isEmptyStack () == incorrect) {item = stack.top (); Stack.pop (); TmpStack.push (item); } // while (tmpStack.isEmptyStack () == incorrect) // {// item = tmpStack.top (); // tmpstack.pop (); // stack.push (item); // cout & lt; & Lt; item; //} stack = tmpStack; Return; }    

No comments:

Post a Comment