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
|
#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);
bool is_full();
bool is_empty();
friend IntStack print_stack(IntStack* pstack);
};
IntStack print_stack(IntStack* pstack){
for (int i = 0; i < 100; i++){
//cout << pstack;
}
}
int main()
{
int arr[5] = {1,2,3,4,5};
IntStack stack;
stack.reset();
stack.push(9);
stack.pop();
stack.push(arr, 5);
stack.pop(arr, 5);
print_stack(&stack);
return 0;
}
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 << "error: stack is full, cannot push another int";
exit(1);
}
//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 << "error: stack is empty, cannot delete element";
exit(1);
}
//decrement to previous stack index then set it to NULL
--i;
st_arr[i] = NULL;
}
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++){} //figure out how to make this line unnecessary
}
bool IntStack::is_full(){
if(i == 100)
return true;
else
return false;
}
bool IntStack::is_empty(){
if(i != 0)
return false;
else
return true;
}
|