Question

In: Computer Science

Define a class template called genericStack for storing any data type in a static stack. Your...

Define a class template called genericStack for storing any data type in a static stack. Your class template must include a constructor, a destructor, push, pop, peek, isFull, and isEmpty functions (no display function is required). Write a simple main function in your program that demonstrates the class template with a stack of strings.

Solutions

Expert Solution

#include <iostream>
using namespace std;

// Class for stack
template <class ItemType>
class genericStack{
       private:
       ItemType *info;
       int top;

   public:
       genericStack();
       bool isEmpty();
       bool isFull();
       void push(ItemType x);
       void pop(ItemType &x);
       // destructor
       ~genericStack();
};

// Constructor to initialize stack
template <class ItemType>
genericStack<ItemType>::genericStack()
{
   info = new ItemType[5]; // set 5 as max
   top = -1; // assign -1 to first element
}
// Utility function to check if the stack is empty or not
template <class ItemType>
bool genericStack<ItemType>::isEmpty()
{
   return top == -1; // true for empty
}
// Utility function to check if the stack is full or not
template <class ItemType>
bool genericStack<ItemType>::isFull()
{
   return top == 5 ;   // top become size
}
// function to add an element x in the stack
template <class ItemType>
void genericStack<ItemType>::push(ItemType x)
{
   if (isFull())
   {
       cout << "Stack is Full.\n";
       return;
   }
   info[++top] = x; // push item to stack
}
// function to pop top element from the stack
template <class ItemType>
void genericStack<ItemType>::pop(ItemType &x)
{
   // check for stack underflow
   if (isEmpty())
   {
       cout << "UnderFlow Stack is Empty \n";
       return;
   }
   // // decrease stack size by 1 and copy to x
   x = info[top];
   top = top - 1;
}
// Destructor
template <class ItemType>
genericStack<ItemType>::~genericStack(){
   delete [] info; // delete stack
}
int main(){
   genericStack<string>obj; // create object of string
   string elem; // to get popped element
   // pushing
   obj.push("Ram");
   obj.push("albert");
   obj.push("newton");
   obj.pop(elem);
   cout<<elem<<endl; // newton since last in first out
   obj.pop(elem);
   cout<<elem<<endl;
   obj.pop(elem);
   cout<<elem<<endl;
}

/* OUTPUT */


Related Solutions

Write your own version of a class template that will create a static stack of any...
Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program. please make a version to copy.
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static stack of integers. Write a template that will create a static stack of any data type. Demonstrate the class with a driver program. Dynamic Stack Template In this chapter you studied DynIntStack, a class that implements a dynamic stack of integers. Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program. Static Queue Template...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
(C++ ONLY) You will define your own data type. It will be a struct called Fraction....
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form. You will...
C++ Define a class PersonalRecord, which will be storing an employee’s information. Internally, the class should...
C++ Define a class PersonalRecord, which will be storing an employee’s information. Internally, the class should have the following attributes: SSN (string type), fullName (string type), homeAddress (string type), phoneNumber (string type), salaryRate (per year, float type), and vacationDays (accrued vacation, int type). The constructor should take six parameters: four of them are necessary to provide values to at the time of initialization (when a constructor will be called/invoked): SSN, fullName, homeAddress, and phoneNumber; and two of them are optional...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT