Data structure (Java language)--stack simple implementation

Source: Internet
Author: User

A stack is a table that restricts insertions and deletions to only one location, which is the end of the table, called the top of the stack. The basic operation of the stack is a stack push and a pop, which is equivalent to inserting, the latter being the last inserted element. The stack is sometimes called FIFO first-out table.

Because the stack operation is a constant time, the stack does not have a noticeable improvement unless in exceptional cases. The first way to implement a stack is to use a single-linked list to implement push by inserting it at the top of the table, and to implement pop by removing the top element of the table. The top operation simply returns the value of the top element. The second way to do this is to use arrays, avoiding chains and being a more popular solution. Stack of the stack of Topofstack to point to the expression, for the empty stack the value is-1. To push an element x into the stack, we make topofstack add 1 and then set theitems[topofstack]=x. To eject the top element of the stack, pop () returns Theitems[topofstack] and Topofstack minus 1.

These operations are not only run as constants, but also run at very fast constant times. On some machines, the push and pop of integers can be written as a machine instruction if they operate on registers with self-increment and self-reducing addressing functions. Modern computers use stack operations as part of the instruction system, so the stack is probably the most basic data structure behind the array.

The following is a stack implemented with arrays, structures and arrays, but simplifies operations where the main function is used as a test:

Import Java.util.iterator;import Java.util.nosuchelementexception;public class Mystack<anytype> implements iterable<anytype> {private static final int default_capacity = 10;private int thesize;private anytype[] Theitems;pr ivate int Topofstack;public Mystack () {clear ();} public void Clear () {thesize = 0;topofstack = -1;ensurecapacity (default_capacity);} public int size () {return thesize;} public Boolean isEmpty () {return size () = = 0;} public void Trumtosize () {ensurecapacity (size ());} @SuppressWarnings ("unchecked") public void ensurecapacity (int newcapacity) {if (Newcapacity < size ()) {return;} anytype[] Old = Theitems;theitems = (anytype[]) new object[newcapacity];for (int i = 0; I <= topofstack; i++) {Theitems [i] = old[i];} Thesize = newcapacity;} Public AnyType Top () {if (size () = = 0) {throw new NullPointerException ();} return theitems[topofstack];} Public AnyType Pop () {if (size () = = 0) {throw new NullPointerException ();} return theitems[topofstack--];} public void push (AnyType x) {if (topofstack + 1 = = size ()) {ensurecapacity (Size () * 2 + 1);} Theitems[++topofstack] = x;} @Overridepublic iterator<anytype> Iterator () {return new Stackiterator ();} Private class Stackiterator implements iterator<anytype> {private int current = 0;public Boolean hasnext () {return C Urrent <= Topofstack;} Public AnyType Next () {if (!hasnext ()) {throw new Nosuchelementexception ();} return theitems[current++];}} public static void Main (string[] args) {mystack<integer> stack = new mystack<integer> (); Stack.push (1); Stack.push (2); Stack.push (3); Stack.pop (); Stack.push (4); Stack.push (5);iterator<integer> Iterator = Stack.iterator (); while (Iterator.hasnext ()) {System.out.print (Iterator.next () + "");}}}
Output Result:

1 2 4 5

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure (Java language)--stack simple implementation

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.