Java implements sequence table data structure

package com.irisian;

public class Point {

    private int x;
    private int y;

    public Point() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }

}



package com.irisian;

import java.util.Arrays;

public class SqList {
    private Point [] p;
    private int length;
    private int listsize;

    public Point[] getP() {
        return p;
    }

    public void setP(Point[] p) {
        this.p = p;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getListsize() {
        return listsize;
    }

    public void setListsize(int listsize) {
        this.listsize = listsize;
    }

    @Override
    public String toString() {
        return "SqList [p=" + Arrays.toString(p) + ", length=" + length + ", listsize=" + listsize + "]";
    }

}

package com.irisian;

public class MyList {

    private static final int LIST_INIT_SIZE = 100; // initialize length
    private static final int LISTINCREMENT = 50; // increase length
    private static final int OVERFLOW = -2; //
    private static final int OK = 1; //
    private static final int ERROR = 0; //

    /**
     * Initialize linear table
     */
    @SuppressWarnings("unused")
    public int initList(SqList l) {
        Point[] p = new Point[LIST_INIT_SIZE];
        if (p == null) {
            // If p is empty, it means that the allocation fails, then exit the program
            System.exit(OVERFLOW);
        }
        // If the allocation is successful, change the length of SqList to
        l.setListsize(LIST_INIT_SIZE);
        l.setLength(0);
        l.setP(p);
        return OK;
    }
    
    /**
     * insert value for
     the linear table * @param l linear table where the value to be inserted
     * @param i insertion position
     * @param p inserted value
     * @return returns whether it is successful
     */
    public int insertList(SqList l,int i,Point p){
        //The insertion position is 1=<i<=l.getLength()+1
        if(i<1||i> (l.getLength()+1)){
            return ERROR;
        }
        //If the length of the array is full, then the length should be reassigned
        if(l.getLength()>=l.getListsize()){
            // Define a new array, and then assign the value of the original array to the new array
            Point [] p1=new Point[l.getListsize()+LISTINCREMENT];
            for (int j = 0; j < l.getP().length; j++) {
                p1[j]=l.getP()[j];
            }
            //Assign the new array to the linear table
            l.setP(p1 );
            l.setListsize(l.getListsize()+LISTINCREMENT);
        }
        
        //if the inserted position is correct and not full yet
        //get a reference to the insertion position
        //move from the back
        for (int j = l.getLength ()-1; j >= i-1; j--) {
            Point point = l.getP()[j];
            l.getP()[j+1]=point;
        }
        l.getP()[i -1]=p;
        l.setLength(l.getLength()+1);
        return OK;
    }
    
    /**
     * delete the value in the sequence table
     * @param l sequence table
     * @param i delete element position
     * @param p element to delete element (deleted element is passed back by this reference)
     * @return
     */
    int listDelete(SqList l,int i,Point p){
        //i is legal Range 1=<i<=l.getLength();
        if(i<1||i>l.getLength()){
            return ERROR;
        }
        //If the data is legal, delete it
        //Get the reference of the element to be deleted
        p = l.getP()[i-1];
        //Overwrite i with the number after i
        for (int j = i; j <= l.getLength(); j++) {
            Point point = l.getP() [j];
            l.getP()[j-1]=point;
        }
        l.setLength(l.getLength()-1);
        return OK;
    }
    
    /**
     * Merge the two sequence tables
     * @param l1
     * @param l2
     * @param l3
     */
    void mergeList(SqList l1,SqList l2,SqList l3){
        Point[] p1 = l1.getP();
        Point[] p2 = l2.getP();
        
        
        / / Set the new sqlList to the total length of the two linear lists
        l3.setLength(l1.getLength()+l2.getLength());
        l3.setListsize(l1.getLength()+l2.getLength());
        // Allocate memory space
        l3.setP(new Point[l1.getLength()+l2.getLength()]);
        Point[] p3 = l3.getP();
        
        int i=0,i1=0,i2=0;
        System. err.println();
        
        while(i<l1.getLength()&&i<l1.getLength()){
            if(p1[i].getX()>p2[i].getX()){
                p3[i]= p1[i];
                i1++;
            }else if(p1[i].getX()<p2[i].getX()){
                p3[i]=p2[i];
                i2++;
            }else{
                p3[i]=p2[i];
                i1++;
                i2++;
            }
            i++;
        }
        while(i1<l1.getLength()){
            p3[i]=p1[i1];
            i1++;
        }
        
        while(i2<l2.getLength()){
            p3[i]=p2[i2];
            i2++;
        }
    }
    
    
    public static void main(String[] args) {
        MyList MyList = new MyList();
        SqList sqList = new SqList();
        int initList = MyList.initList(sqList);
        if(initList==OK){
            System.out.println("Successful initialization of the sequence table!");
        }
        Point point = new Point(1, 2);
        MyList.insertList(sqList , 1, point);
        System.err.println(sqList);
        //Delete an element
        System.err.println("Delete an element");
        MyList.listDelete(sqList, 1, point);
        System.err.println( sqList);
        System.err.println(point);
        System.err.println("Insert an element");
        point=new Point(2,3);
        MyList.insertList(sqList, 1, point);
        System.err. println(sqList);
        System.err.println("Initialize another sequence list");
        SqList sqList2 = new SqList();
        int initList2 = MyList.initList(sqList2);
        System.out.println(initList2==OK?"顺序表初始化成功":"失败");
        Point point2 = new Point(3, 4);
        MyList.insertList(sqList2, 1, point);
        MyList.insertList(sqList2, 2, point2);
        System.err.println(sqList2);
        System.err.println("合并");
        SqList sqList3 = new SqList();
        MyList.mergeList(sqList, sqList2, sqList3);
        System.err.println(sqList3);
        
        
        
    }
    
}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325597885&siteId=291194637