package com.datastructure.test;
public interface CustomStack {
//压栈方法
public void push(T data)throws Exception;
//弹栈/移除顶部元素,并返回对应的数据
public T pop()throws Exception;
//获取stact第一个元素
public T peek();
//判断栈是否为空
public boolean empty();
//返回栈中元素的个数
public int size();
}
package com.datastructure.test;
public class CustomArrayStack implements CustomStack {
static final int defaultSize = 15;
//指示顶部元素的位置
private int size;
private T[] arrays;
/*
* 无参构造方法,做一些初始化的操作
*/
@SuppressWarnings("unchecked")
public CustomArrayStack() {
size = 0;
arrays = (T[]) new Object[defaultSize];
}
/*
* 根据用户自定义数组大小初始化数组
*/
@SuppressWarnings("unchecked")
public CustomArrayStack(int customSize) {
size = 0;
arrays = (T[]) new Object[customSize];
}
/*
* 向栈顶添加元素
*/
@Override
public void push(T data) throws Exception {
if (size
package com.datastructure.test;
public class CustomArrayStackTest {
public static void main(String[] args) {
CustomArrayStack stack = new CustomArrayStack<>(10);
System.out.println("是否为空:"+stack.empty());
try {
stack.push("I");
stack.push("am");
stack.push("andy");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("栈顶元素:"+stack.size());
System.out.println("栈顶元素:"+stack.peek());
try {
System.out.println("移除元素:"+stack.pop());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("栈顶元素:"+stack.size());
System.out.println("栈顶元素:"+stack.peek());
}
}
是否为空:true
栈长度:3
栈顶元素:andy
移除元素:andy
栈长度:2
栈顶元素:am
package com.datastructure.test;
public class CustomLinkedStack implements CustomStack {
private int size;
private Node topNode;
@Override
public void push(T data) throws Exception {
Node newTopNode;
if (empty()) {
newTopNode = new Node(data, null);
}else {
newTopNode = new Node(data, topNode);
}
topNode = newTopNode;
size++;
}
@Override
public T pop() throws Exception {
Node oldTopNode = topNode;
topNode = topNode.nextNode;
size--;
return oldTopNode.data;
}
@Override
public T peek() {
return topNode.data;
}
@Override
public boolean empty() {
return size==0;
}
@Override
public int size() {
return size;
}
/**
* 节点内部类
*/
class Node{
private T data;
private Node nextNode;
public Node(T data,Node nextNode){
this.data = data;
this.nextNode = nextNode;
}
}
}
package com.datastructure.test;
public class CustomLinkedStackTest {
public static void main(String[] args) {
CustomLinkedStack stack = new CustomLinkedStack<>();
System.out.println("是否为空:"+stack.empty());
try {
stack.push("I");
stack.push("am");
stack.push("andy");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("栈长度:"+stack.size());
System.out.println("栈顶元素:"+stack.peek());
try {
System.out.println("移除元素:"+stack.pop());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("栈长度:"+stack.size());
System.out.println("栈顶元素:"+stack.peek());
}
}
package com.datastructure.test;
import java.util.LinkedList;
class CustomCollectionStack implements CustomStack {
private LinkedList linkedList = new LinkedList<>();
public CustomCollectionStack() {
}
@Override
public void push(T data) throws Exception {
linkedList.add(data);
}
@Override
public T pop() throws Exception {
return linkedList.removeLast();
}
@Override
public T peek() {
return linkedList.getLast();
}
@Override
public boolean empty() {
return linkedList.isEmpty();
}
@Override
public int size() {
return linkedList.size();
}
}
package com.datastructure.test;
public class CustomCollectionStackTest {
public static void main(String[] args) {
CustomCollectionStack stack = new CustomCollectionStack<>();
System.out.println("是否为空:"+stack.empty());
try {
stack.push("I");
stack.push("am");
stack.push("andy");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("栈长度:"+stack.size());
System.out.println("栈顶元素:"+stack.peek());
try {
System.out.println("移除元素:"+stack.pop());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("栈长度:"+stack.size());
System.out.println("栈顶元素:"+stack.peek());
}
}
(4)结果与2一致
文章:
Java数据结构与算法之学习路线
Java数据结构与算法之Array数组
Java数据结构与算法之LinkedList单链表