Java implementation chain Stack

Source: Internet
Author: User

The Java implementation sequence stack was learned earlier: http://www.cnblogs.com/lixiaolun/p/4644134.html

Next, learn Java to implement the chain stack.

Chain Stack Class Code:

Package Linkedstack;public class Linkstack {private Element base;private element top;class element{public Object data;pub Lic Element next; /** * Initialize stack * */void initstack () {top = new element (); base = new Element (); top.data=null;top.next=base;base.data=null;base. Next=null;} /** * into the stack * */void push (Object o) {Element e = new Element (); e.data = O;if (top.next==base)//first-time into stack operation {e.next=base;top.next=e;} Else{e.next=top.next;top.next=e;}} /** * out of Stack * */void pop () {if (top.next==base) {System.out.println ("There are no elements in the stack!") ");} Else{system.out.println ("stack operation" +top.next.data); top.next=top.next.next;}} /** * Print Stack * */void print () {System.out.print ("Print stack:"); Element temp =top;while (temp.next!=base) {System.out.print (temp.next.data+ "\ t"); temp =temp.next;} System.out.println ();}}

Link stack test class code:

Package Linkedstack;public class Linkstackmain {public static void main (string[] args) {Linkstack lstack = new Linkstack () ; Lstack.initstack (); Lstack.pop (); Lstack.push (1); Lstack.push (2); Lstack.push (3); Lstack.push (4); Lstack.print (); Lstack.pop (); Lstack.pop (); Lstack.print (); Lstack.pop (); Lstack.pop (); Lstack.print (); Lstack.pop ();}}

  

Java implementation chain Stack

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.