java stack chain to achieve

1. What is the link stack
1. The link stack structure is similar to the list and
2. Insert and delete operations are at the head of the list
3. That is: a link stack is in top of the first node, a single chain directed from the stack bottom of the stack
2. The advantages and disadvantages of chain stack
To solve the drawbacks of a fixed size stack space.
3. The realization of ideas and conventions
Since the link stack is essentially a single linked list, a linked list of nodes composed, so it is implemented by way of an internal class. Create a Node class, it defines the data for storing on a stack Node Next the first address String data node, and for storing the current stack; outside the class definition method Node Stack operation, stack, popping Wait. External internal class can not be instantiated class.
Stack 1. To define a class which COUNT, for counting the number of nodes in the stack, to define a Node top, is a reference to an object, for node points to the top.
2. incoming operation, the call stack method, it is necessary to apply a node with the Node-argument constructor method call, the incoming data into the data, and then the head top node address is assigned to the storage node.next, this new increase of node points to push it in front of a node, then the first address of the last new stack of nodes assigned to the top, so that the top is updated, point to a new head node.
3. popping operation, the top needs to point to the next node node popped, to note here is that we must define an intermediate variable for temporarily storing the current value of the top of the return stack data playing conflict.
Stack categories:
 1 package chainStack;
 2 /**
 3 * @类名  stack.java
 4 * @作者       修仙小华
 5 * @版本  V1.0
 6 * @日期  2019年7月24日-上午9:13:10
 7 * @描述  
 8 *
 9 */
10 public class Stack {
11     int count=0;
12     Node top;
13 
14     public void push(String data) {
15         Node node=new Node(data);
16         node.next=top;
17         count++;
18         top=node;
19     }
20     public String peek() {
21         return top.data;
22     }
23     public String pop() {
24         Node temp=top;
25         if (top.next==null) {
26             top=null;
27         }else {
28             top=top.next;
29         }
30         this.count--;
31         return temp.data;
32     }
33     public int size() {
34         return this.count;
35     }
36     public boolean isEmpaty() {
37         if (top==null) {
38             return true;
39         }else {
40             return false;
41         }
42     }
43     class Node{
44         String data;
45         Node next;
46         public Node() {}
47         public Node(String data) {
48             this.data=data;
49         }
50     }
51 }

测试类:

 1 public class StackTest {
 2     public static void main(String[] args) {
 3         Stack sta=new Stack();
 4         sta.push("张三");
 5         sta.push("李四");
 6         sta.push("王二");
 7         sta.push("麻子");
 8         System.out.println(sta.pop());
 9         System.out.println(sta.peek());
10         System.out.println(sta.isEmpaty());
11         System.out.println(sta.size());
12 
13     }
14 
15 }

 

 

Guess you like

Origin www.cnblogs.com/had1314/p/11267982.html