数据结构(java)-顺序表/单链表

版权声明:中华人民共和国持有版权 https://blog.csdn.net/Fly_Fly_Zhang/article/details/84441719

顺序表

package com.tuln.datastract;

import java.util.Arrays;

/**
 * @Created with IntelliJ IDEA
 * @Description:
 * @Package: com.tuln.datastract
 * @User: FLy
 * @Date: 2018/11/21
 * @Time: 19:04
 */

class TestSqlist{ //顺序表
    private int [] elem;  //
    private int usedsize; //有效数据个数
    public TestSqlist(){
        this(10);
    }
    public TestSqlist(int size){
        this.elem=new int[size];
        this.usedsize=0;
    }
    public boolean isFull(){ //判断数组是否装满
        if(usedsize==elem.length){
            return false;
        }
        return true;
    }
    public boolean Insert(int pos,int value){ //插入元素
        if(isFull()){  //数组扩容
            this.elem=Arrays.copyOf(this.elem,this.elem.length);
        }
        if(pos>usedsize||pos<0){
            return false;
        }else {
            for (int i = usedsize-1; i >=pos ; i--) {
                elem[i+1]=elem[i];
            }
            elem[pos]=value;
            usedsize++;
            return true;
        }
    }
    public boolean isEmpty(){
        if(usedsize==0){
            return true;
        }
        return false;
    }

    public int search(int key){ //查找数组元素
        if(isEmpty()){
            return -1;
        }

        for (int i = 0; i <usedsize ; i++) {
            if(this.elem[i]==key){
                    return i;
            }
        }
        return -1;
    }
    /*
     *删除关键字key  第一次开出现的key
     */
    public boolean delete(int key){
        int index=search(key); //调用查找方法,找到角标
        if(index<0){
            return false;
        }else {
            for (int i = index; i< this.usedsize-1  ; i++) {
                this.elem[i]=this.elem[i+1];
            }
            this.usedsize--;
            this.elem[usedsize]=0;
        }
        return true;
    }
    /*
     *得到pos位置的值
     */
    public int getpos(int pos){
        if(pos<0||pos>this.usedsize||isEmpty()){
         throw new UnsupportedOperationException("pos位置不合法或者顺序表为空");
        }
        show(elem[pos]);
        return this.elem[pos];
    }
    public static void show(int tmp){
        System.out.println(tmp);
    }
}
public class TestSqlistDemo {
    public static void main(String[] args) {

    }
}

单链表

  1. 基本的操作:头插 尾插
  2. 判断一个单链表是否有环?环的入口点?环的长度?
  3. 判断两个单链表是否相交?相交求交点?
  4. 合并两个有序的单链表为了一个单链表?
  5. 单链表的反转?
  6. 单链表的逆置?
  7. O(1)的 时间

在这里插入图片描述

package com.tuln.datastract;
import java.lang.String;

/**
 * @Created with IntelliJ IDEA
 * @Description: 单链表
 * @Package: com.tuln.datastract
 * @User: FLy
 * @Date: 2018/11/21
 * @Time: 20:22
 */
class TestLink{

    class Entry{ //内部类
        int data; //一般可定义成Object
        Entry next;
        public Entry(){ //头结点
            this.data=-1;
            this.next=null;
        }
        public Entry(int val){
            this.data=val;
            this.next=null;
        }
    }
    private Entry head;//头引用
    public TestLink(){
        this.head=new Entry();
    }
    public TestLink(int val){
        this.insertHead(val);
    }
    /*
     *  头插法
     */
    public void insertHead(int val){
        Entry cur=new Entry(val);
        cur.next=this.head.next; //先链接后面的链表,
        head.next=cur; //再用头部去链接新建的列表
    }
    /*
     *尾插法
     * 找到尾巴 插入数据
     */
    public void insertTail(int val){
        // 找到尾巴
        Entry cur=this.head; //头不动 赋值一个变量去判断。cur为head后的第一个链表
        while(cur.next!=null){
            cur=cur.next;
        }
        Entry entry=new Entry(val);
        //插入数据
        cur.next=entry;
    }
    /*
     *得到单链表的长度(数据节点的个数)
     */
    public int getLength(){
        int count=0;
        Entry cur=this.head.next; //同上 复制head变量去判断
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    /*
     *任意位置插入
     */
    public void  insertPos(int val,int pos) throws Exception {
        /*
        if(pos<0||pos>getLength()){
            throw new Exception("链表为空");
        }

        int count=0;
        Entry cur=this.head.next;
        while(cur!=null){
            if(count+1==pos){
                Entry cur1=new Entry(val);
                cur1.next=cur.next;
                cur.next=cur1;
            }
        }*/
        if(pos<0||pos>getLength()){
            return;
        }
        Entry cur =this.head;
        for(int i=0;i<=pos-1;i++){
           cur =cur.next;
        }
        Entry entry=new Entry(val);
        entry.next=cur.next;
        cur.next=entry;
    }
    /*
     *   删除单链表当中所有值为val的节点
     */
    public void delete(int val){
        Entry cur1=this.head;
        Entry cur2=cur1.next; //需要注意head后第一个元素就为val的情况。
        while( cur2!=null ){
            if(cur2.data==val){
               cur1.next=cur2.next;
               cur2=cur1.next;
            }else {
                cur1=cur1.next;
                cur2=cur2.next;
            }
        }
        show();
    }
    /*
     * 打印单链表所有的数据
     */
    public void show(){
        Entry cur=this.head.next;
        while(cur !=null){
            System.out.println(cur.data);
            cur=cur.next;
        }
    }
}
public class TestLinkDemo {
    public static void main(String[] args) throws Exception {
        TestLink testLink = new TestLink();
        /*testLink.insertHead(10);
        testLink.insertHead(20);*/

        for(int i = 0;i < 10;i++) {
            testLink.insertTail(i);//
        }
        testLink.show();
        testLink.insertPos(999,5);
        testLink.show();
        testLink.delete(1);

    }

}

区别:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Fly_Fly_Zhang/article/details/84441719