forked from noahc66260/C-PrimerPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack2.cpp
89 lines (79 loc) · 1.67 KB
/
stack2.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
// stack2.cpp -- implementation of Stack ADT class (stack2.h)
// This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata
// compile with pe12_4.cpp
#include "stack2.h"
#include<iostream> // for std::cerr
Stack::Stack(int n) // creates stack with n elements
{
if (n < 1)
{
std::cerr << "Error. Argument must be a positive integer. "
<< "Setting n to 10." << std::endl;
n = 10;
}
pitems = new Item[n];
size = top = 0;
}
Stack::Stack(const Stack & st)
{
pitems = new Item[st.size];
for (int i = 0; i < st.size; i++)
pitems[i] = (st.pitems)[i];
size = st.size;
top = st.top;
}
Stack::~Stack()
{
delete [] pitems;
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::push(const Item & item)
{
if (top < MAX)
{
pitems[top++] = item;
size++;
return true;
}
else
return false;
}
bool Stack::pop(Item & item)
{
if (top > 0)
{
item = pitems[--top];
size--;
return true;
}
else
return false;
}
Stack & Stack::operator=(const Stack & st)
{
if (this == &st)
return *this;
delete [] pitems;
size = st.size;
top = st.top;
pitems = new Item[size];
for (int i = 0; i < size; i++)
pitems[i] = (st.pitems)[i];
return *this;
}
std::ostream & operator<<(std::ostream & os, const Stack & s)
{
using std::endl;
for (int i = 0; i < s.top; i++)
{
os << "item " << i + 1 << ": " << (s.pitems)[i] << endl;
}
return os;
}