Repository navigation
Expand file tree
/
Copy pathStack.hpp
More file actions
69 lines (60 loc) · 1.48 KB
/
Copy pathStack.hpp
File metadata and controls
69 lines (60 loc) · 1.48 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef Stack_hpp
#define Stack_hpp
#include <iostream>
#include "Node.hpp"
template <class T>
class Stack{
private:
Node<T>* top;
int count;
public:
Stack();
void push (T value);
bool pop(T &value); //It returns true if the pop was successful, false if the stack is empty.
bool peek(T &value); //We cannot use T because if in case the stack is empty we cannot return False cause it expect a T type value. So now if it is not empty we are storing the value in the value variable(reference).
int size();
bool isEmpty();
};
template <class T>
Stack<T>::Stack(){
top = nullptr;
count = 0;
}
template <class T>
void Stack<T>::push(T value){
Node<T>* newNode = createNode(value);
newNode->next = top;
top = newNode;
count++;
}
template <class T>
bool Stack<T>::pop(T &value) {
if (top != nullptr) {
Node<T>* temp = top;
value = top->data;
top = top->next;
delete temp;
count--;
return true;
} else{
return false;
}
}
template <class T>
bool Stack<T>::peek(T &value){
if (top != nullptr){ // since value is reference variable it cannot be nullptr and hence we are giving this checking condition
value = top->data;
return true;
}else{
return false;
}
}
template <class T>
int Stack<T>::size(){
return count;
}
template <class T>
bool Stack<T>::isEmpty(){
return (count == 0)?true:false;
}
#endif