栈实现之数组:
package StackArray;
//栈--基于数组顺序栈Java实现
//主要操作:
//创建一个空栈
//判断栈是否为空
//向栈中压入元素
//从栈顶弹出元素
//栈顶查看元素,并不移除该元素
//返回对象在栈中的位置
public class StackArrayTest<T> {
private Object[] data = null;
//栈的容量
private int MaxSize = 0;
//栈中元素的指针
private int top = -1;
//构造函数
public StackArrayTest(){
//栈中默认的容量为10
this.data = new Object[10];
this.MaxSize = 10;
}
public StackArrayTest(int initSize){
if(initSize < 0){
throw new RuntimeException("初始化大小不能小于0:"+initSize);
}else{
this.data = new Object[initSize];
this.MaxSize = initSize;
++top;
}
}
//判断栈是否为空
public boolean isEmpty(){
return top==-1?true:false;
}
//向栈中压入元素
public void puch(T element){
if(top == MaxSize-1){
System.out.println("栈中元素已满,无法入栈");
}else{
top++;
data[top] = element;
}
}
//从栈顶弹出元素
public T pop(){
if(top==-1){
throw new RuntimeException("栈为空!");
}else{
Object temp = data[top];
data[top] = null;
top--;
return (T) temp;
}
}
//栈顶查看元素,并不移除该元素
public T peek(){
if(top==-1){
throw new RuntimeException("栈为空!");
}else{
Object temp = data[top];
return (T) temp;
}
}
//返回对象在栈中的位置
public int search(T element){
//对元素从栈顶进行遍历
int i = top;
while(top!=-1){
if(peek()!= element){
top--;
}else{
break;
}
}
int resultIndex = top+1;
top = i;
return resultIndex;
}
public static void main(String[] args){
StackArrayTest stack = new StackArrayTest();
Object[] array1 = {1,3,5,4,7};
for(int i = 0;i<array1.length;i++){
stack.puch(array1[i]);
}
System.out.println(stack.peek());
int i = stack.search(5);
System.out.println(i);
stack.pop();
System.out.println(stack.peek());
stack.puch(111);
System.out.println(stack.peek());
}
}
栈实现之链表:
package StackArray;
//栈---链式存储
//创建空栈
//入栈
//弹出栈
//查看栈顶元素
//判断栈是否为空
//栈中元素显示
public class StackLinkedTest<E> {
private class Node<E>{
E e;
Node next;
public Node(E e,Node next){
this.e = e;
this.next = next;
}
public Node(){
}
}
private Node<E> top;
private int size = 0;
public StackLinkedTest(){
}
public StackLinkedTest(E e,Node next){
this.top.e = e;
this.top = next;
}
public boolean isEmpty(){
return size==0;
}
public void puch(E e){
//将入栈的元素放在链表的头节点top上,其新top.next指向原头节点上
top = new Node(e,top);
size++;
}
public E peek(){
if(isEmpty()){
throw new RuntimeException("栈为空!");
}else{
return top.e;
}
}
public E pop(){
if(isEmpty()){
throw new RuntimeException("栈为空!");
}else{
Node<E> temp = top;
top = temp.next;
temp.next = null;
size--;
return temp.e;
}
}
public String toString(){
//栈中元素显示
if(isEmpty()){
throw new RuntimeException("栈为空!");
}else{
//从top节点开始遍历
StringBuilder sb = new StringBuilder("[");
Node current = top;
for(int i = 0;i<size;i++,current=current.next){
sb.append(current.e.toString()+",");
}
int len = sb.length();
return sb.delete(len-1, len).append("]").toString();
}
}
public static void main(String[] args){
StackLinkedTest stack1 = new StackLinkedTest();
stack1.puch("你好");
stack1.puch("我好");
stack1.puch("大家好");
stack1.puch("哈哈");
System.out.println(stack1.toString());
stack1.pop();
System.out.println(stack1.toString());
Object e = stack1.peek();
System.out.println(e.toString());
}
}
代码获得最大、最小栈:
package GetMaxMinStackTest;
import java.util.Arrays;
import java.util.Stack;
//获取最大值、最小值的栈
//用一个辅助栈每次记录存入栈中的最大、小值,当取值时从辅助栈的栈顶出栈得到最大值或者最小值
public class GetStackMaxMin {
Stack stack1 = new Stack();
//存入最大值的辅助栈
Stack MaxStack = new Stack();
//存入最小值的辅助栈
Stack MinStack = new Stack();
//入栈
public void push(Integer e){
stack1.push(e);
if(MaxStack.isEmpty()||e.compareTo((Integer) MaxStack.peek())>0){
MaxStack.push(e);
}else{
MaxStack.push(MaxStack.peek());
}
if(MinStack.isEmpty()||e.compareTo((Integer) MinStack.peek())<0){
MinStack.push(e);
}else{
MinStack.push(MinStack.peek());
}
}
//出栈
public void pop(){
if(stack1.isEmpty()&&MaxStack.isEmpty()&&MinStack.isEmpty()){
stack1.pop();
MaxStack.pop();
MinStack.pop();
}
}
//获得最大值
public Object getMax(){
return MaxStack.peek();
}
//获得最小值
public Object getMin(){
return MinStack.peek();
}
public static void main(String[] args){
GetStackMaxMin gsmm = new GetStackMaxMin();
Object[] array1 = {5,4,7,2,9};
for(int i = 0;i<array1.length;i++){
gsmm.push(Integer.valueOf(array1[i].toString()));
}
System.out.println(gsmm.getMax());
System.out.println(gsmm.getMin());
}
}