-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.hpp
More file actions
53 lines (46 loc) · 932 Bytes
/
Copy pathStack.hpp
File metadata and controls
53 lines (46 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef STACK_HPP
#define STACK_HPP
#include <iostream>
using namespace std;
template <typename Object>
class Node{
public:
Object item;
Node<Object> *next;
Node(const Object& item,Node<Object> *next=NULL){
this->item = item;
this->next = next;
}
};
template <typename Object>
class Stack{
private:
Node<Object> *topOfStack;
public:
Stack(){
topOfStack=NULL;
}
bool isEmpty()const{
return topOfStack == NULL;
}
void push(const Object& item){
topOfStack = new Node<Object>(item,topOfStack);
}
void pop(){
if(isEmpty()) throw "Stack is Empty";
Node<Object> *tmp = topOfStack;
topOfStack = topOfStack->next;
delete tmp;
}
const Object& top()const{
if(isEmpty()) throw "Stack is Empty";
return topOfStack->item;
}
void makeEmpty(){
while(!isEmpty()) pop();
}
~Stack(){
makeEmpty();
}
};
#endif