Reverse printing on a stack

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 55
Reputation: RayvenHawk is an unknown quantity at this point 
Solved Threads: 1
RayvenHawk RayvenHawk is offline Offline
Junior Poster in Training

Reverse printing on a stack

 
1
  #1
Aug 30th, 2008
I've finished writing the program for my class and everything actually does what it's suppose to (well 99% anyway).

I'm going to add snippets of the code that should help figure out the small glitch. What's happening is I can add x amount of elements to my stack and display them, but when they are displayed they are in reverse order instead of from bottom to top (the order they were input into the stack).

  1. template<class Type>
  2. Type mystack<Type>::top() const
  3. {
  4. assert(stackTop != 0);
  5. return list[stackTop - 1];
  6. }

the above is the command to display the elements

  1. template<class Type>
  2. void mystack<Type>::push(const Type& newItem)
  3. {
  4. if(!isFullStack())
  5. {
  6. list[stackTop] = newItem;
  7. stackTop++;
  8. }
  9. else
  10. cout <<"Cannot add to a full stack."<<endl;
  11. }

the above is the code to push the new element into the stack

  1. if (choice == 1)
  2. {
  3. do
  4. {
  5. cin >> num;
  6. intStack.push(num);
  7. j++;
  8. }
  9. while(j < amount);
  10. workStack = intStack;
  11. }

the above is the main function to call the push command. (int j is initialized to 0)

Any reason why my stack is displaying backwards??
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Reverse printing on a stack

 
1
  #2
Aug 30th, 2008
Originally Posted by RayvenHawk
Any reason why my stack is displaying backwards??
That's how a stack works, it's a "last-in, first-out" structure. The item at the top will always be the last item that was pushed:
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. int main()
  5. {
  6. using namespace std;
  7.  
  8. stack<int> st;
  9.  
  10. for (int i = 0; i < 5; ++i)
  11. st.push(i);
  12.  
  13. // Display backward
  14. while (!st.empty()) {
  15. cout << st.top() << '\n';
  16. st.pop();
  17. }
  18. }
To get the items in the order they were pushed you need a queue:
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. int main()
  5. {
  6. using namespace std;
  7.  
  8. queue<int> q;
  9.  
  10. for (int i = 0; i < 5; ++i)
  11. q.push(i);
  12.  
  13. // Display forward
  14. while (!q.empty()) {
  15. cout << q.front() << '\n';
  16. q.pop();
  17. }
  18. }
Or you can figure out a way to reverse the stack. One way is to use two stacks:
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. int main()
  5. {
  6. using namespace std;
  7.  
  8. stack<int> st;
  9.  
  10. for (int i = 0; i < 5; ++i)
  11. st.push(i);
  12.  
  13. stack<int> temp;
  14.  
  15. // Display forward
  16. while (!st.empty()) {
  17. temp.push(st.top());
  18. st.pop();
  19. }
  20.  
  21. while (!temp.empty()) {
  22. cout << temp.top() << '\n';
  23. temp.pop();
  24. }
  25. }
Each item is popped from the reversed stack into the temporary stack. Reversing a reversed list gives you the forward list:
  1. st:
  2.  
  3. |4|
  4. |3| |3|
  5. |2| |2| |2|
  6. |1| |1| |1| |1|
  7. |0| |0| |0| |0| |0|
  8.  
  9. temp:
  10.  
  11. |0|
  12. |1| |1|
  13. |2| |2| |2|
  14. |3| |3| |3| |3|
  15. |4| |4| |4| |4| |4|
Another way is to display the stack using recursion:
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. namespace EdRules {
  5. using namespace std;
  6.  
  7. void DisplayForward(stack<int> st)
  8. {
  9. if (st.empty())
  10. return;
  11.  
  12. int temp = st.top();
  13.  
  14. st.pop();
  15. DisplayForward(st);
  16.  
  17. cout << temp << '\n';
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. using namespace std;
  24.  
  25. stack<int> st;
  26.  
  27. for (int i = 0; i < 5; ++i)
  28. st.push(i);
  29.  
  30. // Display forward
  31. EdRules::DisplayForward(st);
  32. }
This is exactly the same thing as using two stacks because recursion takes the place of the temporary stack.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC