-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
81 lines (70 loc) · 1.54 KB
/
Stack.cpp
File metadata and controls
81 lines (70 loc) · 1.54 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
74
75
76
77
78
79
80
81
// Honor Pledge: ashstrin
// I pledge that I have neither given nor received any help
// on this assignment.
#include "Stack.h";
#include <stdexcept>
#include <cstddef>
#include <iostream>
template <typename T>
Stack <T>::Stack (void) : Array_Base<T>()
{
}
template <typename T>
Stack <T>::Stack(size_t length) : Array_Base<T>(length), top_(-1) //-1
{
}
//Stack
template <typename T>
Stack <T>::Stack (const Stack & stack) : Array_Base<T>(stack)
{
}
//push
template <typename T>
void Stack <T>::push (T element)
{
int num = ((top_ + 1) % Stack<T>::max_size_);
top_++;
if(top_ < Stack<T>::max_size_){
Array_Base<T>::set(num, element);
}else{
top_--;
throw std::overflow_error("Error - Stack is full");
}
}
// pop
template <typename T>
T Stack <T>::pop (void)
{
int num = ((top_ + 1) % Stack<T>::max_size_);
if(!is_empty()){
num = ((top_) % Stack<T>::max_size_);
top_--;
}else{
top_++;
throw std::underflow_error("Error - Stack is empty");
}
return Array_Base<T>::data_[num];
}
// operator =
template <typename T>
const Stack <T> & Stack <T>::operator = (const Stack & rhs)
{
if(this != &rhs){
//Array_Base<T>::~Array_Base();
Array_Base<T>::data_ = new T[rhs.max_size()];
Array_Base<T>::max_size_ = rhs.max_size();
Array_Base<T>::cur_size_ = rhs.size();
Array_Base<T>::data_ = rhs.data_;
top_ = rhs.top();
}
return *this;
}
// clear
template <typename T>
void Stack <T>::clear (void)
{
top_ = 0;
Array_Base<T>::cur_size_ = 0;
delete [] Array_Base<T>::data_;
Array_Base<T>::data_ = new T[Array_Base<T>::max_size_];
}