Question

In: Computer Science

You will write Stack class in c++ that will be integrated into a larger software product...

You will write Stack class in c++ that will be integrated into a larger software product provided by the instructor. This Stack class uses a dynamically allocated array to store data values (integers) that have been pushed onto the stack object. When the client code attempts to push another integer onto a full stack, your Push operation should invoke the Resize() function which attempts to double the capacity of the stack and then add the new data value to the resized stack array. In Resize() a new array (that holds twice as many integers) is dynamically allocated, data from the old stack array is copied into the new larger stack array, the old stack array is deallocated, and the new data value is pushed onto the new array.

Solutions

Expert Solution

ANS:

//STACK HEADER

class Stack
{
private:
   int* stackArray;
   int StackSize;
   int top;
public:
   Stack(int);
   ~Stack();
   void push(int);
   void pop(int&);
   bool isFull();
   bool isEmpty();

   void reSizeStack(int item);
   int sizeOfStack();
   void printStack();
};

//STACK CPP

#include "Stack.h"
#include<iostream>
using namespace std;
Stack::Stack(int len)
{
   stackArray = new int[len];
   StackSize = len;
   top = -1;
}
Stack::~Stack()
{
   delete[] stackArray; // deleteing stack dynamic array after program completion
}
void Stack::push(int item)
{
   if (isFull())
   {
       cout << "Stack is Full" << endl;
       cout << "Resize Stack" << endl;
       reSizeStack(item);
   }

   else
   {
       top += 1;
       stackArray[top] = item;
       cout << "Element Added in stack" << endl;
   }
}
void Stack::pop(int& num)
{
   if (isEmpty())
   {
       cout << "Stack is Empty" << endl; // If empty then only cout else pop
   }

   else
   {
       num = stackArray[top];
       top--;
   }

}

void Stack::reSizeStack(int item)
{
   int oldSize = StackSize;
   int* oldStack = new int[StackSize];
   StackSize = StackSize * 2;

   //Copying old stack data to new stack

   for (int i = 0; i < oldSize; i++)
   {
       oldStack[i] = stackArray[i];
   }

   stackArray = new int[StackSize];

   //Copying Old data into StackArray and new element too

   for (int i = 0; i < oldSize; i++)
   {
       stackArray[i] = oldStack[i];
   }

   //Inserti\ng new element
   push(item);


  
}

bool Stack::isFull()
{
   if (top == StackSize - 1)
   {
       return true; // If top is equal to end of array then it is full so return true
   }

   else
   {
       return false;
   }
}
bool Stack::isEmpty()
{
   if (top == -1)
   {
       return true; //if top=-1 it means no element in stack so empty
   }

   else
   {
       return false;
   }
}

int Stack::sizeOfStack()
{
   return StackSize;
}

void Stack::printStack()
{

   cout << "Visiting Stack and printing each element" << endl;
   int element;
   for (int i = 0; i < StackSize; i++)
   {
       pop(element);
       cout << "Element#" << i << "=" << element << endl;
   }

}

int main()
{
   Stack obj(3);

   cout << "Enter elements of Stack" << endl;

   int element;

   for (int i = 0; i < 3; i++)
   {
       cout << "Enter element#" << i + 1 << endl;
       cin >> element;
       obj.push(element);

   }

   obj.push(10);
   obj.printStack();
}

Comment down for any queries
Please give a thumbs up if you are satisfied with answer :)


Related Solutions

Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
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...
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...
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
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...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
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.
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT