java实现单向链表的增删改查

java实现单向链表的增删改查

package com.eshore.ac.batch;

import com.eshore.ac.util.DESUtil;
import com.eshore.ac.util.ObjectUtil;
import org.junit.Test;

import java.util.*;


public class demoTest {
          
   
    private class Node {
          
   
        private Object obj;
        private Node next = null;

        Node(Object obj){
            this.obj = obj;
        }
    }
    private Node head = null;      //头结点

    // 插入操作
    public void add(Object obj) {
        Node node=new Node(obj);
        node.next=head;
        head=node;
    }

    // 删除
    public Object deletehead() throws Exception{
        if(head==null){
            throw new  Exception("this link is Empty");
        }
        Node temp=head;
        head=head.next;
        return temp.obj;
    }

    // 遍历
    public void display() throws Exception{
        if(head==null){
            throw new  Exception("this link is Empty");
        }

        Node cur=head;
        while(cur!=null){
            System.out.print(cur.obj+"--->");
            cur=cur.next;
        }
        System.out.print("
");
    }
    // 判断是否为空
    public boolean isEmpty() {
        return (head==null);
    }

    // 查找某个结点
    public boolean contains(Object obj) throws Exception{
        if (head==null){
            throw new Exception("this link is Empty");
        }
        Node cur=head;
        while(cur!=null){
            if(cur.obj.equals(obj)){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    // 删除一个链表中包含obj的所有元素
    public void remove(Object obj) throws Exception{
        if (head==null){
            throw new Exception("this link is Empty");
        }

        if(head.obj.equals(obj)){
            head=head.next;
            remove(obj);
        }else {
            Node pre=head;
            Node cur=head.next;
            while(cur!=null){
                if(cur.obj.equals(obj)){
                    pre.next=cur.next;
                }
                pre=cur;
                cur=cur.next;
            }
        }
    }

    public void removeDuplicate(Node head) throws Exception{
        if (head==null){
            throw new Exception("this link is Empty");
        }

        demoTest newListNode=new demoTest();
        Node pre=head;
        Node cur=head.next;
        newListNode.add(pre.obj);
        while(cur!=null){
            if(!newListNode.contains(cur.obj)){
                newListNode.add(cur.obj);
                pre=cur;
                cur=cur.next;
            }else {
                pre.next=cur.next;
                cur=cur.next;
            }
        }
    }


    @Test
    public  void test() throws Exception {
        demoTest listNode = new demoTest();
        listNode.add(1);
        listNode.add(1);
        listNode.add(2);
        listNode.add(3);
        listNode.add(2);
        listNode.add(3);
        listNode.add(3);

        //删除重复的节点
        listNode.display();
        listNode.removeDuplicate(listNode.head);
        listNode.display();
//        listNode.remove(2);
//        listNode.display();
//        listNode.remove(3);
//        listNode.display();

    }
}
经验分享 程序员 微信小程序 职场和发展