Skip to content

Instantly share code, notes, and snippets.

@benapie
Last active January 12, 2019 11:31
Show Gist options
  • Save benapie/b27ed3aec99538f2dfe2cb61dd53efb0 to your computer and use it in GitHub Desktop.
Save benapie/b27ed3aec99538f2dfe2cb61dd53efb0 to your computer and use it in GitHub Desktop.
Implementation of a stack data type using arrays in C++.
#define MAX_SIZE 32
/**
* Implementation of a stack data type using an array.
*/
class Stack {
private:
int data_array[MAX_SIZE] = {};
int size = 0;
public:
Stack () = default;
/**
* Inserts a new element to the top of the stack.
* @param data the data of the new element.
*/
void Push(int data) {
if (size == MAX_SIZE) {
return;
}
data_array[size] = data;
size++;
}
/**
* Removes an element from the top of the stack and returns it.
* @return the data of the element removed.
*/
int Pop() {
if (size == 0) {
std::cerr << "Stack empty." << std::endl;
return -1;
}
size--;
return data_array[size];
}
/**
* Returns the top element without removing it.
* @return the data of the top element.
*/
int get_top() {
if (size == 0) {
std::cerr << "Stack empty." << std::endl;
}
return data_array[size - 1];
}
/**
* Returns the size of the list.
* @return the size of the list.
*/
int get_size() {
return size;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment