-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
123 lines (97 loc) · 1.62 KB
/
Stack.cpp
File metadata and controls
123 lines (97 loc) · 1.62 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;
Stack::Stack()
{
stack = new Card*[100];
capacity = 100;
count = 0;
}
Stack::Stack(int size)
{
stack = new Card*[size];
capacity = size;
count = 0;
}
Stack::Stack(const Stack& other)
{
count = other.count;
capacity = other.capacity;
stack = new Card*[capacity];
for (int i = 0; i < count; i++)
*stack[i] = *(other.stack[i]);
}
Stack::~Stack()
{
for (int i = 0; i < count; i++)
delete stack[i];
delete[] stack;
}
int Stack::getCount()
{
return count;
}
bool Stack::push(const Card& c)
{
if (!isFull())
{
stack[count] = new Card(c);
count++;
return true;
}
cout << "Stack is full!!!";
return false;
}
Card* Stack::pop()
{
if (!isEmpty())
{
count--;
return stack[count];
}
cout << "Stack is empty!!!";
return NULL;
}
Card* Stack::peek()
{
if (!isEmpty())
return stack[count];
cout << "Stack is empty!!!";
return NULL;
}
bool Stack::isEmpty()
{
return count == 0;
}
bool Stack::isFull()
{
return count == capacity;
}
Stack& Stack::operator=(const Stack& other)
{
if (this == &other)
return *this;
for (int i = 0; i < count; i++)
delete stack[i];
delete [] stack;
count = other.count;
capacity = other.capacity;
stack = new Card*[capacity];
for (int i = 0; i < count; i++)
*stack[i] = *(other.stack[i]);
return *this;
}
string Stack::display()
{
string str = "";
for (int i = count - 1; i >= 0; i--)
str += stack[i]->display() + "\n";
return str;
}
ostream& operator<<(ostream& out, const Stack& stack)
{
for (int i = stack.count - 1; i >= 0; i--)
out << *(stack.stack[i]) << endl;
return out;
}