JAVA 数据结构与算法之————栈
栈和队列的特性在本篇博客中不进行介绍,附上代码。
1, 以线性表作为栈的实现过程。
package stack_queue;
import java.util.ArrayList;
import java.util.List;
public class LineStack<E> {
private List<E> data = new ArrayList<E>();
private int top = -1;//站定指针
// 获取栈容量
public int size() {
return this.top + 1;
}
// 获取栈顶指针
public int getTop() {
return top;
}
// 判空
public boolean isEmpty() {
return top == -1 ? true : false;
}
// 进栈
public boolean push(E e) {
data.add(e);
top++;
return true;
}
// 查看栈顶元素,但不出栈
public E peek() {
if (top == -1) {
throw new RuntimeException("栈已空");
} else {
return (E) data.get(top);
}
}
// 出栈
public E pop() {
if (top == -1) {
throw new RuntimeException("栈已空,不能出栈");
} else {
return (E) data.get(top--);
}
}
}
测试:
public class Test {
public static void main(String[] args) {
// 进栈
lineStack.push(2);
// 查看栈顶元素但不出栈
System.out.println(lineStack.peek());
System.out.println(lineStack.isEmpty());
System.out.println(lineStack.size());
// 出栈
System.out.println(lineStack.pop());
System.out.println(lineStack.isEmpty());
System.out.println(lineStack.size());
}
2, 用链表实现栈
package stack_queue;
public class LinkStack<E> {
private int size; //栈容量
private Node top; //栈顶指针
// 结点
private class Node<E> {
E data;
Node<E> pre; //指向前一个节点的指针
public Node() {
}
public Node(E data, Node pre) {
this.data = data;
this.pre = pre;
}
}
//初始化栈
public LinkStack() {
this.size = 0;
top = null;
}
// 获取e
public E getNodeValue(Node n) {
return (E) n.data;
}
// 获取当前栈的长度
public int length() {
return size;
}
// 判空
public boolean isEmpty() {
if (size == 0) {
return true;
} else {
return false;
}
}
// 进栈
public boolean push(E data) {
top = new Node(data, top);
size++;
return true;
}
// 查看栈顶元素,但不出栈
public E peek() {
if (isEmpty()) {
throw new RuntimeException("栈空异常");
} else {
return getNodeValue(top);
}
}
// 出栈
public E pop() {
if (isEmpty()) {
throw new RuntimeException("栈空异常");
} else {
Node<E> value = top;
top = top.pre;
value.pre = null;
size--;
return getNodeValue(value);
}
}
}
测试:
package stack_queue;
public class Test {
public static void main(String[] args) {
LinkStack<Integer> linkStack = new LinkStack<>();
// 进栈
linkStack.push(2);
// 查看栈顶元素但不出栈
System.out.println(linkStack.peek());
System.out.println(linkStack.isEmpty());
System.out.println(linkStack.size());
// 出栈
System.out.println(linkStack.pop());
System.out.println(linkStack.isEmpty());
System.out.println(linkStack.size());
}
}
3,还有更为简单的方法来实现栈操作:用LinkedList实现栈
package stackandqueue;
import java.util.LinkedList;
public class LinkedStack<E> {
LinkedList<E> li = new LinkedList<>();
// 获取栈长度
public int size(){
return li.size();
}
// 判空
public boolean isEmpty(){
return li.isEmpty();
}
// 进栈
public boolean push(E e){
li.push(e);
return true;
}
// 出栈
public E pop(){
return li.pop();
}
// 查看栈顶元素,但不出栈
public E peek(){
return li.peekFirst();
}
// 测试
public static void main(String[] args) {
LinkedStack<Integer> stack = new LinkedStack<>();
stack.push(1);
stack.push(2);
System.out.println("size: " + stack.size());
System.out.println("peek: " + stack.peek());
System.out.println("size: " + stack.size());
System.out.println("pop: " + stack.pop());
System.out.println("size: " + stack.size());
System.out.println("isEmpty: " + stack.isEmpty());
}
}
代码拙劣,望大家多提意见,共同进步!