[Java data structure] 3.1 sequential Stack

Source: Internet
Author: User

Stack

Definition:

Stack) is a linear table of Last in first out, LIFO). It must be deleted and inserted only at the end of the table.

The figure is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15533IF0-0.jpg "title =" qq 30522193220.jpg "/>

Features:

I. Stack special linear table sequence table and linked list). It has some special requirements and restrictions on operations: the elements of the stack must be "post-in-first-out ".

3. the table at the end of the stack is called the top of the stack), and the corresponding header is called the bottom)

2. Stack operations can only be performed at the end of the linear table.


Implementation of the sequence Stack:

The time complexity of the push (), pop (), and peek () methods is O (1). When the stack capacity needs to be expanded, the time complexity of the push () method is O (n)

Package com. ds. stack; import java. util. Arrays; public class Stack <T> {// Java array implementation. You can use a java Generic Array. To use this function, use the private Object [] stack container provided by Java. // The default initial size of the stack is private final static int INIT_SIZE = 2. // The private int index of the top stack index; /*** default constructor ** the initial size of the Stack is default */public Stack () {this. stack = new Object [INIT_SIZE]; index =-1;}/*** constructor ** @ param initSize * Initial Stack size */public stack (int initSize) {if (initSize <0) throw new IllegalArgumentException (); this. stack = new Object [initSize]; index =-1 ;}/*** Stack exit Operation ** @ return top stack object */public synchronized T pop () {// return this. top =-1? Null :( T) this. element [this. top --]; if (! IsEmpty () {T temp = peek (); stack [index --] = null; return temp;} return null ;} /*** out stack operation ** @ return top stack object */public synchronized void push (T item) {if (isFull () {// if the stack is full, the created stack Object [] temp = stack; temp = new Object [2 * stack. length]; stack = Arrays. copyOf (temp, temp. length);} stack [++ index] = item;}/*** view stack top object ** @ return stack top object */public T peek () {// return this. top =-1? Null: (T) this. element [this. top]; if (! IsEmpty () {return (T) stack [index];} return null;}/*** check whether the stack is empty ** @ return true if the stack is empty, otherwise, false */public boolean isEmpty () {return index =-1;}/*** check whether the stack is full ** @ return returns true if the stack is full, otherwise, false */public boolean isFull () {return index = stack. length-1;} public static void main (String [] args) {Stack <Integer> stack = new Stack <Integer> (3); for (int I = 0; I <5; I ++) {stack. push (I);} System. out. println ("stack. top: "+ stack. peek ());}}


If you have any questions, feel free to contact us!


This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1288757

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.