一看就懂的java顺序表和链表的基础操作

本文介绍了顺序存储结构和链式存储结构的概念,通过Java实现了一个顺序表类`MyArrayList`,包括添加、查找、删除等基本操作。同时,展示了链表类`MyLinkedList`的实现,包含头插法、尾插法、任意位置插入、查找和删除等功能。通过这两个类的实例,详细阐述了顺序表和链表在数据操作上的区别和特点。
摘要由CSDN通过智能技术生成

顺序存储结构是一种在物理地址上连续,逻辑上也连续的存储结构,它的本质是使用数组实现,但是当数据个数较多且无法确定数据的个数时,对这些数据进行插入,删除等一系列操作时,消耗较大
链式存储结构是一种在物理地址上不连续,逻辑上连续的存储结构,它的想法和糖葫芦一样,用一根签串起所有的山楂,而链式存储结构的节点就如山楂,它的结构中包含数据域和指针域(或引用),通过引用,将各个节点串联起来。链式存储结构不需要确定数据的具体个数,且对数据进行插入,删除等操作时,消耗较小。
下面将展示顺序表和链表的基础操作

顺序存储结构

package com.company;

import java.util.Arrays;

public class MyArrayList {
    int usedSize;//有效数据的个数
    int[] elem;

    public MyArrayList() {
        this.usedSize = 0;
        this.elem = new int[10];
    }

    public void add(int pos, int data) {   //在pos位置新增元素
        if (pos < 0 || pos > this.usedSize) {
            System.out.println("输入位置不合法");
        }
        if (this.usedSize == this.elem.length) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);  //扩容
        }
        for (int i = this.usedSize - 1; i > pos; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    public void print() {//打印
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(elem[i]);
        }
        System.out.println();
    }

    public boolean contains(int tofind) {//查找是否包含所寻找的值
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == tofind) {
                System.out.println("已找到");
                return true;
            }
        }
        System.out.println("未找到");
        return false;
    }

    public int search(int tofind) {//查找是否有所寻找的值,若有,返回下标
        int result = 0;
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == tofind) {
                result = i;
                System.out.println("下标为" + result);
                return i;
            }
        }
        System.out.println("未找到");
        return -1;
    }

    public int getpos(int tofind) {//查找是否有所寻找的值,若有,返回该值
        int data = 0;
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == tofind) {
                data = this.elem[i];
                System.out.println("data=" + data);
                return data;
            }
        }
        System.out.println("未找到");
        return -1;
    }
    public void setpos(int pos,int data){//将pos位置的值改为data
        if(pos<0||pos>=this.usedSize){
            return;
        }
        this.elem[pos]=data;
    }

    public void MyArrayListLength(){//输出顺序表的长度
        int length=this.usedSize;
        System.out.println("length="+length);
    }

    public void DeleteAllData1(){//第一种情况:数组里面存放的是普通类型
        this.usedSize=0;
    }
    public void DeleteAllData2(){//第二种情况:数组里面存放的是引用类型
        for (int i = 0; i <this.usedSize ; i++) {
            //this.elem[i]=null;
        }
    }
    public void DeleteOneData(int toDelete){//删除指定元素:首先确定该元素位置,然后将其后面的元素依次向前移动一位
        int pos=0;
        for (int i = 0; i <this.usedSize ; i++) {
            if(this.elem[i]==toDelete){
                pos=i;
            }
           }
            for (int j=pos;j<this.usedSize-1;j++){
                this.elem[j]=this.elem[j+1];
            }
        this.usedSize--;
    }
}



MyArrayList类的测试主函数
public class Main{
    public static void main(String[] args) {
MyArrayList myArrayList=new MyArrayList();
myArrayList.add(0,24);
myArrayList.add(1,58);
myArrayList.add(2,98);
myArrayList.add(3,125);
myArrayList.add(4,54);
myArrayList.add(5,41);
myArrayList.print();
myArrayList.contains(54);
myArrayList.search(54);
myArrayList.getpos(54);
myArrayList.MyArrayListLength();

myArrayList.DeleteOneData(54);
myArrayList.print();
    }

}

链式存储结构

//链表 MyLinkedList的测试函数
public class Main{
    public static void main(String[] args) {
MyLinkedList myLinkedList=new MyLinkedList();
        myLinkedList.addFirstData(7);
        myLinkedList.addFirstData(6);
        myLinkedList.addFirstData(4);
        myLinkedList.addFirstData(3);
        myLinkedList.addFirstData(2);
        myLinkedList.addFirstData(1);
        //myLinkedList.print();
        myLinkedList.addLastData(8);
        myLinkedList.addLastData(9);
        myLinkedList.addLastData(10);
        myLinkedList.addLastData(1);
        //myLinkedList.print();
        myLinkedList.addIndexData(5,5);
        //myLinkedList.print();
       myLinkedList.RemoveOneData(5);
        //myLinkedList.print();
        myLinkedList.RemoveAllData(1);
        myLinkedList.print();
        int length=myLinkedList.getlength();
        System.out.println("length="+length);
    }
}


package com.company;
//单项不带头循环链表
 class Node{
    int data;//数据域
    Node next;//指针域(引用)

    public Node(int data) {
        this.data = data;
    }
}

public class MyLinkedList {
    public Node head; //指向头节点
    public void print(){  //打印
        Node cur;
        cur=this.head;
        while(cur!=null){
            System.out.println(cur.data+"");
            cur=cur.next; //新的cur变为原来cur的next
        }
    }

public int getlength(){ //找该链表的长度
    int usedSize=0; //有效长度
       Node cur=this.head;
       while (cur!=null){
           cur=cur.next;
           usedSize++;
       }
       return usedSize;
}

public boolean IndexLawful(int index){ //判断index(位置)是否合法
        if(index<0||index>this.getlength()){
            return false;
        }else{
            return true;
        }
}

    public void addFirstData(int data){//头插法
        Node node=new Node(data);
        node.next=head;
        this.head=node;
    }

    public void addLastData(int data){//尾插法
       Node node=new Node(data);
       if(this.head==null){           //考虑当该链表一个结点都没有的时候的情况
           this.head=node;
       }else{
           Node cur=this.head;
           while (cur.next!=null){      //寻找尾结点
               cur=cur.next;
           }
           cur.next=node;
       }
    }

    public void addIndexData(int index,int data){//从任意位置插入(插入位置为index)
        Node node=new Node(data);
       if(this.IndexLawful(index)){
           if(index==0){
               this.addFirstData(data);
               return;
           }
           if(index==this.getlength()){
               this.addLastData(data);
               return;
           }
           Node cur=this.head;
           for (int i = 1; i <index-1 ; i++) {
              cur=cur.next;
            }
           node.next=cur.next;
           cur.next=node;
       }
         }

         public Node SearchPrey(int index){//寻找index的前驱
        Node cur=this.head;
        int count=0;
        while (count<index-1){
            cur=cur.next;
            count++;
        }
        return cur;
    }

    public boolean contains(int key){//查找key的值是否在该链表当中
        Node cur=this.head;
        while(cur!=null){
            if(cur.data==key){
                return true;
            }else{
                cur=cur.next;
            }
        }
        return false;
         }

    // 删除节点数据为key的结点
        public Node SearchKeyPrey(int key){  //寻找key的前驱节点
           Node cur=this.head;
           while (cur.next!=null){    // 当cur为最后一项时,没找到,若是cur!=null,则会超出范围
               if(cur.next.data==key){
                   return cur;
               }
               cur=cur.next;
           }
         return null;
    }
        public void RemoveOneData(int key){
        if(this.head==null){
            return;
        }
        if(this.head.data==key){
            this.head.next=this.head;
            return;
        }
        Node cur=this.SearchKeyPrey(key);
        cur.next=cur.next.next;
        }


        public void RemoveAllData(int key){
            Node cur=head.next;   //cur指向第二个节点,然后指向需要删除的节点
            Node prev=head;       //prev指向cur的前驱
            while(cur!=null){
                if(cur.data==key){
                    prev.next=cur.next;
                    cur=cur.next;
                }else{
                    prev=cur;
                    cur=cur.next;
                }
            }
            if(head.data==key){    //先删除除头节点外的其他节点,再删除头节点
                head=head.next;
            }
        }

        public void CleanLinkedList(){
        this.head=null;           //将头节点置为空,则后续节点都没有被引用,系统会自动回收
        }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Serendipity sn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值