18-General Stack

Source: Internet
Author: User
Import Java. lang. reflect. array;/*** generic stack ** @ Param <t> */public class Stack <t> {private class <t> type; // Private int size of the class to which the stack element belongs; // stack depth private T [] arr; // use an array to store private int top; // The subscript public stack (class <t> type, int size) of the top element of the stack {This. type = type; this. size = size; arr = createarray (size); Top =-1;}/*** create an array * @ Param size * @ return */@ suppresswarnings ("unchecked ") private T [] createarray (INT size) {r Eturn (T []) array. newinstance (type, size);}/*** pressure stack * @ Param T */Public void push (t) {top ++; arr [Top] = T ;} /*** output stack ** @ return */Public t POP () {T = arr [Top]; top --; return t ;} /*** get the top element of the stack * @ return */Public t PEEK () {return arr [Top];} /*** determine whether the stack is empty * @ return */Public Boolean isempty () {return Top =-1 ;} /*** determine whether the stack is full * @ return */Public Boolean isfull () {return Top = (size-1);} public St Atic void main (string [] ARGs) {stack <string> S = new stack <string> (string. class, 100); S. push ("with perseverance as a good friend"); S. push ("experience-based adviser"); S. push ("be careful as Brother"); S. push ("as expected"); While (! S. isempty () {string STR = S. Pop (); system. Out. println (STR );}}}

Output:

Hope as the Sentinel, be careful as the brother, use experience as the adviser, and take perseverance as a good friend

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.