-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTStack.h
More file actions
100 lines (90 loc) · 2.04 KB
/
Copy pathTStack.h
File metadata and controls
100 lines (90 loc) · 2.04 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Created by egorm on 25.10.2023.
//
#ifndef UNTITLED7_TSTACK_H
#define UNTITLED7_TSTACK_H
#include <iostream>
template<typename T>
class TStack {
private:
T *begin;
T *end;
int size;
public:
TStack(){
size = 100;
begin = new T[size];
end = reinterpret_cast<int>(begin);
}
TStack(T _n){
if(_n>=0){
size = _n;
begin = new T[size];
end = begin;
}else{
throw "Invalid Argument";
}
}
TStack(const TStack& s){
size = s.size;
begin = new T[size];
for(int i =0;i<size;++i){
begin[i] = s.begin[i];
}
end = begin + size;
}
~TStack(){
delete[] begin;
}
void push(T el){
int now_size = (end - begin);
std::cout<<now_size<<size<<std::endl;
if(now_size >= size){
T* tmp = new T[size*2];
delete[] begin;
for(int i = 0;i<size;++i){
tmp[i] = begin[i];
}
begin = tmp;
begin[size] = el;
std::cout<<"!!!"<<begin[size]<<std::endl;
end = begin +size+1;
size = size*2;
std::cout<<size<<std::endl;
}else{
int ind = (end - begin);
begin[ind] = el;
std::cout<<begin[ind]<<std::endl;
end++;
}
}
T top(){
if(!isEmpty()){
return *(end--);
}
}
void pop(){
end--;
}
bool isEmpty(){
return begin == end;
}
bool operator==(const TStack& s){
if(s.end - s.begin != end - begin){
return false;
}
for(int i =0;i<end-begin;++i){
if(begin[i] != s.begin[i]){
return false;
}
}
return true;
}
friend std::ostream& operator<<(std::ostream &out, const TStack &s){
for(size_t i = 0;i<s.end - s.begin;++i){
out<<s.begin[i];
}
return out;
}
};
#endif //UNTITLED7_TSTACK_H