Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lab Stack
- public class MyStack
- {
- private int maxSize;
- private Object[] stackArray;
- private int top;
- public MyStack(int s)
- {
- maxSize = s;
- stackArray = new Object[maxSize];
- top = -1;
- }
- public void push(Object j)
- {
- stackArray[++top] = j;
- }
- public Object pop()
- {
- return stackArray[top--];
- }
- public Object peek()
- {
- return stackArray[top];
- }
- public boolean empty()
- {
- return (top == -1);
- }
- public boolean isFull()
- {
- return (top == maxSize - 1);
- }
- public Object search(int i)
- {
- return stackArray[i];
- }
- public static void main(String[] args)
- {
- MyStack theStack = new MyStack(10);
- theStack.push(10);
- theStack.push(20);
- theStack.push(30);
- theStack.push(40);
- theStack.push(50);
- while (!theStack.empty())
- {
- Object value = theStack.pop();
- System.out.print(value);
- System.out.print(" ");
- }
- System.out.println("");
- System.out.println("Find obj on index 2:" +theStack.search(2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement