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).

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

the above is the command to display the elements

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

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??

Votes + Comments
sir, how was the codes in reversing a word using stacks?
2
Contributors
1
Reply
6
Views
9 Years
Discussion Span
Last Post by Radical Edward
1

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:

#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();
  }
}

To get the items in the order they were pushed you need a queue:

#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();
  }
}

Or you can figure out a way to reverse the stack. One way is to use two stacks:

#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();
  }
}

Each item is popped from the reversed stack into the temporary stack. Reversing a reversed list gives you the forward list:

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|

Another way is to display the stack using recursion:

#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);
}

This is exactly the same thing as using two stacks because recursion takes the place of the temporary stack.

Votes + Comments
Whoo! =)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.