Question: The Problem Complete the class Stack to implement a basic stack. Use the requirements listed below to determine the apocopate data structure to use. The

The Problem

Complete the class Stack to implement a basic stack. Use the requirements listed below to determine the apocopate data structure to use.

The following member functions must be implemented in Stack:

int size() - returns the number of elements in the stack (0 if empty)

bool isEmpty() - returns if the list has no elements, else false

void push(int val) - pushes an item to the stack in O(1) time

int pop() - removes an item off the stack and returns the value in O(1) time

Only change the stack.cpp and stack.h files.

//main.cpp file

#include

using namespace std;

#include "stack.h"

int main() {

Stack s;

int r;

cout<<"push(3) to the stack..."<

s.push(3);

cout<<"Stack size: "<

cout<<"push(2) to the stack..."<

s.push(2);

cout<<"Stack size: "<

cout<<"push(1) to the stack..."<

s.push(1);

cout<<"Stack size: "<

cout<<"pop()"<

r = s.pop();

cout<<"Returned Value: "<

cout<<"pop()"<

r = s.pop();

cout<<"Returned Value: "<

cout<<"pop()"<

r = s.pop();

cout<<"Returned Value: "<

return 0;

}

//stack.cpp file

#include "stack.h"

// `int size()` - returns the number of elements in the stack (0 if empty)

int Stack::size() const {

return 0;

}

// `bool isEmpty()` - returns if the list has no elements, else false

bool Stack::isEmpty() const {

return true;

}

// `void push(int val)` - pushes an item to the stack in O(1) time

void Stack::push(int value) {

}

// `int pop()` - removes an item off the stack and returns the value in O(1) time

int Stack::pop() {

return -1;

}

//stack.h file

#ifndef _STACK_H

#define _STACK_H

class Stack {

public:

int size() const;

bool isEmpty() const;

void push(int value);

int pop();

};

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!