Java data structure and algorithm--stack __ data structure

Source: Internet
Author: User
Tags int size
Brief Introduction

Stack is a linear storage structure that has the following characteristics:
1. The data in the stack is in and out of the stack in accordance with the "LIFO (LIFO, last in-first)" mode.
2. When adding/deleting data to the stack, you can only operate from the top of the stack.

Stacks typically include three kinds of operations: Push, Peek, pop.
Push– adds elements to the stack.
peek– returns the top element of the stack.
pop– returns and deletes the operation of the top element of the stack. schematic diagram of the stack

The data in the stack in turn is 30–> 20–> stack

Before the stack: The top element of the stack is 30. In this case, the elements in the stack are 30–> 20–> 10 in turn
After the stack: 30 out of the stack, the top element of the stack becomes 20. At this point, the elements of the stack in turn are 20–> stack

Before the stack: The top element of the stack is 20. In this case, the elements in the stack are 20–> 10 in turn
After the stack: 40 into the stack, the top element of the stack becomes 40. At this point, the elements of the stack in turn are 40–> 20–> stack Java implementation

Java also provides an implementation of "stack", which is the Stack class in the collection framework.

This section gives 2 kinds of Java implementations
Java implementation of one: array implementation of the stack, can store any type of data.
Java Implementation II: Java Collection Collection of the "stack" (stack) sample.

1. Array implementation of the stack, can store any type of data

Implementation code (GENERALARRAYSTACK.JAVA)

Package com.qq.main;

Import Java.lang.reflect.Array;
    public class Generalarraystack<t> {private static final int default_size = 12;
    Private t[] Marray;

    private int count;
    Public Generalarraystack (class<t> type) {This (type, default_size);  @SuppressWarnings ("unchecked") public generalarraystack (class<t> type, int size) {Marray = (t[))
        Array.newinstance (type, default_size);
    Count = 0;
    public void push (T-t) {marray[count++] = T;
    Public T Peek () {return marray[count-1];
        Public T Pop () {t = marray[count-1];
        count--;
    return t;
    public int size () {return count;
    //Returns whether "stack" is an empty public boolean isempty () {return size () = 0; }//print "stack" public void Printarraystack () {if (IsEmpty ()) {System.out.printf ("The Stack is Empty
        \ n "); } System.out.printf ("StackSize () =%d\n ", size ());
        int I=size ()-1;
            while (i>=0) {System.out.println (marray[i]);
        i--;
        } public static void Main (string[] args) {String temp;

        generalarraystack<string> stack = new generalarraystack<string> (string.class);
        Stack.push ("10");
        Stack.push ("20");

        Stack.push ("30");
        Assign a stack top element to TMP and delete the top element temp = Stack.pop ();

        System.out.println ("tmp=" +temp);
        Assign the top of the stack to TMP only, and do not delete the element.
        temp = Stack.peek ();

        System.out.println ("tmp=" +temp); Stack.

    Printarraystack ();
 }
}

Run output results:

tmp=30
tmp=20
stack size () =3
10

The results show that Generalarraystack is a stack implemented through arrays, and generics are used in Generalarraystack.

Example of a "stack" (stack) in a 2.Java collection collection

Implementation code (STACKTEST.JAVA)

Package com.qq.main;

Import Java.util.Stack;

public class Stacktest {public

    static void Main (string[] args) {
        Integer temp = 0;
        stack<integer> stack = new stack<integer> ();

        Stack.push (ten);
        Stack.push (a);
        Stack.push (a);

        temp = Stack.pop ();
        System.out.printf ("temp=%d\n", temp);

        temp = Stack.peek ();
        System.out.printf ("temp=%d\n", temp);

        Stack.push ();

        while (!stack.isempty ()) {
            temp = Stack.pop ();
            System.out.printf ("temp=%d\n", temp);

        }

    }


Output results;

temp=30
temp=20
temp=40
temp=20
temp=10

So far, the understanding of the stack has a certain depth, can be used basically.

Learning Blog: http://www.cnblogs.com/skywang12345/p/3562239.html

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.