基于顺序表的栈实现:
package StackExercise;
public class MyStack<T> {
// 最大尺寸
public static final int MAXSIZE = 20;
// 顶端
private int top;
// 所有的栈
private Object[] objs;
// 栈的最大值
private int max;
// 初始化
public MyStack() {
this(MyStack.MAXSIZE);
}
public MyStack(int size) {
if (size <= 0) {
throw new RuntimeException("初始化栈必须大于0!" + size);
} else {
objs = new Object[size];
top = -1;
max = size;
}
}
// 压入元素
public void push(T t) {
if (top == max - 1) {
throw new RuntimeException("栈以满!");
} else {
objs[++top] = t;
}
}
// 弹出顶部元素
public T pop() {
if (top == -1) {
throw new RuntimeException("栈为空!");
} else {
T ret = (T) objs[top];
objs[top--] = null;
return ret;
}
}
// 是否为空
public boolean isEmpty() {
return top == -1;
}
// 是否满
public boolean isFull() {
return top == max - 1;
}
// 查看顶部元素,不弹出
public T peek() {
if (top == -1) {
throw new RuntimeException("栈为空!");
} else {
return (T) objs[top];
}
}
public void print() {
for (int i = 0; i <= top; i++) {
System.err.println(objs[i]);
}
}
}
测试代码:
package StackExercise;
public class Main {
public static void main(String[] args) {
MyStack<Integer> t = new MyStack<Integer>();
for (int i = 0; i < 10; i++) {
t.push(i);
}
t.print();
for (int i = 0; i < 3; i++) {
t.pop();
}
t.print();
}
}
基于顺序表的链表实现:
package StackExercise;
public class LinkedStack<T> {
// 最大尺寸
public static final int MAXSIZE = 20;
// 所有的栈
public LinkedStackData<T> head;
// 栈的顶端
public int top;
// 栈的最大值
public int max;
// 初始化
public LinkedStack() {
this(LinkedStack.MAXSIZE);
}
public LinkedStack(int size) {
if (size <= 0) {
throw new RuntimeException("初始化栈必须大于0!" + size);
} else {
head = new LinkedStackData<T>();
top = -1;
max = size;
}
}
// 是否为空
public boolean isEmpty() {
return top == -1;
}
// 是否满
public boolean isFull() {
return top == max - 1;
}
// 查看顶部元素,不弹出
public T peek() {
if (top == -1) {
throw new RuntimeException("栈为空!");
} else {
return (T) (findTop().data);
}
}
// 弹出顶部元素
public T pop() {
T ret = null;
if (top == -1) {
throw new RuntimeException("栈为空!");
} else {
LinkedStackData<T> tData = head;
while (tData.next != null) {
if (tData.next.next == null) {
ret = tData.next.data;
tData.next = null;
break;
}
tData = tData.next;
}
}
return ret;
}
// 压入元素
public void push(T t) {
if (top == max - 1) {
throw new RuntimeException("栈以满!");
} else {
LinkedStackData<T> newData = new LinkedStackData<T>();
newData.data = t;
LinkedStackData<T> topData = findTop();
topData.next = newData;
top++;
}
}
// 寻找顶部元素
private LinkedStackData<T> findTop() {
LinkedStackData<T> tData = head;
while (tData.next != null) {
tData = tData.next;
}
return tData;
}
// 打印
public void print() {
LinkedStackData<T> i = head;
while (i.next != null) {
i = i.next;
System.err.println(i.data);
}
}
}
基础数据类:
package StackExercise;
public class LinkedStackData<T> {
// 下一个
public LinkedStackData<T> next;
// 当前数据
public T data;
}
测试代码:
package StackExercise;
public class Main {
public static void main(String[] args) {
LinkedStack<Integer> t = new LinkedStack<Integer>();
for (int i = 0; i < 10; i++) {
t.push(i);
}
t.print();
for (int i = 0; i < 3; i++) {
t.pop();
}
t.print();
}
}
以为这个会比链表东西会多一些,但是写起来感觉比链表好像要简单,也可能是方法少,但是理解起来可能对新手有一些难度吧。
网友评论