0

I just coded this implementation of a stack using a singly linked list. Its written in C++ using the STL. I believe I am complete with it but there may be functions I am forgetting or ways to improve/ optimize my code. I tried using exceptions to learn how to use them so the code may not be best practice. Anyhow anyone who may want to use this use it, anyone looking to suggest ways to optimize my code type away :)

```
/*
* Stack.h
*/
#ifndef STACK_H_
#define STACK_H_
template <typename T>
class Stack {
public:
// default constructor
Stack(): front(NULL) , stackSize(0) {}
// destructor
virtual ~Stack();
// add node to the top of the stack
void push(const T& item);
// delete a node from the top of the stack
void pop();
// references the item at the top of the stack
T& top();
// get the size of the stack
int size() {return stackSize;}
// returns true if the stack is empty, false otherwise
bool empty() {return ((front == NULL) ? true: false);}
// returns true if you can't allocate new memory, false otherwise
bool full();
// write out the stack
void writeStack();
// clear the stack
void clear();
private:
struct node {
T nodeValue;
node *next;
// default constructor
node(): next(NULL) {}
// constructor. Initialize nodeValue and next.
node(const T& item, node *nextNode = NULL):
nodeValue(item), next(nextNode) {}
};
node *front, *newNode;
int stackSize;
};
template <typename T>
void Stack<T>::push(const T& item){
try {
newNode = new node(item,front);
if (!full()){
front = newNode;
stackSize++;
}
else
throw "Allocation error";
}
catch(char* e) {
std::cout<<"Error: "<<e<<std::endl;
}
}
template <typename T>
void Stack<T>::pop(){
if (!empty()) {
clear();
stackSize--;
}
else
std::cout<<"pop(): Stack is Empty"<<std::endl;
}
template <typename T>
T& Stack<T>::top(){
if (!empty())
return front->nodeValue;
else
std::cout<<"top(): Stack is empty"<<std::endl;
std::exit(1);
}
template <typename T>
bool Stack<T>::full(){
node *temp = new node;
if (temp == NULL)
return true;
else
return false;
}
template <typename T>
void Stack<T>::writeStack(){
node* curr = front;
while (curr != NULL){
std::cout<<curr->nodeValue<<" ";
curr = curr->next;
}
}
template <typename T>
void Stack<T>::clear(){
node* curr = front;
front = front->next;
delete curr;
}
template <typename T>
Stack<T>::~Stack(){
try {
if (!empty())
clear();
else
throw "Destructor: Stack is empty";
}
catch (char* e){
std::cout<<e<<std::endl;
}
}
#endif /* STACK_H_ */
```

Here is an example of main()

```
/*
* main.cpp
*/
#include <iostream>
#include "Stack.h"
using namespace std;
int main(){
Stack<int> myStack;
if (myStack.empty())
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
for (int i = 0; i < 10; i++)
myStack.push(i);
cout<<"The top of the Stack is: "<<myStack.top()<<endl;
if (myStack.empty())
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
myStack.writeStack();
cout<<endl<<"My Stack size is: "<<myStack.size()<<endl;
cout<<endl;
while (!myStack.empty())
myStack.pop();
if (myStack.empty())
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
cout<<"The top of the Stack is: "<<myStack.top()<<endl;
return 0;
}
```

heres the output

Stack is empty

The top of the Stack is: 9

Stack is not empty

9 8 7 6 5 4 3 2 1 0

My Stack size is: 10

Stack is empty

top(): Stack is empty