-1

I want to print the values in the stack in the order they were given as input, but after compiling, the stack is 2 4 7 5. Could anyone please help me?

stacktype.h

#ifndef STACKTYPE_H_INCLUDED
#define STACKTYPE_H_INCLUDED

const int MAX_ITEMS = 5;
class FullStack
// Exception class thrown
// by Push when stack is full.
{};
class EmptyStack
// Exception class thrown
// by Pop and Top when stack is emtpy.
{};
template <class ItemType>
class StackType
  {
   public:
     StackType();
     bool IsFull();
     bool IsEmpty();
     void Push(ItemType);
     void Pop();
     ItemType Top();
   private:
     int top;
     ItemType items[MAX_ITEMS];
  };

#endif // STACKTYPE_H_INCLUDED

stacktype.cpp

#include "StackType.h"

template <class ItemType>
StackType<ItemType>::StackType()
  {
    top = -1;
  }

template <class ItemType>
bool StackType<ItemType>::IsEmpty()
  {
    return (top == -1);
  }

template <class ItemType>
bool StackType<ItemType>::IsFull()
  {
    return (top == MAX_ITEMS-1);
  }

template <class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
  {
    if( IsFull() ) throw FullStack();
    top++;
    items[top] = newItem;
  }

template <class ItemType>
void StackType<ItemType>::Pop()
  {
    if( IsEmpty() ) throw EmptyStack();
    top--;
  }

template <class ItemType>
ItemType StackType<ItemType>::Top()
  {
    if (IsEmpty()) throw EmptyStack();
    return items[top];
  }

main.cpp

#include <iostream>
#include "StackType.h"
#include "StackType.cpp"

using namespace std;

int main()
   {
      StackType<int> mystack;

     if (mystack.IsEmpty()) {
          cout << "Stack is Empty" << endl;
   }
   else {
    cout << "Stack is Not Empty" << endl;
   }

    int stc[]={5, 7, 4, 2};

    for(int i=0; i<4; i++)
   {
      mystack.Push(stc[i]);
   }

   if (mystack.IsEmpty()) {
       cout << "Stack is Empty" << endl;
   }
   else {
       cout << "Stack is Not Empty" << endl;
   }

   if (mystack.IsFull()) {
       cout << "Stack is Full" << endl;
   }
   else {
       cout << "Stack is Not Full" << endl;
   }

   for(int i=0; i<4; i++)
    {
       cout << mystack.Top() << " ";
       mystack.Pop();
    }
    cout << endl;

[After compilation the stack looks 2 4 7 5 but I want to print the stack like 5 7 4 2][1]} [1]: https://i.stack.imgur.com/xyPXq.jpg

Tonmoy
  • 1
  • 1
    A stack is a container with a limited interface. You can insert items and remove them, and what you remove is the most recently inserted item. If you want to iterate through the container in the order of insertion, then that container *shouldn't be a stack*. – Nicol Bolas Mar 13 '22 at 15:42
  • A stack is a LIFO data structure. That is how they are designed, and how you designed yours. They generally only have operations: push, pop, top, and optionally size, empty, and clear. Restricted to *only* those possible operations your only option is to invert your stack into a new one by repeatedly popping until empty and pushing into a new stack, then repeatedly popping the new stack, printing the values as they come off, and pushing them back on to the original stack to restore its former glory. – WhozCraig Mar 13 '22 at 15:42
  • Read this please: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – πάντα ῥεῖ Mar 13 '22 at 15:52

1 Answers1

0

main.cpp

#include <iostream>
#include "StackType.h"
#include "StackType.cpp"

using namespace std;

int main()
{
    StackType<int> mystack;
    StackType<int> temp;

    if (mystack.IsEmpty()) {
        cout << "Stack is Empty" << endl;
    }
    else {
        cout << "Stack is Not Empty" << endl;
    }

    int stc[]={5, 7, 4, 2};

    for(int i=0; i<4; i++)
    {
        mystack.Push(stc[i]);
    }

    if (mystack.IsEmpty()) {
        cout << "Stack is Empty" << endl;
    }
    else {
        cout << "Stack is Not Empty" << endl;
    }

    if (mystack.IsFull()) {
        cout << "Stack is Full" << endl;
    }
    else {
        cout << "Stack is Not Full" << endl;
    }


    for(int i=0; i<4; i++)
    {
        temp.Push(mystack.Top());
        mystack.Pop();
    }

    for(int i=0; i<4; i++)
    {
       cout << temp.Top() << " ";
       mystack.Push(temp.Top());
       temp.Pop();
    }
    cout << endl;

    mystack.Push(3);

    for(int i=0; i<5; i++)
    {
        temp.Push(mystack.Top());
        mystack.Pop();
    }

    for(int i=0; i<5; i++)
    {
        cout << temp.Top() << " ";
        mystack.Push(temp.Top());
        temp.Pop();
    }
    cout<<endl;

    if (mystack.IsFull()) {
        cout << "Stack is Full" << endl;
    }
    else {
        cout << "Stack is Not Full" << endl;
    }

    mystack.Pop();
    mystack.Pop();
    cout<<mystack.Top()<<" ";
}
Tonmoy
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 26 '22 at 12:17