栈-顺序存储-Java实现
2012-02-16 17:12
330 查看
package com.wrh.lab.dataStructure.stackAndQueue; /** * the interface of the SeqStack * @author wrh * * @param <E> */ public interface Stack<E> { /** * push the element to the stack * @param element */ public void push(E element); /** * pop the element from the stack and * return the pop element */ public E pop(); /** * * @return the top value */ public E getTop(); /** * * @return true for empty and false for not empty */ public boolean isEmpty(); /** * clear the stack */ public void clear(); }
package com.wrh.lab.dataStructure.stackAndQueue; /** * * @author wrh * the stack node */ public class StackNode<E> { private E element; private StackNode<E> next; /** * constructor */ public StackNode() { element = null; next = null; } public StackNode(E element, StackNode<E> next) { this.element = element; this.next = next; } public E getElement() { return element; } /** * @param element */ public void setElement(E element) { this.element = element; } public StackNode<E> getNext() { return next; } public void setNext(StackNode<E> next) { this.next = next; } }
package com.wrh.lab.dataStructure.stackAndQueueImpl; import com.wrh.lab.dataStructure.stackAndQueue.Stack; public class SeqStackImpl<E> implements Stack<E> { private static final int defaultSize = 3; private int size; //max size of the stack private int top; //index for top object private E[] listArray; //array hoding stack objects private static final int MAX_SIZE = 7; /** * constructor init a stack for the size defaultSize */ public SeqStackImpl() { size = defaultSize; top = -1; listArray = (E[]) new Object[size]; } /** * constructor * init a stack for the size of size * @param size */ public SeqStackImpl(int size) { if (size < 1 || size > MAX_SIZE) { System.out.println("the size is not vailable"); } else { this.size = size; top = -1; listArray = (E[]) new Object[size]; } } @Override public void push(E element) { if (size < MAX_SIZE && top < size) { listArray[++top] = element; } else if (top < MAX_SIZE){ resizeList(listArray); listArray[++top] = element; } else { System.out.println("the stack is full! "); } } @Override public E pop() { if (isEmpty()) { //if the stack is empty System.out.println("the stack is empty"); return null; } else { return listArray[top--]; } } @Override public E getTop() { return listArray[top]; } /** * resize the list * @param entry */ public void resizeList(E[] list) { int resize = size * 3 / 2 +1; Object[] seqList_upp = new Object[resize]; System.arraycopy(list,0,seqList_upp,0,size); this.listArray = (E[]) seqList_upp; } @Override public boolean isEmpty() { return 0 == top; } @Override public void clear() { top = 0; } public static void main(String[] args) { Stack<Integer> s = new SeqStackImpl<Integer>(5); s.push(1); s.push(2); s.push(3); s.push(4); s.push(5); System.out.println(s.isEmpty()); System.out.println(s.pop()); System.out.println(s.pop()); s.clear(); System.out.println(s.pop()); } }
相关文章推荐
- 二叉树的顺序存储实现(Java)
- 使用java实现顺序存储的二叉树
- 数据结构(四)---栈的顺序存储的实现---java版
- java实现线性表的顺序存储操作
- 线性表的顺序存储及JAVA实现
- 数据结构:线性表的顺序存储--Java实现
- 顺序存储-数据结构-java实现
- 链表-顺序存储-Java实现
- Java使用顺序存储实现队列
- 线性表 顺序存储 链式存储 ---java实现
- 数据结构Java实现——④数组—>稀疏矩阵三元组顺序存储-->矩阵的倒置
- java实现线性表的顺序存储
- 二叉树的顺序存储及其Java实现
- 线性表的Java实现--顺序存储
- java实现数据结构中线性表的顺序存储
- 线性表java实现之顺序存储源码
- 线性表 顺序存储 链式存储 ---java实现
- 线性表的顺序存储之java实现
- 栈的Java实现(顺序存储实现与链式存储实现)
- 队列(顺序存储) java实现