Header file section include using namespace std Implement a
//Header file section
 #include<iostream>
 using namespace std;
 //Implement a simple array based Stack class template.
 template<class T>
 class Stack
 {
 public:
   //Constructor
   Stack(int maxSize);
   //Destructor
   ~Stack()
   {
    delete[] S;
   }
   //isEmpty method call
   int IsEmpty()const
   {
    return top==-1;
   }
   //getSize method call
   int getSize()const
   {
    return top==MaxTop;
   }
   //Top method call
   T Top()const;
   //Push method call
   void Push(T);
   T Pop();
   void Print();
 private:
   int top;
   int MaxTop;
   T *S;
 };
 //Method defintion of constructor
 template<class T>
 Stack<T>::Stack(int maxSize)
 {
 
 MaxTop=maxSize-1;
 S=new T[maxSize];
 top=-1;
 }
 //Method definiton of Push
 template<class T>
 void Stack<T>::Push(T x)
 {
 if(getSize())
   cout<<\"no memory()\";
 else
 {
   S[++top]=x;
 }
 }
 //Method definition of Pop
 template<class T>
 T Stack<T>::Pop()
 {
 T x;
 if(IsEmpty())
 {
   cout<<\"stack is empty\ \";
   return -1;
 }
 else
 {
   x=S[top--];
   return x;
 }
 }
 //Method definition of Top
 template<class T>
 T Stack<T>::Top()const
 {
 if(IsEmpty())
   return 0;
 else
   return S[top];
 }
//Method definition of Display()
 template<class T>
 void Stack<T>::Print()
 {
 //an empty stack by throwing an exception
 if(IsEmpty())
   cout<<\"Error:Empty stack by throwing an exception.\";
 else
   for(int i=top;i>=0;i--)
   {
    cout<<S[i]<<\"\\t\";
   }
 }
 //choice method
 void choice()
 {
 cout<<\"\ 1.Push\ 2.Pop\ 3.Top\ 4.Print\ \";
 }
 //main method
 void main()
 {
 //create a stack class\'s object
 Stack<int>sobj(5);
 int ch,x;
//Choose the stack operation\'s options
 do
 {
   choice();
   cout<<\"Enter your choice:\";
   cin>>ch;
  switch(ch)
   {
    case 1:
     cout<<\"Enter a value to push into the stack:\";
     cin>>x;
     sobj.Push(x);
     break;
    case 2:
     x=sobj.Pop();
     if(x!=-1)
      cout<<\"Pop value is\\t\"<<x<<endl;
     break;
    case 3:
     if(x=sobj.Top())
      cout<<\"Top most value is \\t\"<<x<<endl;
     break;
    case 4:
     sobj.Print();
     break;
   }
 }while(ch>=1&&ch<=4);
 //Pause the system for a while
 system(\"PAUSE\");
 }
Stack object data structures do not contain code to throw an exception when a stack.pop() function is called on an empty stack. This is due to the fact that it is easy to establish this exception handing elsewhere. Create a class named SafeStack that implements a stack of strings.
Use an instance of stack from <string> to hold string values and implement the same interface as the data type. However, your implementation (class) should throw an exception if an attempt is made to remove a value from an empty stack.
See Chapter 17 of the book for how to complete this assignment.
Submit a screenshot of the executed program and the code of the program. can anyone help with this question. i coompiled that code but i have errors.
Solution
#include <stdio.h>
#define SIZE 10
int ar[SIZE];
int top1 = -1;
int top2 = SIZE;
//Functions to push data
void push_stack1 (int data)
{
if (top1 < top2 - 1)
{
ar[++top1] = data;
}
else
{
printf (\"Stack Full! Cannot Push\ \");
}
}
void push_stack2 (int data)
{
if (top1 < top2 - 1)
{
ar[--top2] = data;
}
else
{
printf (\"Stack Full! Cannot Push\ \");
}
}
//Functions to pop data
void pop_stack1 ()
{
if (top1 >= 0)
{
int popped_value = ar[top1--];
printf (\"%d is being popped from Stack 1\ \", popped_value);
}
else
{
printf (\"Stack Empty! Cannot Pop\ \");
}
}
void pop_stack2 ()
{
if (top2 < SIZE)
{
int popped_value = ar[top2++];
printf (\"%d is being popped from Stack 2\ \", popped_value);
}
else
{
printf (\"Stack Empty! Cannot Pop\ \");
}
}
//Functions to Print Stack 1 and Stack 2
void print_stack1 ()
{
int i;
for (i = top1; i >= 0; --i)
{
printf (\"%d \", ar[i]);
}
printf (\"\ \");
}
void print_stack2 ()
{
int i;
for (i = top2; i < SIZE; ++i)
{
printf (\"%d \", ar[i]);
}
printf (\"\ \");
}
int main()
{
int ar[SIZE];
int i;
int num_of_ele;
printf (\"We can push a total of 10 values\ \");
//Number of elements pushed in stack 1 is 6
//Number of elements pushed in stack 2 is 4
for (i = 1; i <= 6; ++i)
{
push_stack1 (i);
printf (\"Value Pushed in Stack 1 is %d\ \", i);
}
for (i = 1; i <= 4; ++i)
{
push_stack2 (i);
printf (\"Value Pushed in Stack 2 is %d\ \", i);
}
//Print Both Stacks
print_stack1 ();
print_stack2 ();
//Pushing on Stack Full
printf (\"Pushing Value in Stack 1 is %d\ \", 11);
push_stack1 (11);
//Popping All Elements From Stack 1
num_of_ele = top1 + 1;
while (num_of_ele)
{
pop_stack1 ();
--num_of_ele;
}
//Trying to Pop From Empty Stack
pop_stack1 ();
return 0;
}






