FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Stack_Scratch/src/stack.cpp at main · Build-X-From-Scratch/Stack_Scratch · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Build-X-From-Scratch
/
Stack_Scratch
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
1
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Stack_Scratch
/
src
/
stack.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (83 loc) · 2.13 KB
Breadcrumbs
Stack_Scratch
/
src
/
stack.cpp
Copy path
File metadata and controls
84 lines (83 loc) · 2.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
iostream
>
#
include
<
cstddef
>
#
include
<
stdexcept
>
//
template <int size,std::size_t capacity>
class
Stack
{
//
data member abstraction
private:
int
capacity;
int
size;
int
* array;
//
constructor
public:
Stack
(){
this
->
size
=
0
;
this
->
capacity
=
1
;
this
->
array
=
new
int
[capacity];
}
Stack
(
int
capacity){
this
->
capacity
= capacity;
this
->
array
=
new
int
[capacity];
int
size =
0
;
}
~Stack
(){
delete[]
array;
}
public:
//
getter is_full
int
get_size
(){
return
this
->
size
;
}
int
get_capacity
(){
return
this
->
capacity
;
}
bool
is_full
(){
if
(size == capacity){
return
true
;
}
return
false
;
}
int
top
(){
return
this
->
array
[size -
1
];
}
//
getter
bool
is_empty
(){
if
(size ==
0
){
return
true
;
}
return
false
;
}
//
setter push
void
push
(
int
data){
if
(size == capacity){
std::cout <<
"
jumlah element dalam stack telah maksimal
"
<< std::endl;
throw
std::runtime_error
(
"
stack telah penuh
"
);
}
array[size] = data;
size++;
}
void
pop
(){
array[size -
1
] =
0
;
size--;
}
public:
//
method print
void
print_stack
(){
for
(
size_t
i =
0
;i < size;i++){
std::cout << array[i] <<
"
"
;
}
std::cout << std::endl;
}
};
int
main
(){
Stack
stack1
(
100
);
stack1.
push
(
1
);
stack1.
push
(
2
);
stack1.
push
(
3
);
std::cout <<
"
isi stack saat ini
"
<< std::endl;
stack1.
print_stack
();
std::cout <<
"
Kapasitas maksimal Stack:
"
<< stack1.
get_capacity
() << std::endl;
std::cout <<
"
Size Array:
"
<< stack1.
get_size
() << std::endl;
std::cout <<
"
element paling atas:
"
<< stack1.
top
() << std::endl;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL