Stack implementation of data structures (JAVA) (I)

Source: Internet
Author: User

Stack implementation of data structures (JAVA) (I)

// Stack top element public class StackElement
 
  
{Private StackElement
  
   
NextElement; private T data; public StackElement (T data) {this. data = data;} public StackElement
   
    
GetNextElement () {return nextElement;} public void setNextElement (StackElement
    
     
NextElement) {this. nextElement = nextElement;} public T getData () {return data;} public void setData (T data) {this. data = data ;}}
    
   
  
 

 

Tool Stack:

 

Public class SeqStack
 
  
{Private StackElement
  
   
Obj; // private int size used to save data; // number of elements public SeqStack () {this. size = 0;} // Add an element public boolean push (T data) {if (data! = Null) {StackElement
   
    
TempElement = new StackElement (data); tempElement. setNextElement (this. obj); this. obj = tempElement; tempElement = null; // reclaim this. size ++; return true;} return false;} // a public boolean pop () {if (this. size> 0) {this. obj = this. obj. getNextElement (); this. size --; return true;} return false;} // clear the public void clear () {this. size = 0; this. obj = null;} // get the stack top element public T get () {return obj = null? Null: obj. getData () ;}// stack size public int size () {return this. size;} public static void main (String [] arg) {SeqStack
    
     
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
     
      
Running result:
      

 

 

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.