顺序栈:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package com.mycompany.data.structure.line.stack;/** * * @author wyman */public class SequenceStack { public int base, top = 0; public int stacksiz = 0; public int STACK_INIT_SIZE = 100; public int STACKINCREMENT = 10; public Object[] stackArr; public SequenceStack(int stack_init_size, int stackincrement) { this.STACKINCREMENT = stackincrement; this.STACK_INIT_SIZE = stack_init_size; stackArr = new Object[STACK_INIT_SIZE]; stacksiz = STACK_INIT_SIZE; } public SequenceStack() { stackArr = new Object[STACK_INIT_SIZE]; stacksiz = STACK_INIT_SIZE; } public SequenceStack InitStack(int... size) { if (size.length == 2) { return new SequenceStack(size[0], size[1]); } else if (size == null) { return new SequenceStack(); } else { return null; } } public void destoryStack() { } public void ClearStack() { this.top = this.base; } public boolean StackEmpty() { if (this.top == this.base) { return true; } return false; } public int StackLength() { return this.top; } public Object getTop() { return stackArr[this.top]; } public void push(Object obj) { if (this.top == this.stacksiz) { Object[] newArr = new Object[this.stacksiz + this.STACKINCREMENT]; System.arraycopy(stackArr, 0, newArr, 0, stacksiz); this.stacksiz += this.STACKINCREMENT; stackArr = newArr; } stackArr[this.top] = obj; this.top++; } public Object pop() { if (this.top == this.base) { return null; } else { this.top--; return this.stackArr[this.top]; } } public void stackTraverse() { }}
链栈:
/** * * @author wyman */public class StackElem { public Object data; public StackElem next; public StackElem(Object data, StackElem next) { this.data = data; this.next = next; } public StackElem() { }}===========================================/** * * @author wyman */public class LinkStack { public int stacksiz = 0; public StackElem top; public StackElem base; public LinkStack() { this.top = new StackElem(); this.base = this.top; } public LinkStack InitStack() { return new LinkStack(); } public void destoryStack() { this.top = null; } public void ClearStack() { this.top = this.base; } public boolean StackEmpty() { if (this.top == this.base) { return true; } return false; } public int StackLength() { return stacksiz; } public Object getTop() { if (this.stacksiz > 0) { return this.top; } return null; } public void push(Object obj) { this.top.data=obj; StackElem se = new StackElem(); se.next=this.top; this.top = se; stacksiz++; } public Object pop() { if (this.stacksiz <= 0) { return null; } else { this.top = this.top.next; stacksiz--; return this.top; } } public void stackTraverse() { }}