单链表

作者: 448f984add9e | 来源:发表于2018-12-06 20:56 被阅读0次

【节点】

package linkedList;

public class Node {
    int val;
    Node next;
    public Node(int val){
        this.val=val;
        this.next=null;
    }
}

【单链表】

package linkedList;

public class LinkedList {
    Node first;
    Node last;
    
    public LinkedList(int[] array) {
        for (int i = 0; i < array.length; i++) {
            addNodeToLinkedList(array[i]);
        }
    }
    
    public void addNodeToLinkedList(int val) {//创建单链表
        Node newNode=new Node(val);
        if(first==null) {
            first=newNode;
            last=first;
            return;
        }
        last.next=newNode;
        last=last.next;
    }
    
    public void printLinkedList(Node head) {//打印单链表
        Node current=head;
        System.out.print("当前链表节点顺序:");
        while(current!=null) {
            System.out.print("  "+current.val);
            current=current.next;
        }
    }
    
    public void insertFirst(int val) {//新节点插在表头前面
        Node newNode=new Node(val);
        newNode.next=first;
        first=newNode;
    }
    
    public void insertLast(int val) {//新节点插在表尾后面
        Node newNode=new Node(val);
        last.next=newNode;
        last=newNode;
    }
    
    public void insert(int target,int val) {//新节点插在目标值后面 
        Node newNode=new Node(val);
        Node current=first;
        while(current.val!=target) {
            current=current.next;
        }
        Node temp=current.next;
        current.next=newNode;
        newNode.next=temp;
    }
    
    public void search(int val) {//从链表里查询值是否存在
        Node current=first;
        while(current!=null) {
            if(current.val==val) {
                System.out.println("链表里存在  "+current.val);
                return;
            }
            current=current.next;
        }
        if(last.val!=val)
            System.out.println("链表里不存在  "+val);
    }
    
    public void deleteFirst(Node head) {//删除表头
        head=head.next;
    }
    
    public void deleteLast(Node head) {//删除表尾
        Node current=head;
        Node temp=null;
        while(current.next!=null) {
            temp=current;
            current=current.next;
        }
        temp.next=null;
        last=temp;
    }
    
    public void delete(int val) {//删除某值节点
        Node current=first;
        Node temp=null;
        while(current.val!=val) {
            temp=current;
            current=current.next;
        }
        temp.next=current.next;
    }
    
}

【测试】

package linkedList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String[] args)throws IOException {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        int[] array=new int[5];
        System.out.println("请依次输入五个整数:");
        for (int i = 0; i < array.length; i++) {
            array[i]=Integer.parseInt(in.readLine());
        }
        System.out.print("输入五个整数为:");
        for (int i = 0; i < array.length; i++) {
            System.out.print("  "+array[i]);
        }
        System.out.println();
        LinkedList linkedList=new LinkedList(array);
        linkedList.printLinkedList(linkedList.first);
        System.out.println();
        while(true) {
            System.out.print("请输入一个整数作为新插入节点值插在表头前面(输入 -1 退出):");
            int val=Integer.parseInt(in.readLine());
            if(val==-1) {
                System.out.println("退出表头前面插入操作!!!");
                break;
            }
            linkedList.insertFirst(val);
            linkedList.printLinkedList(linkedList.first);
            System.out.println();
        }
        while(true) {
            System.out.print("请输入一个整数作为新插入节点值插在表尾后面(输入 -1 退出):");
            int val=Integer.parseInt(in.readLine());
            if(val==-1) {
                System.out.println("退出表尾后面插入操作!!!");
                break;
            }
            linkedList.insertLast(val);
            linkedList.printLinkedList(linkedList.first);
            System.out.println();
        }
        while(true) {
            System.out.print("请输入一个链表中存在的整数,新节点插在它后面:");
            int target=Integer.parseInt(in.readLine());
            System.out.print("请输入一个整数作为新插入节点值插在中间某节点后面(输入 -1 退出):");
            int val=Integer.parseInt(in.readLine());
            if(val==-1) {
                System.out.println("退出插入某中间节点后面的操作!!!");
                break;
            }
            linkedList.insert(target,val);
            linkedList.printLinkedList(linkedList.first);
            System.out.println();
        }
        while(true) {
            System.out.print("请输入一个整数检查链表里是否存在(输入 -1 退出):");
            int val=Integer.parseInt(in.readLine());
            if(val==-1) {
                System.out.println("退出查询操作!!!");
                break;
            }
            linkedList.search(val);
            System.out.println();
        }
        while(true) {
            System.out.print("输入 1 删除表头(输入 -1 退出):");
            int val=Integer.parseInt(in.readLine());
            if(val==-1) {
                System.out.println("退出删除表头操作!!!");
                break;
            }
            if(val==1)
            linkedList.deleteFirst(linkedList.first);
            linkedList.printLinkedList(linkedList.first);
            System.out.println();
        }
        while(true) {
            System.out.print("输入 1 删除表尾(输入 -1 退出):");
            int val=Integer.parseInt(in.readLine());
            if(val==-1) {
                System.out.println("退出删除表尾操作!!!");
                break;
            }
            if(val==1)
            linkedList.deleteLast(linkedList.first);
            linkedList.printLinkedList(linkedList.first);
            System.out.println();
        }
        while(true) {
            System.out.print("输入你想删除的节点整数值(输入 -1 退出):");
            int val=Integer.parseInt(in.readLine());
            if(val==-1) {
                System.out.println("退出删除某值操作!!!");
                break;
            }
            if(val==1)
            linkedList.delete(val);
            linkedList.printLinkedList(linkedList.first);
            System.out.println();
        }
        // TODO Auto-generated method stub
    }

}

相关文章

  • 单链表 C++

    单链表 C++ 题目 1、创建单链表2、初始化单链表3、释放单链表4、获取单链表中元素的数量5、输出单链表中的所有...

  • 线性表:顺序表和链表

    顺序表(数组)优缺点 链表优点 单链表使用 单链表结构 单链表初始化 单链表初始化 单链表建立: 头插法 尾插法 ...

  • 单向链表算法

    单向链表 反转单向链表 单链表查找倒数第k个节点 单链表递归倒序打印 单链表排序 单链表删除重复节点

  • 链表基本操作

    1、删除单链表节点 2、插入单链表结点 单链表具体实现

  • 25_静态单链表的实现

    关键词: 单链表的一个缺点、静态单链表设计思路、静态单链表的继承层次结构、静态单链表的实现思路、静态单链表的实现 ...

  • 线性表的链式存储-单链表

    单链表操作 [x] 单链表的创建(尾插法、头插法) [x] 单链表的查找操作 [x] 单链表的删除操作 [x] 单...

  • Algorithm小白入门 -- 单链表

    单链表递归反转链表k个一组反转链表回文链表 1. 递归反转链表 单链表节点的结构如下: 1.1 递归反转整个单链表...

  • 单链表反转

    单链表 单链表反转 递归方法

  • JavaScript数据结构2——单链表

    以下的代码包括了以下几部分 单链表初始化 单链表的插入 单链表的删除 单链表的创建(头插法) 单链表的创建(尾插法...

  • 链表

    链表:通过“指针”将零散的内存块联系起来。常见链表结构:单链表、循环链表和双链表。 单链表 对比数组学习单链表 循...

网友评论

    本文标题:单链表

    本文链接:https://www.haomeiwen.com/subject/mtsacqtx.html