Question

I need a C++ makedile for a templated header class called LinkedStack.h and the implementation in...

I need a C++ makedile for a templated header class called LinkedStack.h and the implementation in LinkedStack.cpp. These are both used in main.cpp. I have been getting compilation errors.

Homework Answers

Answer #1

#ifndef STACK_HPP

#define STACK_HPP

namespace adt{

template<class T>

class stack{

private:

class Node{

public:

T item;

Node* prev;

};

Node* top;

int size;

public:

stack();

~stack();

void empty();

bool isEmpty();

int getSize();

the stack

T* showTop();

T* pop();

void push(const T& newItem);

bool isFull();

};

}

/////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

namespace adt{

template<class T>

stack<T> :: stack(){

top = NULL;

size = 0;

}

template<class T>

stack<T> :: ~stack(){

Node* aux;

while(top){

aux = top;

top = top->prev;

delete aux;

}

}

template<class T>

void stack<T> :: empty(){

Node* aux;

while(top){

aux = top;

top = top->prev;

delete aux;

}

size = 0;

}

template<class T>

bool stack<T> :: isEmpty(){

return top==NULL;

}

template<class T>

int stack<T> :: getSize(){

return size;

}

template<class T>

T* stack<T> :: showTop(){

if(top){

return &(top->item);

}

return NULL;

}

template<class T>

T* stack<T> :: pop(){

if(top){

//save the item

T* pItem = &(top->item);

//save the node to destroy

Node* oldTop = top;

//update the top

top = top->prev;

//destroy the oldTop

delete oldTop;

//update size

size--;

//return the item

return pItem;

}

return NULL;

}

template<class T>

void stack<T> :: push(const T& newItem){

Node* newNode;

try{

newNode = new Node();

}catch( std::bad_alloc &ba ){

throw ba;

}

newNode->prev = top;

newNode->item = newItem;

top = newNode;

size++;

}

template<class T>

bool stack<T> :: isFull(){

Node* newNode;

try{

newNode = new Node();

}catch( std::bad_alloc &ba ){

return true;

}

delete newNode;

return false;

}

}

/////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#include <iostream>

#include "stack.hpp"

using namespace std;

int main(){

adt::stack<float> s;

//testing initial conditions:

cout << "create" << '\n';

cout << "isEmpty: " << s.isEmpty() << "\n";

cout << "getSize: " << s.getSize() << "\n";

cout << "showTop: " << s.showTop() << "\n";

cout << "isFull: " << s.isFull() << "\n\n";


s.push(8.5);

s.push(8.5);

s.push(8.5);

s.push(8.5);

s.push(8.5);

s.push(7.5);

s.push(9.5);

cout << "push" << '\n';

cout << "isEmpty: " << s.isEmpty() << "\n";

cout << "getSize: " << s.getSize() << "\n";

cout << "showTop: " << *(s.showTop()) << "\n";

cout << "isFull: " << s.isFull() << "\n\n";

s.pop();

cout << "pop" << '\n';

cout << "isEmpty: " << s.isEmpty() << "\n";

cout << "getSize: " << s.getSize() << "\n";

cout << "showTop: " << *(s.showTop()) << "\n";

cout << "isFull: " << s.isFull() << "\n";

return 0;

}

PLEASE GIVE A THUMBS UP!!!!!!!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How can I avoid compilation errors in C++?
How can I avoid compilation errors in C++?
Create a stadium class. The class header file content (.h file) can go in this question,...
Create a stadium class. The class header file content (.h file) can go in this question, the class implementation (.cpp file) can go in the following question, and the main.cpp content can go in the third question. static data members: starting seat number next seat number data members: stadium name name of home team current opponent seats sold (vector of seat numbers) member functions: custom constructor - take input of stadium name default constructor get next seat number (static) change...
From the Examples in this module that I have posted, Write a class called ‘Student’ inheriting...
From the Examples in this module that I have posted, Write a class called ‘Student’ inheriting ‘Person’ class with the following properties and methods Degree GPA Getters and Setters Write another class called ‘Faculty’ inheriting ‘Employee’ with the following properties and methods Department Specialization Getters and setters. (customer.py has the classes you need) *** IN PYTHON PLEASE***
Write a C++ header (.h) file to specify a class GiftCard, with at least 3 meaningful...
Write a C++ header (.h) file to specify a class GiftCard, with at least 3 meaningful private data members and 3 public methods, including two constructors. At least one numeric data member and one calculator method to make this class meaningful / useful.
I need a c++ program for my class project. The topic for the c++ program is...
I need a c++ program for my class project. The topic for the c++ program is On Cyber Security password cracking. please keep it easy and simple. So the professor doesn't doubt on me. Advanced Thank you for your help.
JAVA. The question mentioned was: Create a class called Point that represents a point in the...
JAVA. The question mentioned was: Create a class called Point that represents a point in the cartesian coordinate system. The class should have fields representing the x coordinate and y coordinate (both are integer type). Question I need an answer to: Write a client class that would create two objects in the Point class above called p1 and p2 and assigns values to the filed in these objects. Print out the x coordinate followed by x comma and last coordinate...
Please write a class "template" AVLTree in C++. The class must be placed in a single...
Please write a class "template" AVLTree in C++. The class must be placed in a single header file called AVLTree.h The class must contain the following public member functions: -       necessary constructors for your implementation -       destructor -       isEmpty () – determines if the tree is empty - returns 1 if it is empty; 0 otherwise -       height () – returns the height of the node or -1 if a nullptr -       insert () – inserts data into the tree – must abide by AVL...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
Part 1 I need to make an example class that I make up on my own....
Part 1 I need to make an example class that I make up on my own. It should have some variables, at least one function and at least one procedure in it. And then I should create two instances of the class and demonstrate how to use the class by calling some of the procedures and functions. What I make my class about is up to me. Part 2 Do a simple example of ByVal vs ByRef. I can do...
Create a class called BirthYear. It should only have two member, both of them arrays. One...
Create a class called BirthYear. It should only have two member, both of them arrays. One of type string called Names. The second of type int called BirthYears. in your main class create a StudentBirthYear object. Make sure both arrays are of the size 10. Add ten different names and ten different birth years. Then display each name with its birth year using only 1 for loop. (You can display in a for loop as well as a foreach loop,...