Problem 1 (Stacks) In the lecture we studied IntStack, a class that implements a static stack of integers. Write a templ

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Problem 1 (Stacks) In the lecture we studied IntStack, a class that implements a static stack of integers. Write a templ

Post by answerhappygod »

Problem 1 Stacks In The Lecture We Studied Intstack A Class That Implements A Static Stack Of Integers Write A Templ 1
Problem 1 Stacks In The Lecture We Studied Intstack A Class That Implements A Static Stack Of Integers Write A Templ 1 (18.23 KiB) Viewed 33 times
Please use main.cpp, Data.h , implementation.cpp
main.cpp
// This program demonstrates the IntStack class.
#include <iostream>
#include "Data.h"
using namespace std;
int main()
{
IntStack stack(5);
int catchVar;
cout << "Pushing 5\n";
stack.push(5);
cout << "Pushing 10\n";
stack.push(10);
cout << "Pushing 15\n";
stack.push(15);
cout << "Pushing 20\n";
stack.push(20);
cout << "Pushing 25\n";
stack.push(25);
cout << "Popping...\n";
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
}
Data.h
#pragma once
class IntStack
{
private:
int *stackArray;
int stackSize;
int top;
public:
IntStack(int size);
void push(int num);
void pop(int &num);
bool isFull();
bool isEmpty();
};
Implementation.cpp
#include <iostream>
#include "Data.h"
using namespace std;
//*******************
// Constructor *
//*******************
IntStack::IntStack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}
//*************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************
void IntStack::push(int num)
{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = num;
}
}
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
void IntStack::pop(int &num)
{
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
num = stackArray[top];
top--;
}
}
//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//***************************************************
bool IntStack::isFull()
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
bool IntStack::isEmpty()
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
Problem 1 (Stacks) In the lecture we 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. To run / execute your program, click the button below...
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply