stack.h class stack { private: // data members and utility functions public: //
ID: 3726539 • Letter: S
Question
stack.h
class stack
{
private: // data members and utility functions
public: // for the client to use
// Constructor
stack();
// Destructor
~stack();
// HOW TO CALL: No argument. Returns Boolean.
// PURPOSE: returns true if stack is empty else false
bool isEmpty ();
// HOW TO CALL: No argument. Returns Boolean.
// PURPOSE: returns true if stack is full else false
bool isFull ();
// HOW TO CALL: Provide the element to be pushed as an argument.
// PURPOSE: if full, calls an emergency exit routine
// if not full, enters an element at the top
void push (el_t);
// HOW TO CALL: Gives back the removed element via pass by reference.
// PURPOSE: if empty calls an emergency exit routine
// if not empty, removes an element from the top
void pop (el_t&);
void pop (el_t&);
void pop (el_t&);
};
________________________________
stack.cpp
--------------------------------
stacktest.cpp
Explanation / Answer
here is your files : ------->>>>>>>>>>.
Stack.h : ------->>>>>>>.
#ifndef _STACK_H_
#define _STACK_H_
#define MAX 100
#include<iostream>
using namespace std;
class Stack{
private:
int *data;
int top_;
public:
Stack();
~Stack();
bool isEmpty();
bool isFull();
void push(int el_t);
void top(int &el_t);
void pop(int &el_t);
};
#endif
Stack.cpp : ---------->>>>>>>
#include "Stack.h"
Stack::Stack(){
data = new int[MAX];
top_ = -1;
}
Stack::~Stack(){
delete[] data;
top_ = -1;
}
bool Stack::isEmpty(){
if(top_ <= -1){
top_ = -1;
return true;
}
return false;
}
bool Stack::isFull(){
if(top_ >= (MAX-1)){
return true;
}
return false;
}
void Stack::push(int el_t){
if(isFull()){
return;
}
top_++;
data[top_] = el_t;
}
void Stack::top(int &el_t){
if(isEmpty()){
return;
}
el_t = data[top_];
}
void Stack::pop(int &el_t){
if(isEmpty()){
return;
}
el_t = data[top_];
top_--;
}
stacktest.cpp : --------->>>>>>>.
#include "Stack.cpp"
int main(){
Stack s;
if(s.isEmpty()){
cout<<"stack is empty"<<endl;;
}
s.push(23);
int el_t;
if(!s.isEmpty()){
s.top(el_t);
cout<<"top = "<<el_t<<endl;
}
s.push(21);
if(!s.isEmpty()){
s.top(el_t);
cout<<"top = "<<el_t<<endl;
}
s.push(54);
if(!s.isEmpty()){
s.top(el_t);
cout<<"top = "<<el_t<<endl;;
}
s.push(90);
if(!s.isEmpty()){
s.top(el_t);
cout<<"top = "<<el_t<<endl;
}
s.pop(el_t);
cout<<"Pop Element = "<<el_t<<endl;
if(!s.isEmpty()){
s.top(el_t);
cout<<"Now top = "<<el_t<<endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.