系统的堆栈
package l3;
import java.util.Stack;
/**
*
* @ClassName: StackDemo
* @Description: TODO(系统堆栈)
* @author chen_jf
* @date 2017年4月27日 上午9:39:14
*
*/
public class StackDemo {
public static void main(String[] args) {
Stack stack = new Stack();
stack.push("第一个元素");
stack.push("第二个元素");
stack.push("第三个元素");
stack.push("第四个元素");
stack.push("第五个元素");
System.out.println("堆栈中是否为空:" + stack.empty());
System.out.println("栈顶元素为:" + stack.peek());
System.out.println("第五个元素离栈顶的距离为:" + stack.search("第五个元素"));
System.out.println("------将堆栈中的数据取出-----");
System.out.println("-----stack.size()-------" + stack.size());
System.out.println("-----stack.capacity()-------" + stack.capacity());
while (!stack.empty()) {
System.out.println(stack.pop());
}
System.out.println("-----stack.size()-------" + stack.size());
System.out.println("-----stack.capacity()-------" + stack.capacity());
System.out.println("堆栈中是否为空:" + stack.empty());
}
}
输出:
堆栈中是否为空:false
栈顶元素为:第五个元素
第五个离栈顶的距离为:1
------将堆栈中的数据取出-----
-----stack.size()-------5
-----stack.capacity()-------10
第五个元素
第四个元素
第三个元素
第二个元素
第一个元素
-----stack.size()-------0
-----stack.capacity()-------10
堆栈中是否为空:true
用链表模拟堆栈
package l3;
/**
*
* @ClassName: MyStack
* @Description: TODO(这里用一句话描述这个类的作用)
* @author chen_jf
* @date 2017年4月26日 下午4:59:22
*
*/
public class MyStack {
Node head;
int size;
public MyStack() {
head = new Node();
}
/**
*
* @Title: push
* @Description: TODO(增加节点)
* @param @param data 参数说明
* @return void 返回类型
* @throws
*/
public void push(String data) {
Node p = head;
Node d = new Node(data);
d.next = p.next;
p.next = d;
size++;
}
/**
*
* @Title: pop
* @Description: TODO(删除节点)
* @param @return 参数说明
* @return Node 返回类型
* @throws
*/
public String pop() {
if (isEmpty()) {
System.out.println("堆栈已经为空");
return null;
}
Node p = head;
Node temp = p.next;
p.next = p.next.next;
size--;
return temp.name;
}
/**
*
* @Title: isEmpty
* @Description: TODO(判断堆栈是否为空)
* @param @return 参数说明
* @return boolean 返回类型
* @throws
*/
public boolean isEmpty() {
boolean isNull = false;
if (head.next == null) {
isNull = true;
}
return isNull;
}
/**
*
* @Title: size
* @Description: TODO(判断大小)
* @param @return 参数说明
* @return int 返回类型
* @throws
*/
public int size() {
return size;
}
/**
*
* @Title: search
* @Description: TODO(查找节点)
* @param @param data
* @param @return 参数说明
* @return String 返回类型
* @throws
*/
public String search(String data) {
Node p = head;
while (p.next != null) {
if (p.next.name.equals(data)) {
return data;
} else {
p.next = p.next.next;
}
}
return null;
}
/**
*
* @Title: peek
* @Description: TODO(获取栈顶元素)
* @param @return 参数说明
* @return String 返回类型
* @throws
*/
public String peek() {
if (isEmpty()) {
return null;
}
return head.next.name;
}
}
package l3;
/**
*
* @ClassName: Node
* @Description: TODO(节点类)
* @author chen_jf
* @date 2017年4月27日 上午10:08:34
*
*/
public class Node {
String name;
Node next;
public Node() {
this.name = "";
next = null;
}
public Node(String name) {
super();
this.name = name;
next = null;
// System.out.println("生成节点:" + name);
}
}
package l3;
/**
*
* @ClassName: TestStack
* @Description: TODO(测试模拟堆栈)
* @author chen_jf
* @date 2017年4月27日 上午10:08:47
*
*/
public class TestStack {
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push("第一个元素");
stack.push("第二个元素");
stack.push("第三个元素");
stack.push("第四个元素");
stack.push("第五个元素");
System.out.println("堆栈中是否为空:" + stack.isEmpty());
System.out.println("堆栈中最顶端元素是:" + stack.peek());
System.out.println(stack.search("第五个元素"));
System.out.println("------将堆栈中的数据取出");
System.out.println("-----stack.size()-------" + stack.size());
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
System.out.println("-----stack.size()-------" + stack.size());
}
}
输出:
堆栈中是否为空:false
堆栈中最顶端元素是:第五个元素
第五个元素
------将堆栈中的数据取出
-----stack.size()-------5
第五个元素
第四个元素
第三个元素
第二个元素
第一个元素
-----stack.size()-------0
package l4;
/**
*
* @ClassName: StackArray
* @Description: TODO(这里用一句话描述这个类的作用)
* @author chen_jf
* @date 2017年4月26日 下午5:29:02
*
*/
public class StackArray {
Object[] data;
int top = -1;
public StackArray() {
data = new Object[100];
}
public StackArray(int size) {
data = new Object[size];
}
/**
*
* @Title: isEmpty
* @Description: TODO(判断是否为空)
* @param @return 参数说明
* @return boolean 返回类型
* @throws
*/
public boolean isEmpty() {
boolean isEmpty = false;
if (top == -1) {
isEmpty = true;
}
return isEmpty;
}
/**
*
* @Title: isFull
* @Description: TODO(判断是否已满)
* @param @return 参数说明
* @return boolean 返回类型
* @throws
*/
public boolean isFull() {
boolean isFull = false;
if (top + 1 == data.length) {
isFull = true;
}
return isFull;
}
/**
*
* @Title: push
* @Description: TODO(入栈)
* @param @param da 参数说明
* @return void 返回类型
* @throws
*/
public void push(String da) {
if (isFull()) {
System.out.println("堆栈已满");
return;
}
top++;
data[top] = da;
}
/**
*
* @Title: pop
* @Description: TODO(出栈)
* @param @return 参数说明
* @return Object 返回类型
* @throws
*/
public Object pop() {
if (isEmpty()) {
System.out.println("堆栈已为空");
return null;
}
return data[top--];
}
/**
*
* @Title: peek
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @return 参数说明
* @return Object 返回类型
* @throws
*/
public Object peek() {
if (isEmpty()) {
System.out.println("堆栈已为空");
return null;
}
return data[top];
}
/**
*
* @Title: size
* @Description: TODO(获取堆栈大小)
* @param @return 参数说明
* @return int 返回类型
* @throws
*/
public int size() {
return top + 1;
}
/**
*
* @Title: search
* @Description: TODO(查找节点)
* @param @param data
* @param @return 参数说明
* @return String 返回类型
* @throws
*/
public String search(String da){
for(int i = 0;i
package l4;
public class Test {
public static void main(String[] args) {
StackArray stack = new StackArray();
stack.push("第一个元素");
stack.push("第二个元素");
stack.push("第三个元素");
stack.push("第四个元素");
stack.push("第五个元素");
System.out.println("堆栈中是否为空:" + stack.isEmpty());
System.out.println("堆栈中最顶端元素是:" + stack.peek());
System.out.println(stack.search("第五个元素"));
System.out.println("------将堆栈中的数据取出");
System.out.println("-----stack.size()-------" + stack.size());
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
System.out.println("-----stack.size()-------" + stack.size());
}
}
输出:
堆栈中是否为空:false
堆栈中最顶端元素是:第五个元素
第五个元素
------将堆栈中的数据取出
-----stack.size()-------5
第五个元素
第四个元素
第三个元素
第二个元素
第一个元素
-----stack.size()-------0