| | |
Reverse printing on a stack
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 55
Reputation:
Solved Threads: 1
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).
the above is the command to display the elements
the above is the code to push the new element into the stack
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??
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)
template<class Type> Type mystack<Type>::top() const { assert(stackTop != 0); return list[stackTop - 1]; }
the above is the command to display the elements
C++ Syntax (Toggle Plain Text)
template<class Type> void mystack<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cout <<"Cannot add to a full stack."<<endl; }
the above is the code to push the new element into the stack
C++ Syntax (Toggle Plain Text)
if (choice == 1) { do { cin >> num; intStack.push(num); j++; } while(j < amount); workStack = intStack; }
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??
•
•
•
•
Originally Posted by RayvenHawk
Any reason why my stack is displaying backwards??
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stack> int main() { using namespace std; stack<int> st; for (int i = 0; i < 5; ++i) st.push(i); // Display backward while (!st.empty()) { cout << st.top() << '\n'; st.pop(); } }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <queue> int main() { using namespace std; queue<int> q; for (int i = 0; i < 5; ++i) q.push(i); // Display forward while (!q.empty()) { cout << q.front() << '\n'; q.pop(); } }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stack> int main() { using namespace std; stack<int> st; for (int i = 0; i < 5; ++i) st.push(i); stack<int> temp; // Display forward while (!st.empty()) { temp.push(st.top()); st.pop(); } while (!temp.empty()) { cout << temp.top() << '\n'; temp.pop(); } }
C++ Syntax (Toggle Plain Text)
st: |4| |3| |3| |2| |2| |2| |1| |1| |1| |1| |0| |0| |0| |0| |0| temp: |0| |1| |1| |2| |2| |2| |3| |3| |3| |3| |4| |4| |4| |4| |4|
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stack> namespace EdRules { using namespace std; void DisplayForward(stack<int> st) { if (st.empty()) return; int temp = st.top(); st.pop(); DisplayForward(st); cout << temp << '\n'; } } int main() { using namespace std; stack<int> st; for (int i = 0; i < 5; ++i) st.push(i); // Display forward EdRules::DisplayForward(st); }
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- Linked List Problem (C++)
- Reverse Output (Stack) (C++)
Other Threads in the C++ Forum
- Previous Thread: Simple Source Control
- Next Thread: Limited digits from atof() OR strtod()
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





