-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
89 lines (86 loc) · 1.75 KB
/
Stack.cpp
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
/*
Created by Jaden Kandel
Insertion time : O(1)
Deletion time : O(1)
Search time : O(n)
Access time : O(n)
*/
#include <iostream>
#include <string>
using namespace std;
class Stack{
private:
//top keeps track of
int top;
int arr[10];
public:
//constructor of the Stack, sets all values equal to 0
Stack()
{
top = -1;
for(int i=0; i < 10; i++){
arr[i] = 0;
}
}
//Checks if the stack is full
bool isfull()
{
if(top == 9)
return true;
else
return false;
}
//Checks if the stack is empty
bool isempty()
{
if(top == -1)
return true;
else
return false;
}
//Adds the value "n" on the top of the stack
void push(int n)
{
if(isfull())
{
cout << "The stack is full";
}
else
{
top++;
arr[top] = n;
}
}
//Removes the top value of the stack
void pop()
{
if(isempty())
{
cout << "Nothing to pop";
}
else
{
arr[top] = 0;
top--;
}
}
//Prints out the stack
void showStack()
{
cout << "top" << endl;
for(int i = 9; i > -1; i--){
cout << arr[i] << endl;
}
cout << "bottom" << endl;
}
};
int main()
{
Stack s;
s.push(5);
s.push(5);
s.push(7);
s.showStack();
s.pop();
s.showStack();
}