下面介绍三种实现stack的方法。尽管java中有stack类,但是已经被废弃了。自己动手实现!!
1. 用链表实现
package com.jzm.stackQueueTree;
public class LinkedStack<T> {
private Node topNode; //引用链表中的第一个节点
public LinkedStack(){
clear();
}//end default constructor
private class Node{
private T data; //栈的元素
private Node next; //指向下一个节点的连接
private Node(T data){
this.data = data;
next = null;
}//end constructor
private T getData(){
return data;
}
private Node getNextNode(){
return next;
}
}
private boolean isEmpty(){
return topNode == null;
}
private void clear(){
topNode = null;
}
public void push(T newEntry){
Node newNode = new Node(newEntry);
newNode.next = topNode;
topNode = newNode;
}
public T pop(){
T top = null;
if(!isEmpty()){
top = topNode.getData();
topNode = topNode.getNextNode();
}
return top;
}
public void display(){
Node point = topNode;
System.out.println("从顶端元素开始:");
while(point != null){
System.out.println(point.getData());
point = point.next;
}
}
public static void main(String[] args) {
LinkedStack<Integer> linkedStack = new LinkedStack<Integer>();
for (int i = 0; i < 100; i++){
linkedStack.push(i);
if (i %2 ==0) {
linkedStack.pop();
}
}
linkedStack.display();
}
}
2. 用数组实现
package com.jzm.stackQueueTree;
public class ArrayStack<T> {
private T[] a; //用于存放值的数组
private int topIndex; //栈的顶端索引
private final static int MAX = 500; //默认栈大小
public ArrayStack(){
clear();
a = (T[]) new Object [MAX];
}
public ArrayStack(int size){ //自定义栈大小
clear();
a = (T[])new Object[size];
}
private boolean isFull(){
return (topIndex >= a.length-1);
}
public void clear(){ //清空栈 topIndex = -1;
a = null;
}
public void push(T newEntry){
if (!isFull()) {
a[++topIndex] = newEntry;
}else {
System.out.println("栈满了 topIndex="+topIndex);
}
}
public T pop(){
T top = null;
if(topIndex >= 0){
top = a[topIndex];
topIndex--;
}else{
System.out.println("栈为空");
}
return top;
}
public void display(){
System.out.println("ArrayStack从顶端元素开始:");
for(int i=topIndex; i>=0; i--){
System.out.println(a[i]);
}
}
public static void main(String[] args) {
ArrayStack<Integer> arrayStack = new ArrayStack<Integer>();
for (int i = 0; i < 100; i++){
arrayStack.push(i);
if (i %2 == 0){
arrayStack.pop();
}
} //end for
arrayStack.display();
}
}
3. 用vector实现
package com.jzm.stackQueueTree;
import java.util.Vector;
public class VectorStack<T>{
private Vector <T> stack;
public VectorStack (){
stack = new Vector<T>();
}
public VectorStack (int maxsize){
stack = new Vector<T>(maxsize);
}
public void push(T newEntry){
stack.addElement(newEntry);
}
public T pop(){
T top = null;
if (!isEmpty()) {
top = stack.lastElement();
stack.removeElementAt(stack.size()-1);
}
return top;
}
private boolean isEmpty(){
return stack.isEmpty();
}
public void display(){
System.out.println("vectorStack从顶端元素开始:");
for (int i = stack.size()-1; i>=0; i--) {
System.out.println(stack.elementAt(i));
}
}
public static void main(String[] args) {
VectorStack<Integer> vectorStack = new VectorStack<Integer>();
for (int i = 0; i < 100; i++){
vectorStack.push(i);
if (i %2 == 0){
vectorStack.pop();
}
} //end for
vectorStack.display();
}
}
本文介绍了三种不同的栈实现方式:使用链表、数组和Vector。每种实现都详细展示了其核心代码,包括基本操作如压栈、弹栈及检查栈是否为空等。
5469

被折叠的 条评论
为什么被折叠?



