Name: Pankaj G. Kabra Class: Mca 3 Year
Name: Pankaj G. Kabra Class: Mca 3 Year
Stack is a abstract datatype By using which we can stores the values But is characterized by only three fundamental operations:
push, pop and stack top. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. The pop operation removes an item from the top of the stack. The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state can also occur in stack top operation if stack is empty.
import java.util.*; public class Stack { private List items; public Stack(int size) { items = new ArrayList(); } public void push(Object item) { items.add(item);
}
public Object pop() { if (items.size() == 0) throw new EmptyStackException(); return items.remove(items.size() - 1); } public boolean isEmpty() { return items.isEmpty(); } }