Data structure--java Stack class

Source: Internet
Author: User

Defined

a stack is a subclass of a vector that implements a standard LIFO stack. The stack defines only the default constructor, which is used to create an empty stack. The stack also defines some of its own methods, in addition to all the methods defined by the vector.

Legend

You can see the process of entering the stack (push) and the stack (POP) in the picture below. Simply put, the stack has only one entry (exit), so the advanced post-out (LIFO) is not difficult to understand.

Common methods

serial number description
1 object push ()
2 object pop ()
3 object peek ()
4 boolean empty () test stack is empty.
5 int search () Returns the position of the object in the stack, with a base of 1.

Source

 PackageJava.util;/*** The <code>Stack</code> class represents a last-in-first-out * (LIFO) Stack of objects. It extends class <tt>Vector</tt> with five * operations-a Vector to be treated as a stack. The usual * <tt>push</tt> and <tt>pop</tt> operations is provided, as well as a * method to <t  T>peek</tt> at the top item in the stack, a method to test * for whether the stack is <tt>empty</tt> And a method to <tt>search</tt> * The stack for an item and discover. * <p> * When a stacks is first created, it contains no items. * * <p>a more complete and consistent set of LIFO stack operations are * provided by the {@linkDeque} interface and its implementations, which * should is used in preference to the This class. For example: * <pre> {@code* deque<integer> stack = new arraydeque<integer> ();} </pre> * *@authorJonathan Payne *@sinceJDK1.0*/ PublicclassStack<e>extendsVector<e> {    /*** Creates an empty Stack. */     PublicStack () {}/*** Pushes an item onto the top of this stack. This has exactly * the same effect as: * <blockquote><pre> * addelement (item) </pre></bloc kquote> * *@paramitem The item to is pushed onto this stack. * @returnThe <code>item</code> argument. * @seejava.util.vector#addelement*/     Publice push (E Item) {addelement (item); returnitem; }    /*** Removes the object at the top of this stack and returns that * object as the value of this function. *     * @returnThe object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throwsemptystackexception If this stack is empty. */     Public synchronizede pop () {e obj; intLen =size (); Obj=Peek (); Removeelementat (Len-1); returnobj; }    /*** Looks at the object at the top of the "this" stack without removing it * from the stack. *     * @returnThe object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throwsemptystackexception If this stack is empty. */     Public synchronizedE Peek () {intLen =size (); if(len = = 0)            Throw Newemptystackexception (); returnElementAt (len-1); }    /*** Tests If this stack is empty. *     * @return<code>true</code> if and only if the this stack contains * no items; <code>false</code     > otherwise. */     Public Booleanempty () {returnSize () = = 0; }    /*** Returns The 1-based position where an object is on this stack. * If The object <tt>o</tt> occurs as an item in this stack, this * method returns the distance from the P of the stack of the * occurrence nearest the top of the stack; The topmost item on the * stack are considered to being at distance <tt>1</tt>.     The <tt>equals</tt> * method is used to compare <tt>o</tt> to the * all items in this stack. *     * @paramo the desired object. * @returnThe 1-based position from the top of the stack where * the object is located; the return value <code     >-1</code> * Indicates that the object isn't on the stack. */     Public synchronized intsearch (Object o) {inti =lastIndexOf (o); if(I >= 0) {            returnSize ()-i; }        return-1; }    /**Use serialversionuid from JDK 1.0.2 for interoperability*/    Private Static Final LongSerialversionuid = 1224463164541339165L;}
View Code

The above source code from jdk1.8, if you need to download jdk1.8 official documents click here.

Reference: Http://www.runoob.com/java/java-stack-class.html https://www.cnblogs.com/leefreeman/archive/2013/05/16/3082400.html

Data structure--java Stack class

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.