* @title Node.java
* @description 链表节点
* @date 2014-04-30 14:48
* @author lichunan
* @version 1.0
*/
public class Node {
private int val;
private Node next;
public Node(int val) {
this.val = val;
this.next = null;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
/**
* @title Stack.java
* @description 栈
* @date 2014-04-30 14:51
* @author lichunan
* @version 1.0
*/
public class Stack {
private Node top;
public Node peek(){
if(top != null){
return top;
}
return null;
}
public Node pop(){
if(top == null){
return null;
}else{
Node temp = new Node(top.getVal());
top = top.getNext();
return temp;
}
}
public void push(Node n){
if(n != null){
n.setNext(top);
top = n;
}
}
}
/**
* @title Main.java
* @description 测试主类
* @date 2014-04-30 14:59
* @author lichunan
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
Stack stack = new Stack();
stack.push(new Node(1));
stack.push(new Node(2));
stack.push(new Node(3));
System.out.println(stack.pop().getVal());
System.out.println(stack.pop().getVal());
System.out.println(stack.pop().getVal());
System.out.println(stack.pop());
}
}
package cn.lichunan.test;
/**
* @title Queue.java
* @description 队列
* @date 2014-04-30 22:00
* @author Administrator
* @version 1.0
*/
public class Queue {
private Node first;
private Node last;
public void enQueue(Node n){
if(last == null){
last = n;
first = last;
first.setNext(last);
}else{
last.setNext(n);
last = n;
}
}
public Node delQueue(){
if(first != null){
Node temp = new Node(first.getVal());
first = first.getNext();
return temp;
}
return null;
}
}
package cn.lichunan.test;
/**
*@title Main.java
*@description 测试主类
*@date 2014-04-30 22:16
*@author lichunan
*@version 1.0
*/
public class Main {
public static void main(String[] args) {
Queue queue = new Queue();
queue.enQueue(new Node(1));
queue.enQueue(new Node(2));
queue.enQueue(new Node(3));
queue.enQueue(new Node(4));
System.out.println(queue.delQueue().getVal());
System.out.println(queue.delQueue().getVal());
System.out.println(queue.delQueue().getVal());
System.out.println(queue.delQueue().getVal());
}
}