A stack for everyone
mohayemin opened this issue · 0 comments
mohayemin commented
Overview
The code below implements a very simple stack. It can only store integer data type. Can you change it so that it can store any data types?
// Language: Java
public class SimpleStack {
private int[] container;
private int topIndex;
public SimpleStack(int size) {
container = new int[size];
}
public void push(int item) {
topIndex++;
container[topIndex] = item;
}
public int pop() {
int item = container[topIndex--];
return item;
}
}
Assumptions
The implementation is intentionally simple. Adding additional validations are not focus of this problem.
Task
Change the code such a way that it can support any data type.
Reminders
- React to the problem if you find it interesting and helpful. This will help others to easily identify good problems to solve.
- Feel free to comment about the problem. Is the description unclear? Do you think it is too easy or too difficult than what is mentioned? Comment about it.
- Discussion about the solution is OK. But do not paste a solution here. Give a link to the solution instead.
- Do you have an interesting problem? Post it.