Question

C++ Create an array-based implementation of a stack. Each element of the stack should store a...

C++

Create an array-based implementation of a stack. Each element of the stack should store a string.

The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements).

Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a string), a pop method (returns a string), an empty method (returns bool), and a full method (returns bool).

  • Do not use structs for this assignment.
  • Do not use templates for this assignment.

In your driver program, create a stack with a maximum stack size of 5 elements. Present a menu to the user when the program begins:

  1. Push an item onto stack
  2. Pop an item from stack
  3. Quit

The push option should call the full method and provide a message if the stack is full. If not, it should ask the user for a data item from the keyboard. It will then call the stack's push method.

The pop option should call the empty method and provide a message if the stack is empty. If not, it should call the pop method which returns the item removed from the stack. The main driver should display the data item that was removed from the stack.

  • Upload a zipped file that contains only the following 3 files:
    • Stack.h
    • Stack.cpp
    • StackDriver.cpp
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Stack.h:

#pragma once
#include <iostream>
using namespace std;
class Stack
{
private:
   int max_size;
   int top;
   string* array;
public:
   Stack(int );
   ~Stack();
   void push(string );
   string pop();
   bool empty();
   bool full();
};

Stack.cpp:

#include "Stack.h"
// constructor
Stack::Stack(int max_size)
{
   this->max_size = max_size;
   this->array = new string[max_size];
   top = -1;
}
// destructor
Stack::~Stack()
{
   delete[] array;
}
// pushes to stack
void Stack::push(string value)
{
   top++;
   array[top] = value;
}
// returns popped element
string Stack::pop()
{
   top--;
   return array[top + 1];
}
// returns true if stack is empty
bool Stack::empty()
{
   if (top == -1)
       return true;
   return false;
}
// returns true if stack is full
bool Stack::full()
{
   if (top == max_size - 1)
       return true;
   return false;
}

StackDriver.cpp:

// do comment if any problem arises
// code
#include"Stack.h"
int main()
{
   // create a stack of maximum size 5
   Stack testing(5);
   while (true)
   {
       int choice;
       cout << "\n1. Push an item onto stack\n";
       cout << "2. Pop an item from stack\n";
       cout << "3. Quit\n";
       cout << "Your choice: ";
       cin >> choice;
       if (choice == 1)
       {
           if (testing.full())
               cout << "Stack is full!\n";
           else
           {
               cout << "Enter data: ";
               string temp;
               cin >> temp;
               testing.push(temp);
           }
       }
       else if (choice == 2)
       {
           if (testing.empty())
               cout << "Stack is empty!\n";
           else
               cout << "Popped item: " << testing.pop() << endl;
       }
       else
           break;
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
C++ Create an array-based implementation of a stack. Each element of the stack should store a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT