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

classNode

{public intval;publicNode next;public Node(intval)

{this.val =val;

}

}classListHead

{public intcount ;publicNode next;publicListHead()

{this.count = 0;this.next = null;

}

}classList

{publicListHead head;privateNode current;publicList()

{this.head = newListHead();this.current = null;

}public void addNew(intval)

{

Node newNode= newNode(val);if(this.head.next == null)this.head.next =newNode;else

this.current.next =newNode;this.current =newNode;

}public boolean contains(intval)

{for(Node current = this.head.next; current != null; current =current.next)

{if(current.val ==val)return true;

}return false;

}public void replace(int val,intnewVal)

{for(Node current = this.head.next; current != null; current =current.next)

{if(current.val ==val)

{

current.val=newVal;return;

}

}

}public void replaceAll(int val, intnewVal)

{for(Node current = this.head.next; current != null; current =current.next)

{if(current.val ==val)

current.val=newVal;

}

}public void remove(intval)

{

Node theOneToBeRemoved= null;if(this.head.next != null && this.head.next.val ==val)

{

theOneToBeRemoved= this.head.next;this.head.next =theOneToBeRemoved.next;

theOneToBeRemoved.next= null;return;

}for(Node current = this.head.next; current != null; current =current.next)

{if(current.next != null && current.next.val ==val )

{

theOneToBeRemoved=current.next;

current.next=theOneToBeRemoved.next;

theOneToBeRemoved.next= null;return;

}

}

}public void removeAll(intval)

{

Node theOneToBeRemoved= null;while(this.head.next != null && this.head.next.val ==val)

{

theOneToBeRemoved= this.head.next;this.head.next =theOneToBeRemoved.next;

theOneToBeRemoved.next= null;

}for(Node current = this.head.next; current != null; current =current.next)

{while(current.next != null && current.next.val ==val)

{

theOneToBeRemoved=current.next;

current.next=theOneToBeRemoved.next;

theOneToBeRemoved.next= null;

}

}

}public voidclear()

{

Node theOneToBeRemoved= null;while(this.head.next != null)

{

theOneToBeRemoved= this.head.next;this.head.next =theOneToBeRemoved.next;

theOneToBeRemoved.next= null;

}

}public voidprintList()

{for(Node current = this.head.next; current != null; current =current.next)

{

System.out.print(current.val + " ");

}

System.out.println("\n=================================");

}

}public classHello

{public static voidmain(String[] args)

{

List myList= newList();

myList.addNew(1);

myList.addNew(1);

myList.addNew(1);

myList.addNew(1);

myList.addNew(1);

myList.addNew(2);

myList.addNew(1);

myList.addNew(2);

myList.addNew(1);

myList.addNew(2);

myList.addNew(7);

myList.addNew(7);

myList.addNew(8);

myList.addNew(2);

myList.printList();

myList.replaceAll(2,9);

myList.removeAll(7);

myList.printList();

}

}

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值