Data Structures and Algorithms -Node and SeqList

package key3;

public class Node<T>//单链表结点类,T指定结点的元素类型
{
    public T data;       //数据域,存储数据元素
    public Node<T> next; //地址域,引用后继结点

    public Node(T data, Node<T> next)//构造结点,data指定数据元素,next指定后继结点
    {
        this.data = data;   //T对象引用赋值
        this.next = next;   //Node<T>对象引用赋值
    }
    public Node()
    {
        this(null, null);
    }
    public String toString() //返回结点数据域的描述字符串
    {
        return this.data.toString();
    }
}
package code2;

public class SeqList<T> {
    protected Object element[]; //对象数组,保护成员
    protected int n; //顺序表元素个数(长度)
    
    //1. 构造顺序表
    public SeqList(int length)//构造容量为length的空表
    {
        this.element = new Object[length]; //申请数组的存储空间,元素为null。
        //若length<0,Java抛出负数组长度异常 java.lang.NegativeArraySizeException
        this.n = 0;
    }
    public SeqList()//创建默认容量的空表,构造方法重载
    {
        this(64); //调用本类已声明的指定参数列表的构造方法
    }
    public SeqList(T[] values) //构造顺序表,由values数组提供元素,忽略其中空对象
    {
        this(values.length); //创建容量为values.length的空表
        //若values==null,用空对象调用方法,Java抛出空对象异常NullPointerException
        for (int i=0; i<values.length; i++) //复制数组元素,O(n)
            this.element[i] = values[i]; //对象引用赋值
        this.n = element.length;
    }
    
    //2.求表长
    public int size()//返回顺序表元素个数,O(1)
    {
        return this.n;
    }
    
    //3.判表空
    public boolean isEmpty() //判断顺序表是否空,若空返回true,O(1)
    {
        return this.n==0;
    }
    
    //4.取元素
    public T get(int i) //返回第i个元素,0≤i<n。若i越界,返回null。O(1)
    {
        if (i>=0 && i<this.n)
            return (T)this.element[i]; //返回数组元素引用的对象,传递对象引用
        return null;
    } 
    
    //5.改元素
    //设置第i个元素为x,0≤i<n。若i越界,抛出序号越界异常;若x==null,抛出空对象异常。O(1)
    public void set(int i, T x)
    {
        if (x==null)
            throw new NullPointerException("x==null");     //抛出空对象异常
        if (i>=0 && i<this.n)
            this.element[i] = x;
        else throw new java.lang.IndexOutOfBoundsException(i+"");//抛出序号越界异常
    }
    
    //6.输出
    //返回顺序表所有元素的描述字符串,形式为“(,)”。覆盖Object类的toString()方法
    public String toString()
    {
        String str=this.getClass().getName()+"(";  //返回类名
        if (this.n>0)
            str += this.element[0].toString(); //执行T类的toString()方法,运行时多态
        for (int i=1; i<this.n; i++)
            str += ", "+this.element[i].toString(); //执行T类的toString()方法,运行时多态
        return str+") ";//空表返回()
    }
    
    //7. 插入元素
    //插入x作为第i个元素,x!=null,返回x序号。若x==null,抛出空对象异常。O(n)
    //对序号i采取容错措施,若i<0,插入x在最前;若i>n,插入x在最后
    public int insert(int pos, T x)
    {
        if (x==null)
            throw new NullPointerException("x==null");  //抛出空对象异常
        if (pos<0)       pos=0;                            //插入位置i容错,插入在最前
        if (pos>this.n)  pos=this.n;                       //插入在最后
        Object source[] = this.element;                //数组变量引用赋值,source也引用element数组
        if (this.n==element.length)                    //若数组满,则扩充顺序表的数组容量
        {
            this.element = new Object[source.length*2]; //重新申请一个容量更大的数组
            for (int j=0; j<pos; j++)                    //复制当前数组前i-1个元素
                this.element[j] = source[j];
        }
        for (int j=this.n-1; j>=pos; j--)               //从i开始至表尾的元素向后移动,次序从后向前
            this.element[j+1] = source[j];
        this.element[pos] = x;
        this.n++;
        return pos;                                     //返回x序号
    }
    
    public int insert(T x)  //顺序表尾插入x元素,返回x序号。成员方法重载
    {
        return this.insert(this.n, x); //插入操作中,this.n加1
    }
    
    //8. 删除元素
    public T remove(int i)          //删除第i个元素,0≤i<n,返回被删除元素。若i越界,返回null。
    {
        if (this.n>0 && i>=0 && i<this.n) 
        {
            T old = (T)this.element[i];                    //old中存储被删除元素
            for (int j=i; j<this.n-1; j++)
                this.element[j] = this.element[j+1];       //元素前移一个位置
            this.element[this.n-1]=null;                   //设置数组元素对象为空,释放原引用实例
            this.n--;
            return old;                                    //返回old局部变量引用的对象,传递对象引用
        }
        return null;
        //throw new IndexOutOfBoundsException(i+"");         //抛出序号越界异常
    }
    
    //9.清空顺序表
    public void clear() //删除线性表所有元素
    {
        this.n=0;  //设置长度为0,未释放数组空间
    }
    
    //.合并顺序表
    public void MergeList(SeqList aList)
    {
    	for(int i=0; i<aList.size(); i++)
    		this.insert((T)aList.get(i));
    }
}
Published 130 original articles · won praise 17 · views 7220

Guess you like

Origin blog.csdn.net/weixin_42554191/article/details/103978098