-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
77 lines (62 loc) · 1.1 KB
/
stack.cpp
File metadata and controls
77 lines (62 loc) · 1.1 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
70
71
72
73
//
// stack.cpp
// artunsarioglu_cs300_hw1
//
// Created by Artun on 10.07.2019.
// Copyright © 2019 Artun. All rights reserved.
//
#include <iostream>
#include "stack.h"
using namespace std;
/*
Constructor of Stack
*/
template<class T>
Stack<T>::Stack()
{
topOfStack = -1;
}
/*
Test whether the stack is logically empty
if stack is empty :
return true
return false
*/
template<class T>
bool Stack<T>::isEmpty()
{
return topOfStack == -1;
}
/*
Returns the most recently inserted item in the stack.
If stack is empty :
throw -> exception
*/
template<class T>
coordinate<T> Stack<T>::top()
{
if(isEmpty())
throw underflow_error("Stack is empty!");
return stack_array[topOfStack];
}
/*
Insert 'coor' into the stack
*/
template<class T>
void Stack<T>::push(coordinate<T> coor)
{
stack_array.push_back(coor);
++topOfStack;
}
/*
Removes the most recently inserted item
Exception if the stack is empty
*/
template<class T>
void Stack<T>::pop()
{
if(isEmpty())
throw underflow_error("Stack is empty!");
stack_array.pop_back();
topOfStack--;
}