Self-Realization of stack structure in Java

Source: Internet
Author: User

Package com.pinjia.shop.common.collection;/** * Created by Wangwei on 2017/1/3.        */public class mylinkedstack<t> {//define node data structure private class Node{public T data;        Public Node next;            Public node () {} public node (T data,node Next) {this.data = data;        This.next = next;    }}//stack top element Private Node top;    Number of elements private int size;        Insert element public void push (T element) {top = new Node (element,top);    size++;        }//out of stack operation public T pop () {Node oldNode = top;        top = top.next;        Release reference Oldnode.next = null;        size--;    Return oldnode.data;    }//returns the top element of the stack, but not the stack public T peek () {return top.data;    }//stack length public int length () {return size;    }//determine if the link stack is empty stack public boolean empty () {return size = = 0;        The public String toString () {///link stack is empty if (empty ()) return "[]"; Else{StringBuilder sb = new StriNgbuilder ("[");            For (Node current = top;current! = Null;current = Current.next) {sb.append (current.data.toString () + ",");            } int len = Sb.length ();        Return sb.delete (len-2,len). append ("]"). toString (); }} public static void main (string[] Args) {mylinkedstack<string> stack = new mylinkedstack<string&        Gt; ();        Constantly into the stack Stack.push ("aaaa");        Stack.push ("bbbb");        Stack.push ("cccc");        Stack.push ("dddd");        System.out.println (stack);        Access stack top element System.out.println ("access stack top element:" + stack.peek ());        pops up an element System.out.println ("first pop-up stack top element:" + stack.pop ());        Pop an element again System.out.println ("the second pop-up stack top element:" + stack.pop ());    System.out.println ("stack after two pops:" + stack); }}

  

The following is an array-based implementation:

Package com.pinjia.shop.common.collection;/** * Created by Wangwei on 2017/1/3.    */public class myarraystack<t> {private object[] objs = new object[16];    private int size = 0;    public boolean isEmpty () {return size = = 0; public void Clear () {//to null the data in the array to facilitate GC reclamation for (int i = 0; i < size; i++) {objs[si        ze] = null;    } size = 0;    } public int length () {return size;        The public boolean push (T Data) {///determines whether an array expansion is required if (size >= Objs.length) {resize ();        } objs[size++] = data;    Return true;        }/** * Array expansion */private void resize () {object[] temp = new Object[objs.length * 3/2 + 1];            for (int i = 0; i < size; i++) {temp[i] = objs[i];        objs[i] = null;    } OBJS = temp;        Public T pop () {if (size = = 0) {return null;    } return (T) objs[--size]; } pUblic String toString () {StringBuilder sb = new StringBuilder ();        Sb.append ("myarraystack: [");            for (int i = 0; i < size; i++) {sb.append (objs[i].tostring ());            If (i! = Size-1) {sb.append (",");        }} Sb.append ("]");    return sb.tostring (); } public static void main (string[] Args) {myarraystack<integer> stack = new myarraystack<integer> ()        ;        Stack.push (12);        Stack.push (13);        System.out.println (stack.length ());    System.out.println (stack); }}

  

Self-Realization of stack structure in Java

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.