12.19單向連結串列

weixin_53437610發表於2020-12-20

1.連結串列(linked list )

  • 連結串列是有序的列表
  • 連結串列是以節點的方式來儲存,鏈式儲存
  • 每個節點包含data域,next域,指向下一個節點
  • 如圖:連結串列的各個節點不一定是連續存放的

連結串列實際在記憶體中的示例圖單向連結串列實現

package zhangsong;

import java.util.PrimitiveIterator;

public class Linked {
    public static void main(String[] args) {
        HeadLinke headLinke = new HeadLinke();
        Hero_Linked_List hero_linked_list = new Hero_Linked_List(1, "張三", "小三");
        Hero_Linked_List hero_linked_list1 = new Hero_Linked_List(2, "李四", "小四");
        Hero_Linked_List hero_linked_list2 = new Hero_Linked_List(3, "王五", "小五");
        Hero_Linked_List hero_linked_list3 = new Hero_Linked_List(4, "趙六", "小劉");
        Hero_Linked_List hero_linked_list4 = new Hero_Linked_List(5, "趙六", "小劉");
        headLinke.add(hero_linked_list);
        headLinke.add(hero_linked_list1);
        headLinke.add(hero_linked_list2);
        headLinke.add(hero_linked_list3);
        headLinke.add(hero_linked_list4);
        headLinke.show();
    }
}
class HeadLinke{
    private Hero_Linked_List head = new Hero_Linked_List(0,"","");
    private Hero_Linked_List p = head;
    public void add(Hero_Linked_List hero){
        p.nextLinked = hero;
        System.out.println(p);
        p = p.nextLinked;
    }
    public void show(){
        if (p.nextLinked == null){
            return;
        }
        while (true){
            if (p.nextLinked == null){
                break;
            }
            System.out.println(p);
        }
    }
}
class Hero_Linked_List{
    private int no;
    private String name;
    private String smallname;
    public Hero_Linked_List nextLinked;

    public Hero_Linked_List(int no, String name, String smallname) {
        this.no = no;
        this.name = name;
        this.smallname = smallname;
    }

    @Override
    public String toString() {
        return "Hero_Linked_List{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", smallname=" + smallname +
                '}';
    }
}

排序連結串列實現程式碼

package zhangsong;

import java.util.PrimitiveIterator;

public class Linked {
    public static void main(String[] args) {
        HeadLinke headLinke = new HeadLinke();
        Hero_Linked_List hero_linked_list = new Hero_Linked_List(1, "張三", "小三");
        Hero_Linked_List hero_linked_list1 = new Hero_Linked_List(5, "李四", "小四");
        Hero_Linked_List hero_linked_list2 = new Hero_Linked_List(9, "王五", "小五");
        Hero_Linked_List hero_linked_list3 = new Hero_Linked_List(5, "趙六", "小劉");
        Hero_Linked_List hero_linked_list4 = new Hero_Linked_List(3, "趙六", "小劉");
        headLinke.addsort(hero_linked_list);
        headLinke.addsort(hero_linked_list1);
        headLinke.addsort(hero_linked_list2);
        headLinke.addsort(hero_linked_list3);
        headLinke.addsort(hero_linked_list4);
        headLinke.show();
    }
}
class HeadLinke{
    private Hero_Linked_List head = new Hero_Linked_List(0,"","");
    private Hero_Linked_List p = head;
    public void addsort(Hero_Linked_List hero){
        Hero_Linked_List p1 = head;
        boolean flag = false;
        while (true){
            if (p1.nextLinked == null) {
                break;
            }else if(p1.nextLinked.no > hero.no){
                break;
            }else if(p1.nextLinked.no == hero.no){
                flag = true;
                break;
            }
            p1 = p1.nextLinked;
        }
        if (flag){
            System.out.printf("已有資料,加入失敗");
        }else {
            hero.nextLinked = p1.nextLinked;
            p1.nextLinked = hero;
            System.out.println("成功");
        }
    }
    public void add(Hero_Linked_List hero){
        p.nextLinked = hero;
//        System.out.println(p);
        p = p.nextLinked;
    }
    public void show(){
        Hero_Linked_List p1 = head;
        while (true){
            if (p1.nextLinked == null){
                break;
            }
            p1 = p1.nextLinked;
            System.out.println(p1);
        }
    }
}
class Hero_Linked_List{
    public int no;
    private String name;
    private String smallname;
    public Hero_Linked_List nextLinked;

    public Hero_Linked_List(int no, String name, String smallname) {
        this.no = no;
        this.name = name;
        this.smallname = smallname;
    }

    @Override
    public String toString() {
        return "Hero_Linked_List{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", smallname=" + smallname +
                '}';
    }
}

反轉連結串列,檢視連結串列長度\

package zhangsong;

import java.util.PrimitiveIterator;

public class Linked {
    public static void main(String[] args) {
        HeadLinke headLinke = new HeadLinke();
        Hero_Linked_List hero_linked_list = new Hero_Linked_List(1, "張三", "小三");
        Hero_Linked_List hero_linked_list1 = new Hero_Linked_List(5, "李四", "小四");
        Hero_Linked_List hero_linked_list2 = new Hero_Linked_List(9, "王五", "小五");
        Hero_Linked_List hero_linked_list3 = new Hero_Linked_List(3, "趙六", "小劉");
        headLinke.addsort(hero_linked_list);
        headLinke.addsort(hero_linked_list1);
        headLinke.addsort(hero_linked_list2);
        headLinke.addsort(hero_linked_list3);
        headLinke.show();
        System.out.println("*******************");
        Hero_Linked_List reverselinked = headLinke.reverselinked();
        headLinke.show();

    }
}
class HeadLinke{
    public Hero_Linked_List head = new Hero_Linked_List(0,"","");
    private Hero_Linked_List p = head;
    public Hero_Linked_List reverselinked(){
        Hero_Linked_List temp = head.nextLinked;
        Hero_Linked_List pointer = null;
        Hero_Linked_List head2 = new Hero_Linked_List(0,"","");
        if (head.nextLinked == null || head.nextLinked.nextLinked == null){
            return temp;
        }
        while (temp != null){
            pointer = temp.nextLinked;
            temp.nextLinked = head2.nextLinked;
            head2.nextLinked = temp;
            temp = pointer;
            System.out.println("1");
        }
        head.nextLinked = head2.nextLinked;
        return head;
    }
    public int getLength(Hero_Linked_List head){
        Hero_Linked_List po = head.nextLinked;
        int length = 0;
        while (true){
            if (po == null){
                break;
            }
            length++;
        }
        return length;
    }
    public void rem(int no){
        Hero_Linked_List p1 = head;
        boolean falag = false;
        while (true){
            if (p1.nextLinked == null){
                break;
            }
            if (p1.nextLinked.no == no){
                falag = true;
                break;
            }
            p1 = p1.nextLinked;
        }
        if (falag){
            p1.nextLinked = p1.nextLinked.nextLinked;
        }else {
            System.out.printf("沒有此元素");
        }
    }
    public void addsort(Hero_Linked_List hero){
        Hero_Linked_List p1 = head;
        boolean flag = false;
        while (true){
            if (p1.nextLinked == null) {
                break;
            }else if(p1.nextLinked.no > hero.no){
                break;
            }else if(p1.nextLinked.no == hero.no){
                flag = true;
                break;
            }
            p1 = p1.nextLinked;
        }
        if (flag){
            System.out.printf("已有資料,加入失敗");
        }else {
            hero.nextLinked = p1.nextLinked;
            p1.nextLinked = hero;
            System.out.println("成功");
        }
    }
    public void add(Hero_Linked_List hero){
        p.nextLinked = hero;
//        System.out.println(p);
        p = p.nextLinked;
    }
    public void show(){
        Hero_Linked_List p1 = head;
        while (true){
            if (p1.nextLinked == null){
                break;
            }
            p1 = p1.nextLinked;
            System.out.println(p1);
        }
    }
}
class Hero_Linked_List{
    public int no;
    private String name;
    private String smallname;
    public Hero_Linked_List nextLinked;

    public Hero_Linked_List(int no, String name, String smallname) {
        this.no = no;
        this.name = name;
        this.smallname = smallname;
    }

    @Override
    public String toString() {
        return "Hero_Linked_List{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", smallname=" + smallname +
                '}';
    }
}

相關文章