File tree Expand file tree Collapse file tree
source/chpt_5_StacksQueuesDeques Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include < string>
4+ using namespace std ;
5+
6+ class RuntimeException {
7+ public:
8+ RuntimeException (const string& error) {
9+ errorMessage = error;
10+ }
11+ string getMessage () const {
12+ return errorMessage;
13+ }
14+
15+ private:
16+ string errorMessage;
17+ };
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ template <typename E> class ArrayStack {
4+ public:
5+ ArrayStack (int capacity = DEFAULT_CAPACITY);
6+ int size () const ;
7+ bool empty () const ;
8+ const E &top () const ;
9+ void push (const E &element);
10+ void pop ();
11+
12+ private:
13+ enum { DEFAULT_CAPACITY = 100 };
14+ E *m_stack;
15+ int m_capacity;
16+ int m_topIndex;
17+ };
18+
19+ template <typename E>
20+ ArrayStack<E>::ArrayStack(int capacity)
21+ : m_stack(new E[capacity]), m_capacity(capacity), m_topIndex(-1 ) {}
22+
23+ template <typename E>
24+ int ArrayStack<E>::size() const {
25+ return (m_topIndex + 1 );
26+ }
27+
28+ template <typename E>
29+ bool ArrayStack<E>::empty() const {
30+ return (m_topIndex < 0 );
31+ }
32+
33+ template <typename E>
34+ const E &ArrayStack<E>::top() const {
35+ if (empty ()) {
36+ throw " Top of empty stack" ;
37+ }
38+ return m_stack[m_topIndex];
39+ }
40+
41+ template <typename E>
42+ void ArrayStack<E>::push(const E &element) {
43+ if (size () == m_capacity) {
44+ throw " Stack full" ;
45+ }
46+ m_stack[++m_topIndex] = element;
47+ }
48+
49+ template <typename E>
50+ void ArrayStack<E>::pop() {
51+ if (empty ()) {
52+ throw " Pop for empty stack" ;
53+ }
54+ m_topIndex--;
55+ }
Original file line number Diff line number Diff line change 1+ #include " ArrayStack.h"
2+ #include < iostream>
3+ #include < string>
4+
5+ bool parenthesesMatch (const std::string &expression) {
6+
7+ char openParentheses = ' (' ;
8+ char closeParentheses = ' )' ;
9+
10+ ArrayStack<char > stack;
11+
12+ for (int i = 0 ; i < expression.size (); i++) {
13+ char e = expression.at (i);
14+
15+ if (e == openParentheses) {
16+ stack.push (e);
17+ } else if (e == closeParentheses) {
18+ if (stack.empty ()) {
19+ return false ;
20+ }
21+ if (stack.top () != openParentheses) {
22+ return false ;
23+ }
24+ stack.pop ();
25+ }
26+ }
27+ if (stack.empty ()) {
28+ return true ;
29+ } else {
30+ return false ;
31+ }
32+ }
33+
34+ void printValid (const std::string &expression) {
35+ std::string result;
36+ if (parenthesesMatch (expression)) {
37+ result = " is valid\n " ;
38+ } else {
39+ result = " is not valid\n " ;
40+ }
41+ std::cout << expression << " " << result;
42+ }
43+
44+ int main (int argc, char const *argv[]) {
45+
46+ printValid (" (1+2)*2" );
47+ printValid (" (1+2))*2" );
48+
49+ return 0 ;
50+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ template <typename E> class ArrayStack {
4+ public:
5+ ArrayStack (int capacity = DEFAULT_CAPACITY);
6+ int size () const ;
7+ bool empty () const ;
8+ const E &top () const ;
9+ void push (const E &element);
10+ void pop ();
11+
12+ private:
13+ enum { DEFAULT_CAPACITY = 100 };
14+ E *m_stack;
15+ int m_capacity;
16+ int m_topIndex;
17+ };
18+
19+ template <typename E>
20+ ArrayStack<E>::ArrayStack(int capacity)
21+ : m_stack(new E[capacity]), m_capacity(capacity), m_topIndex(-1 ) {}
22+
23+ template <typename E>
24+ int ArrayStack<E>::size() const {
25+ return (m_topIndex + 1 );
26+ }
27+
28+ template <typename E>
29+ bool ArrayStack<E>::empty() const {
30+ return (m_topIndex < 0 );
31+ }
32+
33+ template <typename E>
34+ const E &ArrayStack<E>::top() const {
35+ if (empty ()) {
36+ throw " Top of empty stack" ;
37+ }
38+ return m_stack[m_topIndex];
39+ }
40+
41+ template <typename E>
42+ void ArrayStack<E>::push(const E &element) {
43+ if (size () == m_capacity) {
44+ throw " Stack full" ;
45+ }
46+ m_stack[++m_topIndex] = element;
47+ }
48+
49+ template <typename E>
50+ void ArrayStack<E>::pop() {
51+ if (empty ()) {
52+ throw " Pop for empty stack" ;
53+ }
54+ m_topIndex--;
55+ }
Original file line number Diff line number Diff line change 1+ #include " ArrayStack.h"
2+ #include < iostream>
3+
4+ int main (int argc, char const *argv[]) {
5+
6+ ArrayStack<int > arrayStack;
7+
8+ std::cout << arrayStack.empty () << std::endl;
9+
10+ return 0 ;
11+ }
You can’t perform that action at this time.
0 commit comments