java数据结构和算法2 栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表

栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表 


package com.yf.structure;

import java.util.Arrays;
import java.util.EmptyStackException;

public class ArrayStack {
   private Object[] elementData;
   private int top;
   private int size;
   public ArrayStack(){
      this.elementData=new Object[10];
      this.top=-1;
      this.size=10;
   }
   
   public ArrayStack(int initalCapacity){
      if(initalCapacity<0){
         throw new IllegalArgumentException("栈初始容量不能小于0"+initalCapacity);
      }
      this.elementData=new Object[initalCapacity];
      this.top=-1;
      this.size=initalCapacity;
   }

   /**
    * 是否需要扩容,如果需要,则扩大一倍并返回true
    */
   public boolean isGrow(int minCapacity){
      int oldCapacity=size;
      if(minCapacity>=oldCapacity){
         int newCapacity=0;
         //左移一位相当于*2
         if((oldCapacity<<1)-Integer.MAX_VALUE>0){
            newCapacity=Integer.MAX_VALUE;
         }else{
            newCapacity=(oldCapacity<<1);
         }
         this.size=newCapacity;
         int[] newArray=new int[size];
         elementData=Arrays.copyOf(elementData, size);
         return true;
      }else{
         return false;
      }
   }
   
   public Object push(Object item){
      isGrow(top+1);
      elementData[++top]=item; 
      return item;
   }
   
   public Object pop(){
      Object obj=peek();
      remove(top);
      return obj;
   }
   
   public Object peek(){
      if(top==-1){
         throw new EmptyStackException();
      }
      return elementData[top];
   }
   
   public void remove(int top){
      elementData[top]=null;
      this.top--;
   }
   
   public boolean isEmpty(){
      return (top==-1);
   }
   
   public int length(){
      return this.size;
   }
    
}


字符串反转:

package com.yf.structure;

public class testMain {

   public static void main(String[] args){
      ArrayStack arrays=new ArrayStack();
      String str="hello world";
      for(int i=0;i<str.length();i++){
         char ch=str.charAt(i);
         arrays.push(ch);
      }
      while(!arrays.isEmpty()){
         System.out.print(arrays.pop());
      }
       
   }
}

判断符号是否匹配:

package com.yf.structure;

public class testMain {

   public static void main(String[] args){
      ArrayStack arrays=new ArrayStack();
      String str="{[<(helloword>]}";
      boolean flag=true;
      for(int i=0;i<str.length();i++){
         char ch=str.charAt(i);
         if(ch=='{'||ch=='['||ch=='<'||ch=='('){
            arrays.push(ch);
         }
         if(ch=='>'||ch==']'||ch=='}'||ch==')'){
            char chp=arrays.pop().toString().charAt(0);
            if((ch=='>'&&chp=='<')||(ch==']'&&chp=='[')||(ch=='}'&&chp=='{')){
               flag=true;
            }else{
               flag=false;
               break;
            }
         }
      }
      if(flag){
         System.out.println("匹配");
      }else{
         System.out.println("不匹配");
      }
       
   }
}

猜你喜欢

转载自blog.csdn.net/weixin_40839342/article/details/80854353