Node.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javalearning6;
/**
*
* @author WEN
*/
public class Node {
int val;
Node next;
Node (int x) {
val = x;
next = null;
}
}
Stack.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javalearning6;
/**
*
* @author WEN
*/
public class Stack {
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.val);
top = top.next;
return temp;
}
}
public void push(Node n) {
if (n != null) {
n.next = top;
top = n;
}
}
}
Queue.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javalearning6;
/**
*
* @author WEN
*/
public class Queue {
Node first;
Node last;
public void enQueue(Node n) {
if (first == null) {
first = n;
last = first;
} else {
last.next = n;
last = n;
}
}
public Node deQueue() {
if (first == null) {
return null;
} else {
Node temp = new Node(first.val);
first = first.next;
return temp;
}
}
}