-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
59 lines (50 loc) · 917 Bytes
/
Stack.cpp
File metadata and controls
59 lines (50 loc) · 917 Bytes
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
//
// Stack.cpp
// pa3
//
// Created by Adam on 4/9/17.
// Copyright © 2017 Adam. All rights reserved.
//
#include <stdio.h>
#include "Stack.h"
Stack::Stack()
{
storage = new string[100];
capacity = 100;
top = -1;
};
Stack::Stack(int capacity)
{
if (capacity <= 0)
throw string("Stack's capacity must be positive");
storage = new string[capacity];
capacity = capacity;
top = -1;
};
void Stack::push(string value)
{
if (top == capacity)
throw string("Stack's underlying storage is overflow");
top++;
storage[top] = value;
}
void Stack::pop()
{
if (top == -1)
throw string("Stack is empty");
top--;
}
string Stack::peek()
{
if (top == -1)
throw string("Stack is empty");
return storage[top];
}
bool Stack::isEmpty()
{
return(top == -1);
}
int Stack::getCapacity()
{
return capacity;
}