Linear table of data structure and algorithm analysis

Source: Internet
Author: User
Tags stub

1. Structure Catalog

:

Custom array out of bounds exception:

/** * @ClassName: OutOfBoundaryException * @Description: TODO  自定义的数组越界的异常* @author 萨摩耶* @date 2018年4月29日 下午3:37:11 *  */@SuppressWarnings("serial")public class OutOfBoundaryException extends Exception{    public OutOfBoundaryException(String message)    {        super(message);    }}

List interface:

/** * @ClassName: List * @Description: TODO 线性表的接口* @author 萨摩耶* @date 2018年4月29日 下午3:31:41 *  */public interface List {    //返回线性表的大小,即数据元素的个数    public int getSize();    //如果线性表为空返回true否则返回false    public boolean isEmpty();    //判断线性表中是否包含数据元素e    public boolean contains(Object e);    //返回数据元素e在线性表中的位置    public int indexOf(Object e);    //将数据元素e插入到线性表中i号位置    public void insert(int i,Object e)throws OutOfBoundaryException;    //将数据元素e插入数据元素obj之前    public boolean insertBefore(Object obj,Object e);    //经数据元素e插入数据元素obj之后    public boolean insertAfter(Object obj,Object e);    //移除位置i的元素    public Object remove(int i)throws OutOfBoundaryException;    //删除线性表中第一个与e相同的元素    public boolean remove(Object e);    //替换线性表中序号为i的数据元素为e返回原数据元素    public Object replace(int i,Object e)throws OutOfBoundaryException;    //返回线性表中序号为i的数据元素    public Object get(int i) throws OutOfBoundaryException;}

Strategy configuration: (with the object type, there is a problem: int type and string type comparison)

public interface Strategy {    //判断两个数据元素是否相等    public boolean equal(Object obj1,Object obj2);    //比较两个数据元素的大小    public int compare(Object obj1,Object obj2);}

Implementation of the list interface (Listarray.java):

public class Listarray implements list{private final int len=8;//array default size private strategy strategy;//data element comparison strategy private int size;//Number of data elements in a linear table private object[] elements;//data element array public Listarray (strategy strategy) {th        Is.strategy=strategy;        this.size=0;    Elements=new Object[len];    } @Override public int getsize () {//TODO auto-generated method stub return size;    } @Override public Boolean isEmpty () {//TODO auto-generated method stub return size==0; } @Override public Boolean contains (Object e) {//TODO auto-generated method stubs for (int i=0;i<si        ze;i++) {if (Strategy.equal (E,elements[i])) return true;    } return false; } @Override public int indexOf (Object e) {//TODO auto-generated method stubs for (int i=0;i<size;i+        +) {if (Strategy.equal (E,elements[i])) return i;  }      return-1;  } @Override public void Insert (int i, Object e) throws outofboundaryexception {//TODO auto-generated method Stub if (i<0| |        i>size) throw new Outofboundaryexception ("Out of Bounds");        if (size>=elements.length) expandsapce ();            for (int j=size;j>i;j--) elements[j]=elements[j-1];            Elements[i]=e;            size++;    Return        } public void Expandsapce () {object[] a=new object[elements.length*2];        for (int i=0;i<elements.length;i++) {a[i]=elements[i];    } elements=a;  } @Override public Boolean insertbefore (Object obj, Object e) {//TODO auto-generated method stub int        I=indexof (obj);        if (i<0) return false;        try {Insert (i,e);        } catch (Outofboundaryexception E1) {//TODO auto-generated catch block E1.printstacktrace ();    } return true; } @OVerride public boolean InsertAfter (Object obj, Object e) {//TODO auto-generated method stub int I=index        of (obj);        if (i<0) return false;        try {Insert (i+1,e);        } catch (Outofboundaryexception E1) {//TODO auto-generated catch block E1.printstacktrace ();    } return true;        } @Override public Object remove (int i) throws outofboundaryexception {//TODO auto-generated method stub if (i<0| |        i>size) throw new Outofboundaryexception ("Out of Bounds");        Object Obj=elements[i];        for (int j=i;j<size;j++) {elements[j]=elements[j+1];        } elements[--size]=null;    return obj;        } @Override Public Boolean remove (Object e) {//TODO auto-generated method Stub int I=indexof (E);        if (i<0) return false;        try {remove (i); } catch (Outofboundaryexception E1) {//TODO auto-generated CatCH Block e1.printstacktrace ();    } return true; } @Override public Object replace (int i, object e) throws Outofboundaryexception {//TODO auto-generated met Hod stub if (i<0| |        i>size) throw new Outofboundaryexception ("Out of Bounds");        Object Obj=elements[i];        Elements[i]=e;    return obj;        } @Override public Object get (int i) throws outofboundaryexception {//TODO auto-generated method stub if (i<0| |        i>size) throw new Outofboundaryexception ("Out of Bounds");    return elements[i]; }}

Implementation of the policy:

/** * @ClassName: IntergerStretegy * @Description: TODO  整数的比较策略* @author 萨摩耶* @date 2018年4月30日 上午9:24:42 *  */public class IntergerStretegy implements Strategy{    @Override    public boolean equal(Object obj1, Object obj2) {        // TODO Auto-generated method stub        if(obj1 instanceof Integer&&obj2 instanceof Integer)        {            if(obj1==obj2)                return true;        }        return false;    }    @Override    public int compare(Object obj1, Object obj2) {        // TODO Auto-generated method stub        return 0;    }}

Test class:

public class Test {    public static void main(String[] args) throws OutOfBoundaryException    {        ListArray la=new ListArray(new IntergerStretegy());        for(int i=0;i<7;i++)        la.insert(i, i+1);        System.out.println(la.get(6));        System.out.println(la.indexOf(5));    }}

Linear table of data structure and algorithm analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.