I need help writing this program in c++. Thank You. Dynamic Stack Template Write
ID: 3670246 • Letter: I
Question
I need help writing this program in c++. Thank You.
Dynamic Stack Template Write your own version of a class template that will create a dynamic stack of any data type. Be sure to include push, pop, and isEmpty methods. The pop method should throw an exception if the stack is empty before attempting to remove the node from the list.
In addition, include a copy constructor and an overloaded assignment operator.
Demonstrate the class with a driver program. The program should ask the user to enter a string. The program then uses the stack to display the string in reverse order. You may not use containers from the STL.
A possible sample run: > Hello, World!
!dlroW ,olleH
Explanation / Answer
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
// A structure to represent a stack
struct Stack
{
int top;
unsigned capacity;
char* array;
};
// function to create a stack of given capacity. It initializes size of
// stack as 0
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (char*) malloc(stack->capacity * sizeof(char));
return stack;
}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack)
{ return stack->top == stack->capacity - 1; }
// Stack is empty when top is equal to -1
int isEmpty(struct Stack* stack)
{ return stack->top == -1; }
// Function to add an item to stack. It increases top by 1
void push(struct Stack* stack, char item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
// Function to remove an item from stack. It decreases top by 1
char pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}
// A stack based function to reverese a string
void reverse(char str[])
{
// Create a stack of capacity equal to length of string
int n = strlen(str);
struct Stack* stack = createStack(n);
// Push all characters of string to stack
int i;
for (i = 0; i < n; i++)
push(stack, str[i]);
// Pop all characters of string and put them back to str
for (i = 0; i < n; i++)
str[i] = pop(stack);
}
int main()
{
char str[100];
cout<<"Enter a string: ";
cin.getline(str,sizeof(str));
reverse(str);
cout<<"Reversed string is "<<str<<endl;
return 0;
}
Out Put:
Enter a string: This is Naidu
Reversed string is udiaN si sihT