码迷,mamicode.com
首页 > 其他好文 > 详细

线性表

时间:2018-12-01 15:25:14      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:cap   for   stat   nbsp   sys   public   ble   getc   线性   

顺序存储线性表(数组实现)

public class Sqlist {

    static int [] table;
    static int n;
    
    public Sqlist(int n)
    {
        table=new int[n];
        this.n=0;
    }
    
    public boolean isEmpty()
    {
        return n==0;
    }
    
    public boolean isFull()
    {
        return n>=table.length;
    }
    
    public int getcapacity()
    {
        return table.length;
    }
    
    public int getlength()
    {
        return n;
    }
    
    //ADD
    public void addElem(int loc,int value)
    {
        if(n>=table.length)
        {
            System.out.println("数组满了");
        }
        if((loc<1)||(loc>table.length))
        {
            System.out.println("插入位置错误");
        }
        if(loc<=n+1)
        {
            for(int i=n-1;i>=loc;i--)
            {
                table[i+1]=table[i];
            }
            table[loc-1]=value;
            n++;
            System.out.println("插入成功,在第"+loc+"个位置之前插入:"+value);
        }
        
    }
    
    //DELETE
    public int getElem(int i)
    {
        if((i<1)||(i>table.length))
        {
            return -1;
        }
        else {
            return table[i-1];
        }
    }
}

 

线性表

标签:cap   for   stat   nbsp   sys   public   ble   getc   线性   

原文地址:https://www.cnblogs.com/Maskisland/p/10048850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!