栈——链表栈

在上一篇中用数组实现了“栈”,这次用链表实现。

代码如下:

 1 public class LinkedStack<E> {
 2 
 3     private int count;// 放入的个数
 4     private Node head; // 头节点
 5 
 6     /**
 7      * 放入元素
 8      *
 9      * @param e
10      */
11     public void push(E e) {
12         Node<E> node = new Node<>(e);
13         if (count == 0) {
14             this.head = node;
15         } else {
16             node.nextNode = this.head;
17             this.head = node;
18         }
19         count++;
20     }
21 
22     /**
23      * 获取元素
24      *
25      * @return
26      */
27     public E pop() {
28         if (count == 0)
29             throw new ArrayIndexOutOfBoundsException();
30         Node<E> node = this.head;
31         this.head = this.head.nextNode;
32         count--;
33         return node.getT();
34     }
35 
36     private class Node<T> {
37 
38         private T t;
39         private Node nextNode;
40 
41         public Node(T t) {
42             this.t = t;
43         }
44 
45         public T getT() {
46             return t;
47         }
48 
49         public void setT(T t) {
50             this.t = t;
51         }
52     }
53 }

 

转载于:https://www.cnblogs.com/shenqiaqia/p/10276361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值