Structure.Stack 栈(包含顺序栈、链表栈的实现)

来源:互联网 发布:极客学院源码下载 编辑:程序博客网 时间:2024/05/22 10:31

包含顺序栈、链表栈的实现

package Structure.Stack;/*Author: CPlusPlus小码农 *If any question,  *Please contact:            * http://daixiecplusplus.blog.163.com/ * QQ:1926742804 */public interface StackInterface<E extends Comparable<E>> {public void Push(E value);public void Pop();public E Top();public void MakeEmpty();}

package Structure.Stack;/*Author: CPlusPlus小码农 *If any question,  *Please contact:            * http://daixiecplusplus.blog.163.com/ * QQ:1926742804 */public class SeqStack<E extends Comparable<E>> implements StackInterface<E> { privateObject data_[];privateint top_;privateint maxSize_;public SeqStack(int stackSize){maxSize_ = stackSize;top_ = -1;data_ = new Object[maxSize_];}private boolean IsEmpty(){return top_ == -1;}private void DoubleSize(){Object temp[] = new Object[2*maxSize_];for(int i = 0 ; i <= top_ ; ++i){temp[i] = data_[i];}data_ = temp;maxSize_ *= 2;}@Overridepublic void Push(E value) {// TODO Auto-generated method stubif( top_ == maxSize_ -1){DoubleSize();}++top_;data_[top_] = value;}@Overridepublic void Pop() {// TODO Auto-generated method stubif(!IsEmpty())--top_;}@Overridepublic E Top() {// TODO Auto-generated method stubif(!IsEmpty())return (E)data_[top_];else return null;}@Overridepublic void MakeEmpty() {// TODO Auto-generated method stubtop_ = -1;}}

package Structure.Stack;/*Author: CPlusPlus小码农 *If any question,  *Please contact:            * http://daixiecplusplus.blog.163.com/ * QQ:1926742804 */class Node<E extends Comparable<E> >{Node<E> next_;E data_;public Node(E data,Node<E> next){data_ = data ; next_ = next;}}public class ListStack<E extends Comparable<E> > implements StackInterface<E> {private Node<E> top_;public ListStack(){top_ = null;}@Overridepublic void Push(E value) {// TODO Auto-generated method stubNode<E> n = new Node<E>(value,null);if(top_ == null){top_ = n;}else{n.next_ = top_;top_ = n;}}@Overridepublic void Pop() {// TODO Auto-generated method stubif(top_ != null){top_ = top_.next_;}}@Overridepublic E Top() {// TODO Auto-generated method stubreturn top_.data_;}@Overridepublic void MakeEmpty() {// TODO Auto-generated method stubtop_ = null;}}

package Structure.Stack;/*Author: CPlusPlus小码农 *If any question,  *Please contact:            * http://daixiecplusplus.blog.163.com/ * QQ:1926742804 */public class StackTest {public static void main(String[] args){StackInterface<Integer> s = new SeqStack<Integer>(5);s.Push(1);s.Push(2);s.Push(3);s.Push(4);s.Push(5);s.Push(6);s.Push(7);s.Push(8);Integer t = s.Top();System.out.println(t);s.Pop();s.Pop();s.Pop();t = s.Top();System.out.println(t);s.Push(1);s.Push(2);s.Push(3);s.Push(4);s.Push(5);s.Push(6);s.Push(7);s.Push(8);t = s.Top();System.out.println(t);System.out.println();System.out.println();StackInterface<Integer> s2 = new ListStack<Integer>();s2.Push(1);s2.Push(10);s2.Push(15);System.out.println(s2.Top());s2.Pop();System.out.println(s2.Top());s2.Pop();System.out.println(s2.Top());}}


0 0