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. The position 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 is the deletion of the last inserted Element.

The stack is sometimes called FIFO first-out table.

Because the stack operation is a constant time. therefore, unless in exceptional cases, the stack does not produce significant improvements.

The first way to implement a stack is to use a single-linked List. Push is implemented by inserting at the top of the table to implement pop by removing the top element of the Table. The top operation simply returns the value of the top Element. Another way to do this is to use arrays, avoid chaining and be 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 performed not only with constants, but also in very fast constant time. 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 important data structure behind the ARRAY.

The following is a stack implemented with arrays, and the structures and arrays are very similar. But the operation is simplified, and 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

Data structure (java Language)--stack simple implementation

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.