自定义栈Stack 和 队列Queue

2019-08-16 09:58:24来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

自定义栈Stack 和 队列Queue

自定义栈

 

接口

package com.test.custom;

public interface IStack<E> {
     E pop();
     void push(E e);
     E peek();
     int size();
     boolean empty();
     void clear();
}

实现类

package com.test.custom;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;

public class CustomStack<E> implements IStack<E>{
    
    List<E> list =new ArrayList<E>();
    

    @Override
    public E pop() {
        // TODO Auto-generated method stub
        E e = peek();
        list.remove(list.size()-1);
        
        return e;
    }

    @Override
    public void push(E e) {
        // TODO Auto-generated method stub
        list.add(e);
    }

    @Override
    public E peek() {
        // TODO Auto-generated method stub
        int len = list.size();
        if(len==0){
            throw new EmptyStackException();
        }
        return list.get(len-1);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public boolean empty() {
        // TODO Auto-generated method stub
        return size()==0;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub
        list.clear();
    }



}

 

测试

package com.test.custom;

public class TestStack {
     public static void main(String[] args) {
         
        
        CustomStack<String> stack =new CustomStack<String>();
        for(int i =0 ; i < 4 ; i++){
            stack.push(""+(char)('A'+i));
        }


        System.out.println(stack.peek());
        System.out.println(stack.size());
        System.out.println(stack.pop());
        System.out.println(stack.size());
        System.out.println(stack.peek());
        stack.push("hello");
        System.out.println(stack.size());
        for(int i=0;i<5;i++){
        System.out.println(stack.pop());
        System.out.println(stack.size());
        }

    }
}

 

 


自定义队列

接口

package com.test.custom;

public interface IQueue<E> {
     boolean offer(E e); //超过容量就抛出 IllegalStateException
     E peek();//获取列头不移除,队列为null返回null
     E poll(); //获取并且移除列头
     int size();
     boolean isEmpty();

}

 

package com.test.custom;

import java.util.ArrayList;
import java.util.List;

public class CustomQueue<E> implements IQueue<E>{
    
    List<E> list =new ArrayList<E>();
    private int size;
    @Override
    public boolean offer(E e) {
        // TODO Auto-generated method stub
        if(list.size()>=size){
            throw new IllegalArgumentException();
        }
        list.add(e);
        return true;
    }

    @Override
    public E peek() {
        // TODO Auto-generated method stub

        if(size() == 0) return null;
        return list.get(0);
    }

    @Override
    public E poll() {
        // TODO Auto-generated method stub
        E e = peek();
        if(size()>0) list.remove(0);
        return e;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return list.size()==0;
    }

    public CustomQueue(int size) {

        this.size = size;
    }


    
     
}

测试

package com.test.custom;

public class TestQueue {
      public static void main(String[] args) {
          CustomQueue<String> stack =new CustomQueue<String>(4);
          for(int i =0 ; i < 4 ; i++){
              stack.offer(""+(char)('A'+i));
          }


          System.out.println(stack.peek());
          System.out.println(stack.size());
          
          System.out.println(stack.poll());
          System.out.println(stack.size());
          
          System.out.println(stack.peek());
          System.out.println(stack.size());
          
          stack.offer("hello");
          System.out.println(stack.size());
          
          for(int i=0;i<5;i++){
          System.out.println("i:"+stack.poll());

          }
          
          for(int i =0 ; i < 5 ; i++){
              stack.offer(""+(char)('A'+i));
          }

      }

}

 

 


原文链接:https://www.cnblogs.com/tk55/p/11152903.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java竞争抢答器

下一篇:java实现文字转语音功能(仅Windows)