Skip to content

Commit 99892c9

Browse files
committed
Stack 100%
1 parent 55c2ed2 commit 99892c9

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
# Add your projects from 'data-structures'
19-
project: [LinkedList, DoublyLinkedList, Queue]
19+
project: [LinkedList, DoublyLinkedList, Queue, Stack]
2020

2121
steps:
2222
# Step 1: Checkout the repository
+57-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1-
#include "../include/Stack.h"
1+
#include "../include/Stack.h"
2+
#include <cassert>
3+
#include <iostream>
4+
5+
6+
int main(int argc, char* argv[]){
7+
Stack<int> stk;
8+
assert(stk.empty() == true);
9+
assert(stk.size() == 0);
10+
try{
11+
stk.pop();
12+
std::cerr << "pop() throw an exception FAILED!" << std::endl;
13+
} catch(const std::underflow_error &e){
14+
std::cout << "pop() throw an exception PASSED! " << e.what() << std::endl;
15+
}
16+
try{
17+
stk.top();
18+
std::cerr << "top() throw an exception FAILED!" << std::endl;
19+
} catch(const std::underflow_error &e){
20+
std::cout << "top() throw an exception PASSED! " << e.what() << std::endl;
21+
}
22+
stk.push(12);
23+
assert(stk.empty() == false);
24+
assert(stk.size() == 1);
25+
assert(stk.top() == 12);
26+
stk.push(7); // 7 12
27+
assert(stk.empty() == false);
28+
assert(stk.size() == 2);
29+
assert(stk.top() == 7);
30+
stk.pop(); // 12
31+
assert(stk.empty() == false);
32+
assert(stk.size() == 1);
33+
assert(stk.top() == 12);
34+
stk.push(9); // 9 12
35+
assert(stk.empty() == false);
36+
assert(stk.size() == 2);
37+
assert(stk.top() == 9);
38+
stk.pop();
39+
stk.pop();
40+
assert(stk.empty() == true);
41+
assert(stk.size() == 0);
42+
try{
43+
stk.pop();
44+
std::cerr << "pop() throw an exception FAILED!" << std::endl;
45+
} catch(const std::underflow_error &e){
46+
std::cout << "pop() throw an exception PASSED! " << e.what() << std::endl;
47+
}
48+
try{
49+
stk.top();
50+
std::cerr << "top() throw an exception FAILED!" << std::endl;
51+
} catch(const std::underflow_error &e){
52+
std::cout << "top() throw an exception PASSED! " << e.what() << std::endl;
53+
}
54+
std::cout << "ALL TEST PASSED!" << std::endl;
55+
56+
return 0;
57+
}

data-structures/Stack/include/Stack.h

+37-4
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,51 @@ template <typename T>
1313
class Stack{
1414
public:
1515
// Capacity
16-
bool empty();
17-
size_t size();
16+
bool empty() const;
17+
size_t size() const;
1818
// Add
19-
void push(T& value);
19+
void push(const T& value);
2020
// Delete
2121
void pop();
2222
// Check
23-
void top();
23+
T& top();
2424
private:
2525
LinkedList<T> list;
2626
};
2727

28+
// Capacity
29+
template <typename T>
30+
bool Stack<T>::empty() const{
31+
return list.empty();
32+
}
33+
34+
template <typename T>
35+
size_t Stack<T>::size() const{
36+
return list.size();
37+
}
38+
39+
// Add
40+
template <typename T>
41+
void Stack<T>::push(const T& value){
42+
list.push_front(value);
43+
}
44+
45+
// Delete
46+
template <typename T>
47+
void Stack<T>::pop(){
48+
if(empty()){
49+
throw std::underflow_error("This Stack is empty");
50+
}
51+
list.pop_front();
52+
}
2853

54+
// Check
55+
template <typename T>
56+
T& Stack<T>::top(){
57+
if(list.empty()){
58+
throw std::underflow_error("This Stack is empty");
59+
}
60+
return list.front();
61+
}
2962

3063
#endif // STACK_H

0 commit comments

Comments
 (0)