Java堆栈功能_【ThinkingInJava】35、用java实现堆栈功能

/**

* 书本:《Thinking In Java》

* 功能:用java实现堆栈功能

* 文件:LinkedStack.java

* 时间:2015年4月17日14:23:34

* 作者:cutter_point

*/

package Lesson15_generices;

public class LinkedStack

{

private static class Node

{

U item;

Node next;//下一个对象的引用

Node() { item = null; next = null; }//默认构造函数的都是空的

Node(U item, Node next)

{

this.item = item;

this.next = next;

}

boolean end() { return item == null && next == null; }

}

private Node top = new Node();//栈顶元素

public void push(T item)

{

top = new Node(item, top);//把这个新的元素赋值给栈顶引用

}

public T pop()

{

T result = top.item;//把栈顶的元素弹出去

if(!top.end())

top = top.next;

return result;

}

public static void main(String[] args)

{

LinkedStack lss = new LinkedStack();//创建一个堆栈类

for(String s : "My name is cutter_point!".split(" "))

{

lss.push(s);

}

String s;

while((s=lss.pop()) != null)

System.out.println(s);

}

}

输出:

cutter_point! is name My

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值