Java simple stack implementation

Source: Internet
Author: User
Package COM. handy. DS; public class simplestack {// Stack Array private int [] arrays; // stack capacity private int capacity; // Number of stack elements Private int size; // automatically increase the size private int autosize = 5; Public simplestack (INT capacity) {size = 0; this. capacity = capacity; arrays = new int [capacity];}/*** @ return the arrays */Public int [] getarrays () {return arrays ;} /*** @ Param arrays * The arrays to set */Public void setarrays (INT [] arrays) {T His. arrays = arrays;}/*** @ return the capacity */Public int getcapacity () {return capacity ;} /*** @ Param capacity * the capacity to set */Public void setcapacity (INT capacity) {This. capacity = capacity;}/*** @ return the size */Public int getsize () {return size ;} /*** @ Param size * the size to set */Public void setsize (INT size) {This. size = size;} // The growth function public void extend (INT increment) {int [] Ex Tendstack = new int [capacity + increment]; capacity = capacity + increment; For (INT I = 0; I <size; I ++) extendstack [I] = arrays [I]; arrays = extendstack;} // returns the top element of the stack and changes the stack state to public int POP () {If (isempty () = true) Return-1; elsereturn arrays [-- size];} // public void push (INT number) {If (isfull () = true) {system. out. println ("the stack is full! "); // Capacity = capacity + autosize; // arrays = new int [capacity]; Extend (autosize); system. Out. println (" automatic growth! "+" Size: "+ autosize); arrays [size ++] = number;} elsearrays [size ++] = number ;} // determine whether the stack is empty public Boolean isempty () {If (size = 0) return true; elsereturn false;} // determine whether the stack is full public Boolean isfull () {If (size = capacity) return true; elsereturn false;} // gets the top element of the stack, but does not change the stack state public int stacktop () {If (isempty () = false) return arrays [size-1]; elsereturn-1 ;}}

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.