Imitation works of stack memory

 

Topic analysis shows:

  1 . Stack (stack) memory features:

   After a first-in data, the operation is always top of the stack of data stack.

  2 . Tool stored (set):

   The feature set implementation class, choose a set of LinkedList.

   LInkedList set of features: additions and deletions fast , slow queries.

   stack memory work is equivalent to a set of tools to add or delete data.

According to the analysis programming:

 

. 1  Package com.stack.demo;
 2  
. 3  Import java.util.LinkedList;
 . 4  
. 5  public  class Stack_LinkedList {
 . 6      public  static  void main (String [] args) {
 . 7          // test
 8          // create a stack of objects 
. 9          the LinkedList <Object > stack = Stack_LinkedList.stack;
 10      
. 11          // push (first in, last out) 
12 is          stack.push ( "Java"); // first push 
13 is          stack.push ( "JavaScript"); // second push 
14          stack.push ( "HTML");// third push 
15          stack.push ( "CSS"); // fourth push (in the stack)
 16          // Check the stack elements 
. 17          System.out.println (Stack);
 18 is          
. 19          // popping 
20 is          stack.pop (); // the top element from the stack
 21          // check the remaining elements, if "css" does not exist, the success of the analog 
22 is          System.out.println (stack);
 23 is                      
24      }
 25      // Create the LinkedList 
26 is      Private  static the LinkedList <Object> = Stack new new the LinkedList <> ();
 27      
28      // push 
29     public static<T> void push(T t) {
30         stack.addFirst(t);
31     } 
32     
33     //弹栈
34     public static<T> void pop() {
35         stack.removeFirst();
36     }    
37 }

 

Result of the program:

1 [css, html, javascript, java]
2 [html, javascript, java]

 

Guess you like

Origin www.cnblogs.com/Singlboy/p/10991382.html