Implement stack structure with Java

Source: Internet
Author: User
Tags int size

Stack is an advanced data structure, the stack stack is the top element of the operation, the following is a simple stack structure using the Java language implementation
Class mystack{
private int size;//stack size
Private object[] elementdata;//elements in stack
private int top;//Stack top pointer

public MyStack(int size){    this.size = size;    this.top = 0;    this.elementData = new Object[size];}public boolean push(Object o){    if (ensureCapacity(top+1)){        top++;        elementData[top] = o;        return true;    }    return false;}public Object pop(){    if (top >= 0){        Object o = elementData[top];        elementData[top] = null;        top--;        return o;    }    return null;}public boolean isEmpty(){    return top == 0;}private boolean ensureCapacity(int capacity) {    if (capacity >= this.size){        return false;    }    return true;}

}

Implement stack structure with Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.