标签:
1 package iYou.neugle.list; 2 3 public class MySeqStack<T> { 4 private Stack stack = new Stack(); 5 6 class Stack { 7 public int maxSize = 10; 8 public T[] data = (T[]) new Object[maxSize]; 9 public int top = -1;// 栈顶指针 10 } 11 12 // 初始化栈 13 public void SeqStackInit() { 14 this.stack.top = -1; 15 System.out.println("栈初始化成功!"); 16 } 17 18 // 入栈 19 public void SeqStackPush(T data) { 20 if (this.stack.top == this.stack.maxSize - 1) { 21 System.out.println("栈中内容已满!"); 22 return; 23 } 24 this.stack.data[++this.stack.top] = data; 25 } 26 27 // 出栈 28 public T SeqStackPop() { 29 if (this.stack.top == -1) { 30 System.out.println("栈中内容已空!"); 31 } 32 T data = this.stack.data[stack.top]; 33 this.stack.data[this.stack.top] = null; 34 this.stack.top--; 35 return data; 36 } 37 38 // 获取栈顶元素 39 public T SeqStackPeek() { 40 if (this.stack.top == -1) { 41 System.out.println("栈中内容已空!"); 42 return null; 43 } 44 return this.stack.data[this.stack.top]; 45 } 46 }
标签:
原文地址:http://www.cnblogs.com/niuxiaoha/p/4636146.html