Implementation of the stack of data Structures (JAVA) (i)

Source: Internet
Author: User

Stack top element public class stackelement<t>{    private stackelement<t>nextelement;    Private T Data;public stackelement (t data) {This.data=data;} Public stackelement<t> getnextelement () {return nextelement;} public void Setnextelement (stackelement<t> nextelement) {this.nextelement = nextelement;} Public T GetData () {return data;} public void SetData (T data) {this.data = data;}}


Tool Stack class:

public class seqstack<t>{Private stackelement<t> obj;//used to hold data private int size;//element number public seqstack () { this.size=0;} Add an Element public boolean push (T data) {if (data!=null) {stackelement<t>tempelement=new stackelement (data) to the top of the stack; Tempelement.setnextelement (this.obj); this.obj=tempelement;tempelement=null;//recovery This.size++;return true;} return false;} The top of the stack pops an element public boolean pop () {if (this.size>0) {this.obj=this.obj.getnextelement (); This.size--;return true;} return false;} Empty stack public void clear () {this.size=0;this.obj=null;} Get stack top element public T get () {return obj==null?null:obj.getdata ();} The size of the stack public int size () {return this.size;} public static void Main (String []arg] {seqstack<integer> selstack=new seqstack (); for (int i=0;i<10;i++) Selstack.push (i); System.out.println ("The size of the stack:" +selstack.size ()); int size=selstack.size (); for (int i=0;i<size;i++) { System.out.println (Selstack.get ()); Selstack.pop ();}}}
Operation Result:

Shortcomings, please correct me.

Implementation of the stack of data Structures (JAVA) (i)

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.