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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <iostream>
#include <stdlib.h>
using namespace std;
class IntStack{
static const int INITIAL_SIZE = 100;
int i;
int st_arr[INITIAL_SIZE];
public:
void reset();
void push(int n);
void push(int a[], size_t array_size);
int pop();
void pop(int a[], size_t n);
int top();
int size();
bool is_full();
bool is_empty();
friend IntStack print_stack(IntStack* pstack);
};
IntStack print_stack(IntStack* pstack){
int stop = 0;
for (int idx = 100; idx > 0; idx--){
if (pstack->st_arr[idx] != 0){
stop = idx;
idx = 0;
}
}
//cout << "STOP POINT IS STACK ELEMENT " << stop << "\n";
for (int i = 0; i <= stop; i++){
cout << pstack->st_arr[i] << endl;
//cout << "i: " << i << endl;
}
}
int main()
{
/*int arr[5] = {1,2,3,4,5};
IntStack stack;
stack.reset();
stack.push(arr, 5);
print_stack(&stack);
//cout << "\n";
//stack.pop(arr, 5);
//print_stack(&stack);
return 0;*/
IntStack stack;
stack.reset();
std::cout << "pushing 11\n";
stack.push(11);
std::cout << "is_empty: " << stack.is_empty() << "\n";
std::cout << "top: " << stack.top() << "\n";
std::cout << "pop: " << stack.pop() << "\n";
std::cout << "is_empty: " << stack.is_empty() << "\n";
std::cout << "pop: " << stack.pop() << "\n"; // this should result in "STACK EMPTY" output, or similar message
std::cout << "pop: " << stack.pop() << "\n"; // this should result in "STACK EMPTY" output, or similar message
std::cout << "is_empty: " << stack.is_empty() << "\n";
std::cout << "pushing 1\n";
stack.push(1);
std::cout << "pushing 2\n";
stack.push(2);
std::cout << "pushing 3\n";
stack.push(3);
std::cout << "pushing 4\n";
stack.push(4);
std::cout << "pop: " << stack.pop() << "\n";
std::cout << "pop: " << stack.pop() << "\n";
std::cout << "top: " << stack.top() << "\n";
std::cout << "pop: " << stack.pop() << "\n";
std::cout << "stack size: " << stack.size() << "\n";
stack.reset();
int arr[] = {0, 1, 2, 3, 4};
stack.push(arr, 5);
print_stack(&stack);
stack.pop(arr, 5);
std::cout << "stack size: " << stack.size() << "\n";
for(int i = 0; i < 5; ++i)
{
std::cout << arr[i] << "\n";
}
return 0;
}
int IntStack::top(){
i--;
int temp = i;
i++;
return st_arr[temp];
}
int IntStack::size(){
return i;
}
void IntStack::reset(){
//loop through the stack and set each element to NULL
for (i = 0; i < INITIAL_SIZE; i++){
st_arr[i] = NULL;
}
i = 0;
}
void IntStack::push(int n){
//if stack is full, int cannot be pushed, signal error
if (i == 100){
cerr << "STACK EMPTY";
}
//set current element of stack to pushed int then increment index
st_arr[i] = n;
i++;
}
void IntStack::push( int arr[], size_t array_size ){
for (int idx = 0; idx < array_size; idx++){
push(arr[idx]);
}
}
int IntStack::pop(){
//if stack is empty, int cannot be set to null, signal error
if (i == 0){
cerr << "STACK EMPTY\n";
}
else
//decrement to previous stack index then set it to NULL
--i;
int temp = st_arr[i];
st_arr[i] = NULL;
return temp;
}
void IntStack::pop( int a[], size_t n ){
i--;
for (int idx = 0; idx < n; idx++){
st_arr[i] = a[idx];
i--;
}
for (i = 0; st_arr[i] != NULL; i++);
}
bool IntStack::is_full(){
if(i == 100)
return true;
else
return false;
}
bool IntStack::is_empty(){
if(i != 0)
return false;
else
return true;
}
|