java 实现简单的链式栈

package com.my;
/**
* 链式栈
* @author wanjn
*
*/
public class LinkedStack {
private Node head;
private int size;
//入栈,前插入构造链表,删除头结点实现栈;向后插入构造链表,删除头结点实现队列
public void push(Object value){
if (head ==null) {
head = new Node(null, value);
}else {
head = new Node(head, value);
}
}
//出栈
public Object pop(){
Object oldValue =null;
if (head!=null) {
oldValue = head.value;
head = head.next;
}
return oldValue; }
@Override
public String toString() {
Node temp = head;
String result= "";
while (temp!=null) {
result +=temp.value+" ,";
temp = temp.next;
}
return result;
}
private class Node{
Node next;
Object value;
Node(Node next,Object value){
this.next = next;
this.value = value;
}
}
}
上一篇:让innerHTML获取的内容包含input和select(option)的最新值


下一篇:可以ping通的打印机,没有办法打印