单向链表

package com.cxy.springdataredis.data;

import ch.qos.logback.core.net.SyslogOutputStream;
import sun.reflect.generics.reflectiveObjects.LazyReflectiveObjectGenerator;

import java.util.Stack;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        Node node =new Node(1,"老外","1111");
        Node node1 =new Node(2,"老外1","2222");
        Node node2 =new Node(3,"老外2","3333");
        Node node3 =new Node(3,"老外333","3333");
        Node node4 =new Node(4,"老外3","4444");
        SingleLinkedList linkedList = new SingleLinkedList();
        /*linkedList.add(node);
        linkedList.add(node1);
        linkedList.add(node2);
        linkedList.add(node4);*/
        linkedList.addByOrder(node);
        linkedList.addByOrder(node4);
        linkedList.addByOrder(node2);
        linkedList.addByOrder(node1);
        reverseList(linkedList.getHead());
        linkedList.list();
        System.out.println("----------------");
        linkedList.update(node3);
        linkedList.list();
        System.out.println("----------------");
        linkedList.deleteNode(1);
        linkedList.list();
        System.out.println("----------------");
        linkedList.deleteNode(4);
        linkedList.list();
        System.out.println(getLength(linkedList.getHead()));

        System.out.println(fingLastIndexNode(linkedList.getHead(),2));

    }
    public static void reversePrint(Node head) {
        if(head.next == null) {
            return;//空链表,不能打印
        }
        //创建要给一个栈,将各个节点压入栈
        Stack stack = new Stack();
        Node cur = head.next;
        //将链表的所有节点压入栈
        while(cur != null) {
            stack.push(cur);
            cur = cur.next; //cur后移,这样就可以压入下一个节点
        }
        //将栈中的节点进行打印,pop 出栈
        while (stack.size() > 0) {
            System.out.println(stack.pop()); //stack的特点是先进后出
        }
    }

    public static void reverseList(Node head){
        if (head.next ==null||head.next.next ==null){
            return ;
        }
        Node temp =head.next;//定义一个辅助的指针,帮助我们来遍历原来的链表
        Node next =null;//
        Node revsrseHead =new Node(0,"","");
        while (temp != null){
            next =temp.next;//保存当前节点的下一个节点
            temp.next =revsrseHead.next;//将temp的下一个节点指向新的链表的最前端
            revsrseHead.next =temp;
            temp =next;//让temp指向下一个
        }
        head.next =revsrseHead.next;
    }
    //获取链表的个数
    public  static int getLength(Node head){
        if (head.next ==null){
            return 0;
        }
        int length =0;
        Node temp =head.next;
        while (temp!=null){
            length++;
            temp =temp.next;
        }
        return length;
    }

    public static Node fingLastIndexNode(Node head,int index){
        Node temp =head.next;
        if (temp ==null){
            return null;
        }
        int length = getLength(head);
        if (index<=0&&index>length){
            return null;
        }
        for (int i =0;i){
            temp =temp.next;
        }
    return temp;

    }

}
class SingleLinkedList{
    //先初始化头节点,头节点不要动 不存放具体数据
    private Node head =new Node(0,"","");

    public Node getHead() {
        return head;
    }


    //添加节点到单项链表
    //思路 当不考虑编号顺序是
    // 找到当前链表的最后节点
    //找到最后这个节点的next指向的新节点
    public  void add(Node node){
        Node temp =head;
        while(true){
            if (temp.next ==null){
                break;
            }
            temp =temp.next;
        }
        temp.next =node;
    }

    public void addByOrder(Node node){
        //头节点不能动,因此只能通过变量找到添加的位置
        //因为单链表,我们必须要找到添加位置的下一个节点,否则插入不了
        Node temp =head;
        boolean flag =false;
        while (true){
            if (temp.next ==null){
                break;
            }
            if (temp.next.num >node.num){
                break;
            }else if(temp.next.num ==node.num){
                flag =true;
                break;
            }
            temp =temp.next;
        }
        if (flag){
            System.out.println("插入的编码已经存在");
        }else{
            node.next=temp.next;
            temp.next =node;
        }
    }

    public  void  update (Node node){
        Node temp =head.next;
        if (temp ==null){
            System.out.println("链表为空");
            return;
        }
        boolean flag =true;
        while (true){
            if (temp == null){
                break;
            }
            if (temp.num ==node.num){
                flag =true;
                break;
            }
            temp =temp.next;
        }
        if (flag){
            temp.nickname =node.nickname;
            temp.name =node.name;
        }
    }

    public  void  deleteNode(int num){
        Node temp =head;
        boolean flag =false;
        while(true){
            if (temp.next ==null){
                System.out.println("当前链表为空");
            }
            if (temp.next.num ==num){
                flag =true;
                break;
            }
            temp =temp.next;
        }
        if (flag){
            temp.next =temp.next.next;
        }else {
            System.out.println("删除的节点不存在");
        }
    }
    //遍历链表
    public  void list(){
        //判断链表是否为空
       if (head.next ==null){
           System.out.println("链表为空");
           return;
       }
       //定义零食变量,定义头节点,辅助遍历
       Node temp =head.next;
       while(true){
           //判断链表是否为空
           if (temp ==null){
               break;
           }
           //打印链表
           System.out.println(temp);
           temp =temp.next;

       }
    }


}

//定义节点
class Node{
    public int num;
    public String name;
    public String nickname;
    public Node next;

    //构造器
    public Node(int num,String name ,String nickname){
        this.num =num;
        this.name =name;
        this.nickname=nickname;
    }

    @Override
    public String toString() {
        return "Node{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}

 

你可能感兴趣的:(单向链表)