Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions