943,807 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3512
  • C++ RSS
Aug 30th, 2008
1

Reverse printing on a stack

Expand Post »
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).

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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??
Similar Threads
Reputation Points: 32
Solved Threads: 2
Junior Poster in Training
RayvenHawk is offline Offline
77 posts
since Aug 2008
Aug 30th, 2008
1

Re: Reverse printing on a stack

Quote 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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Simple Source Control
Next Thread in C++ Forum Timeline: Limited digits from atof() OR strtod()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC