自定义栈
接口
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
时间: 2024-11-05 16:33:55