Question

In: Computer Science

C++ Part 1: Developing And Testing A Stack Template Write a template, Stack.h, to implement a...

C++

Part 1: Developing And Testing A Stack Template

Write a template, Stack.h, to implement a LIFO stack. Here is the specification for its public interface:

class Stack
{
  ...
  Stack( ); // may have a defaulted parameter
  Stack(const Stack<V>&); // copy constructor
  ~Stack();
  Stack<V>& operator=(const Stack<V>&);
  void push(const V&);
  const V& peek( );
  void pop( );
  int size( ) const;
  bool empty( ) const;
  void clear( );
}; 

If you use dynamic memory (and you surely will!) be sure to include the three memory management functions as public members, too. You may implement your Stack as arrayed or as linked -- your choice.

Fully test your template in a test driver CPP named Stack.TestDriver.cpp, remembering to include all the tests we've learned about in this class. Then use the H file in the following application:

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
using namespace std;

// define default capacity of stack
#define SIZE 10

// Class for stack
template <class X>
class stack
{
   X *arr;
   int top;
   int capacity;

public:
   stack(int size = SIZE);   // constructor

   void push(X);
   X pop();
   X peek();

   int size();
   bool isEmpty();
   bool isFull();

   // destructor
   ~stack(){
       delete[] arr;
   }
};

// Constructor to initialize stack
template <class X>
stack<X>::stack(int size)
{
   arr = new X[size];
   capacity = size;
   top = -1;
}

// function to add an element x in the stack
template <class X>
void stack<X>::push(X x)
{
   if (isFull())
   {
       cout << "OverFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }

   cout << "Inserting " << x << endl;
   arr[++top] = x;
}

// function to pop top element from the stack
template <class X>
X stack<X>::pop()
{
   // check for stack underflow
   if (isEmpty())
   {
       cout << "UnderFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }

   cout << "Removing " << peek() << endl;

   // decrease stack size by 1 and (optionally) return the popped element
   return arr[top--];
}

// function to return top element in a stack
template <class X>
X stack<X>::peek()
{
   if (!isEmpty())
       return arr[top];
   else
       exit(EXIT_FAILURE);
}

// Utility function to return the size of the stack
template <class X>
int stack<X>::size()
{
   return top + 1;
}

// Utility function to check if the stack is empty or not
template <class X>
bool stack<X>::isEmpty()
{
   return top == -1;   // or return size() == 0;
}

// Utility function to check if the stack is full or not
template <class X>
bool stack<X>::isFull()
{
   return top == capacity - 1;   // or return size() == capacity;
}

int main()
{
   stack<string> pt(2);

   pt.push("A");
   pt.push("B");

   pt.pop();
   pt.pop();

   pt.push("C");

    // Prints the top of the stack
    cout << "Top element is: " << pt.peek() << endl;

   // Returns the number of elements present in the stack
   cout << "Stack size is " << pt.size() << endl;

   pt.pop();

   // check if stack is empty or not
   if (pt.isEmpty())
       cout << "Stack Is Empty\n";
   else
       cout << "Stack Is Not Empty\n";

   return 0;
}


Related Solutions

Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
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...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
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...
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT