JAVA单链表操作

JAVA单链表操作

java单链表图示
JAVA单链表操作_第1张图片

import java.util.Hashtable;
import java.util.Scanner;

class Node{
    public int data;
    public Node next = null;

    public Node(int data){
        this.data = data;
    }
}
//单向链表
public class Main {

    public static Node head = null;

    public static void addNode(int d){
        //新建一个节点
        Node newNode = new Node(d);
        //链表为空
        if(null == head){
            head = newNode;
            return;
        }
        //循环找到链表的末尾
        Node tmp = head;
        while (tmp.next != null){
            tmp = tmp.next;
        }
        tmp.next = newNode;
    }

    public static int length() {
        int length = 0;
        Node tmp = head;
        while (tmp.next != null) {
            length ++;
            tmp = tmp.next;
        }
        return length;
    }

    //根据索引删除节点
    public static boolean deleteNode(int index){
        if(index < 1 || index > length()){
            return false;
        }
        //特判删除头指针,因为单向链表没有前驱节点,删除比较简单
        if(index == 1){
            head = head.next;
            return true;
        }
        int i = 1;
        Node preNode = head;
        Node curNode = head.next;
        while (curNode != null){
            //找到索引的位置,将i对应的节点删除,
            //i的前驱节点指向index的后继节点(删除第index个节点)
            if(i == index){
                preNode.next = curNode.next;
                return true;
            }
            preNode = curNode;
            curNode = curNode.next;
            i++;
        }
        return true;
    }

    public static void insertNodeByIndex(int index,int nod){
        //首先需要判断指定位置是否合法,
        if(index < 1 || index > length() + 1){
            System.out.println("插入位置不合法。");
            return;
        }

        Node node = new Node(nod);

        int length = 1;           //记录我们遍历到第几个结点了,也就是记录位置。
        Node temp = head;         //可移动的指针
        while(temp.next != null){ //遍历单链表
            if(index == length++){        //判断是否到达指定位置
                //temp代表的是当前位置的前一个结点。
                //前一个结点        当前位置        后一个结点
                //temp            temp.next     temp.next.next
                //插入操作。
                node.next = temp.next;
                temp.next = node;
                return;
            }
            temp = temp.next;
        }
    }


    public static boolean isEmpty(){

        return null == head ? true : false;
    }

    public static Node orderList(){
        int tmp = 0;
        Node curNode = head;
        Node nextNode = null;
        while (curNode.next != null){
            nextNode = curNode.next;
            //从小到大排序
            while(nextNode != null){
                if(curNode.data > nextNode.data){
                    tmp = curNode.data;
                    curNode.data = nextNode.data;
                    nextNode.data = tmp;
                }
                nextNode = nextNode.next;
            }
            curNode = curNode.next;
        }
        return head;
    }

    /**
     * 去掉重复元素:
     * 需要额外的存储空间hashtable,调用hashtable.containsKey()来判断重复结点
     */
    public static void distinctLink(){
        Node temp = head;
        Node pre = null;
        Hashtable hb = new Hashtable();
        while(temp != null){
            if(hb.containsKey(temp.data)){//如果hashtable中已存在该结点,则跳过该结点
                pre.next = temp.next;
            }else{//如果hashtable中不存在该结点,将结点存到hashtable中
                hb.put(temp.data, 1);
                pre = temp;
            }
            temp = temp.next;
        }
    }

    /**
     * 返回倒数第k个结点,
     * 两个指针,第一个指针向前移动k-1次,之后两个指针共同前进,
     * 当前面的指针到达末尾时,后面的指针所在的位置就是倒数第k个位置
     */
    public static Node findReverNode(int k){
        if(k<1 || k>length()){//第k个结点不存在
            return null;
        }
        Node first = head;
        Node second = head;
        for(int i=0; ilength()){//不合法的k
            return null;
        }
        Node temp = head;
        for(int i = 0; i

https://www.cnblogs.com/tengdai/p/9279421.html

你可能感兴趣的:(java数据结构)