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("\n");
    }
    // 判断是否为空
    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();

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单向链表是由若干个节点组成的,每个节点都包含一个数据域和一个指向下一个节点的指针域。在单向链表中,只能从头节点开始遍历链表,每个节点只能访问其后继节点。下面是用单向链表实现增删改查的代码实现: ```python # 定义单向链表节点类 class ListNode: def __init__(self, val): self.val = val self.next = None # 定义单向链表类 class LinkedList: def __init__(self): self.head = None # 添加节点 def add_node(self, val): new_node = ListNode(val) if not self.head: self.head = new_node else: curr = self.head while curr.next: curr = curr.next curr.next = new_node # 删除节点 def delete_node(self, val): if not self.head: return if self.head.val == val: self.head = self.head.next else: curr = self.head while curr.next: if curr.next.val == val: curr.next = curr.next.next return curr = curr.next # 修改节点 def update_node(self, old_val, new_val): curr = self.head while curr: if curr.val == old_val: curr.val = new_val return curr = curr.next # 查找节点 def search_node(self, val): curr = self.head while curr: if curr.val == val: return True curr = curr.next return False ``` 在上面的代码中,我们定义了一个单向链表节点类 ListNode ,包含一个数据域 val 和一个指向下一个节点的指针域 next 。然后定义了单向链表类 LinkedList ,包含一个头节点 head 。在 LinkedList 中,我们实现了 add_node() 方法用于添加节点,delete_node() 方法用于删除节点,update_node() 方法用于修改节点,search_node() 方法用于查找节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值