Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#ifndef _ _ ICS 4 6 _ LL _ STACK _ HPP #define _ _ ICS 4 6 _ LL _ STACK _ HPP #include

#ifndef __ICS46_LL_STACK_HPP
#define __ICS46_LL_STACK_HPP
#include
#include
#include
namespace shindler::ics46::project0{
class StackEmptyException : public std::runtime_error {
public:
explicit StackEmptyException(const std::string& err)
: std::runtime_error(err){}
};
struct Node
{
int data;
Node *next;
};
// This is the LinkedListStack class you will be implementing for this project.
// Remember to see the write-up for more details!
template
class LinkedListStack {
private:
// fill in private member data here
Node *head;
Node *tail;
int linkedSize;
public:
// constructor
LinkedListStack();
// copy constructor (remember, deep copy!)
LinkedListStack(const LinkedListStack& stackb);
// assignment operator (remember, deep copy!)
LinkedListStack& operator=(const LinkedListStack& stackb);
// You do not need to implement these
LinkedListStack(LinkedListStack&&)= delete;
LinkedListStack& operator=(LinkedListStack&&)= delete;
// destructor
~LinkedListStack();
[[nodiscard]] size_t size() const noexcept;
[[nodiscard]] bool empty() const noexcept;
// We have two top() functions. The first gets called if your
// LinkedListStack is not a const, while the latter gets called on a const
// LinkedListStack (the latter might occur via const reference parameter,
// for instance). Be sure to test both! It is important when testing to
// ensure that EVERY funtion gets called and tested!
[[nodiscard]] T& top();
[[nodiscard]] const T& top() const;
void push(const T& elem) noexcept;
void pop();
};
template
LinkedListStack::LinkedListStack() : head(nullptr), tail(nullptr), linkedSize(0){
// TODO: Fill in your constructor implementation here.
// done using default member initialization
}
template
LinkedListStack::LinkedListStack(const LinkedListStack& stackb) : head(nullptr), tail(nullptr), linkedSize(0){
// TODO: Fill in your copy constructor implementation here.
Node* temp = stackb.head;
if (stackb.head == nullptr){
head = nullptr;
}
else {
while(temp->next != nullptr){
push(temp->data);
temp = temp->next;
}
}
}
template
LinkedListStack& LinkedListStack::operator=(
const LinkedListStack& stackb){
// TODO: Fill in your assignment operator implementation here.
//clear the existing content of the stack
if(this != &stackb){
while(!empty()){
pop();
}
//copy content from stackb
Node* temp = stackb.head;
while(temp->next != nullptr){
push(temp->data);
temp = temp->next;
}
}
return *this; // Stub so this function compiles without implementation.
}
template
LinkedListStack::~LinkedListStack(){
// TODO: Fill in your destructor implementation here.
while(!empty())
{
pop();
}
}
template
size_t LinkedListStack::size() const noexcept {
// TODO: Fill in your size() implementation here.
return linkedSize;
}
template
bool LinkedListStack::empty() const noexcept {
// TODO: Fill in your isEmpty() implementation here.
bool check = false;
if(head == nullptr){
check = true;
return check;
}
if (linkedSize ==0){
check = true;
return check;
}
return check;
}
template
T& LinkedListStack::top(){
// TODO: Fill in your top() implementation here.
// The following is a stub just to get the template project to compile.
// You should delete it for your implementation.
if(head){
return head->data;
}
}
template
const T& LinkedListStack::top() const {
// TODO: Fill in your const top() implementation here.
// The following is a stub just to get the template project to compile.
// You should delete it for your implementation.
if (head){
return head->data;
}
}
template
void LinkedListStack::push(const T& elem) noexcept {
// TODO: Fill in your push() implementation here.
// create a new list node
Node* newNode = new Node;
// assign data with the new node
newNode->next = nullptr;
newNode->data = elem;
//add the node to the top of the linked list
// update head to new node
head = newNode;
linkedSize++;
}
template
void LinkedListStack::pop(){
// TODO: Fill in your pop() implementation here.
if (head){
// assign local variable with head node data
Node* temp = head;
head = head->next;
// remove head node
delete temp;
//decrement size variable
linkedSize--;
}
}
}// namespace shindler::ics46::proje

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

Step: 3

blur-text-image

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

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

2. Darwins notes in biology.

Answered: 1 week ago

Question

What is the role of communication (Chapter 4) in leadership?

Answered: 1 week ago