This repository was archived by the owner on Feb 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.cpp
More file actions
85 lines (60 loc) · 1.41 KB
/
Stack.cpp
File metadata and controls
85 lines (60 loc) · 1.41 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
#include "Stack.h"
#include <iostream>
Stack::Stack(){
this->buffer = new int[1];
this->cNoe = 0;
}
Stack::~Stack(){
delete[] this->buffer;
}
Stack::Stack(const Stack &s){
this->cNoe = s.cNoe;
this->buffer = new int(s.cNoe);
for(int i = 0; i < this->cNoe; i++){
this->buffer[i] = s.buffer[i];
}
}
void Stack::push(int value){
this->buffer[this->cNoe] = value;
this->addElement();
}
int Stack::pop(){
if(this->isEmpty()) return -1;
int retVal = this->buffer[this->cNoe - 1];
this->removeElement();
return retVal;
}
int Stack::peek() const{
if(this->isEmpty()) return -1;
return this->buffer[this->cNoe - 1];
}
bool Stack::isEmpty() const{
return (this->cNoe < 1);
}
int Stack::getSize() const{
return this->cNoe;
}
void Stack::addElement(){
this->cNoe += 1;
int* temp = new int[this->cNoe];
for(int i = 0; i < this->cNoe; i++){
temp[i] = this->buffer[i];
}
this->buffer = new int[this->cNoe + 1];
for(int i = 0; i < this->cNoe; i++){
this->buffer[i] = temp[i];
}
delete[] temp;
}
void Stack::removeElement(){
this->cNoe -= 1;
int* temp = new int[this->cNoe];
for(int i = 0; i < this->cNoe; i++){
temp[i] = this->buffer[i];
}
this->buffer = new int[this->cNoe + 1];
for(int i = 0; i < this->cNoe; i++){
this->buffer[i] = temp[i];
}
delete[] temp;
}