线性表java实现

Posted blog_thyin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线性表java实现相关的知识,希望对你有一定的参考价值。

顺序表

public class SequenceList {
    /*
    * content,节点内容
    * location,节点在表中的位置(序号)
    * */
    private String content;
    private int location;
    SequenceList(String content, int location){
        this.content = content;
        this.location = location;
    }
    SequenceList(){
        this.content = "-1";
        this.location = -1;
    }
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getLocation() {
        return location;
    }

    public void setLocation(int location) {
        this.location = location;
    }
}

  

顺序表的各个操作

遍历

/*
    * 遍历顺序表
    * */
    public static void showList(SequenceList[] list){
        for(SequenceList one:list){
            if(one!=null)
                System.out.println("location = "+one.getLocation()+",content = "+one.getContent());
            else
                break;
        }
    }

  

插入

/*
    * 插入节点
    * */
    public static void insertItem(SequenceList[] list, SequenceList item, int currentListLength, int insertLocation){
        list[currentListLength] = new SequenceList();
        if(currentListLength<insertLocation){
            System.out.println("插入位置不在线性表内");
        }else if(currentListLength==insertLocation+1){
            System.out.println("线性表已满");
        }else {
            for(int i = currentListLength;i>insertLocation;i--){
                list[i] = list[i-1];
                list[i].setLocation(i);//重新设置节点位置
            }
            list[insertLocation] = item;
            currentListLength++;
        }

    }

获得特定位置的节点

/*
    * 获得特定位置的节点
    * */
    public static SequenceList getSpecificItem(SequenceList[] list, int location){
        for(SequenceList one:list){
            if(one.getLocation()==location){
                return one;
            }
        }
        return null;
    }

  

获得某个节点的位置

/*
    * 获得某个节点的位置
    * */
    public static int getItemLocation(SequenceList[] list, String content){
        for(SequenceList one:list){
            if(one.getContent().equals(content)){
                return one.getLocation();
            }
        }
        return -1;
    }

  

删除某个位置的节点

 /*
    * 删除节点
    * */
    public static void deleteItem(SequenceList[] list, int location){
        for(int i = location;;i++){
            if(list[i+1]==null){
                list[i] = null;
                break;
            }
            list[i] = list[i+1];
            list[i].setLocation(i);//重新设置节点位置
        }
    }

  

测试

public static void main(String[] args) {
        SequenceList[] lists = new SequenceList[20];
        for(int i = 0;i < 6;i++){
            lists[i] = new SequenceList();
            lists[i].setContent(String.valueOf(i));
            lists[i].setLocation(i);
        }
        insertItem(lists,new SequenceList("a",5),6,5);
        showList(lists);
        deleteItem(lists,5);
        showList(lists);
        System.out.println("5号位的内容是"+getSpecificItem(lists,5).getContent());
        System.out.println("内容为2的位置是"+getItemLocation(lists,"2"));
    }

  

结果

location = 0,content = 0
location = 1,content = 1
location = 2,content = 2
location = 3,content = 3
location = 4,content = 4
location = 5,content = a
location = 6,content = 5
location = 0,content = 0
location = 1,content = 1
location = 2,content = 2
location = 3,content = 3
location = 4,content = 4
location = 5,content = 5
5号位的内容是5
内容为2的位置是2

  

链表

持续更新

以上是关于线性表java实现的主要内容,如果未能解决你的问题,请参考以下文章

Java数据结构(线性表)--线性表的顺序存储及其实现

Java数据结构学习笔记之一线性表的存储结构及其代码实现

java线性表之顺序表实现

[DataStructure]非线性数据结构之哈希表二叉树及多叉树 Java 代码实现

[DataStructure]哈希表二叉树及多叉树 Java 代码实现

顺序表的java实现