单向链表结构图
//模拟结点
public class Node {
//存放在data域中的数据
public int id;
public String name;
public double price;
//next域:指向下一个结点
public Node next;
public Node(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Node{id=" + id + ", name='" + name + "', price=" + price + '}';
}
}
//模拟单向链表
public class SingleLinklist {
//头节点
private Node headNode= new Node(0,"",0.0);
/** * 遍历单项链表 */
public void show(){
//如果头节点的next为空则说明当前链表为空
if (headNode.next == null) {
System.out.println("当前链表为空");
return;
}
Node temp = headNode;
while (true){
//当前结点没有下一个结点
if (temp.next == null){
break;
}
temp = temp.next;
System.out.println(temp);
}
}
}
这个方法写在SingleLinklist 类中
/** * 添加结点(追加在链表的尾端) */
public void add(Node goodsNode){
Node temp = headNode;
while(true){
if (temp.next == null) {
//如果这个结点的next域为空就跳出while循环
break;
}
//将下一个结点赋值给temp
temp = temp.next;
}
temp.next=goodsNode;
}
测试
public class LinkTest {
public static void main(String[] args) {
Node node1 = new Node(1,"nike",699);
Node node2 = new Node(2,"adidas",499);
Node node3 = new Node(3,"vans",399);
Node node4 = new Node(4,"lining",599);
SingleLinklist link = new SingleLinklist();
//测试按顺序插入
link.add(node1);
link.add(node2);
link.add(node3);
link.add(node4);
//遍历结点
link.show();
}
}
/** * 根据id进行添加(插入) */
public void addById(Node goodsNode){
//用来判断id是否重复
boolean flg = false;
Node temp = headNode;
while(true){
if (temp.next == null){
//当前结点没有下一个结点
break;
}
if (temp.next.id > goodsNode.id){
//当下一个结点的id值大于要插入结点的id值时跳出循环
break;
}else if(temp.next.id == goodsNode.id){
//id重复 默认id重复就不添加结点
flg = true;
break;
}
temp = temp.next;
}
if (flg){
//如果id重复就不插入结点
System.out.println("id重复,无法插入");
}else {
//插入结点
goodsNode.next = temp.next;
temp.next = goodsNode;
}
}
测试
public class LinkTest {
public static void main(String[] args) {
Node node1 = new Node(1,"nike",699);
Node node2 = new Node(2,"adidas",499);
Node node3 = new Node(3,"vans",399);
Node node4 = new Node(4,"lining",599);
SingleLinklist link = new SingleLinklist();
//测试按id值插入
link.addById(node4);
link.addById(node3);
link.addById(node2);
link.addById(node1);
link.show();
}
}
/** * 修改结点 * 1.先找到链表中的目标结点 * 2.根据新的数据修改结点 */
public void update(Node goodsNode){
if (headNode.next == null){
System.out.println("链表为空");
return;
}
Node temp = headNode;
//判断是否找到目标结点
boolean isFind = false;
while (true){
if (temp.next == null){
//遍历完整个链表还没找到目标结点
break;
}
if (temp.id == goodsNode.id){
//找到目标结点
isFind = true;
break;
}
temp = temp.next;
}
if (isFind){
//修改结点
temp.name = goodsNode.name;
temp.price = goodsNode.price;
}else{
System.out.println("没有找到目标找到目标结点");
}
}
测试
public class LinkTest {
public static void main(String[] args) {
Node node1 = new Node(1,"nike",699);
Node node2 = new Node(2,"adidas",499);
Node node3 = new Node(3,"vans",399);
Node node4 = new Node(4,"lining",599);
SingleLinklist link = new SingleLinklist();
//测试按顺序插入
link.add(node1);
link.add(node2);
link.add(node3);
link.add(node4);
//测试修改
Node node5 = new Node(2,"anta",299);
link.update(node5);
link.show();
}
}
/** * 根据id删除结点 */
public void delete(Node goodsNode){
if (headNode.next == null){
System.out.println("链表为空");
return;
}
Node temp = headNode;
boolean isFind = false;
while (true){
if (temp.next == null){
//遍历到整个链表还没找到目标结点
//System.out.println("没有找到目标找到目标结点");
break;
}
if (temp.next.id == goodsNode.id){
//找到目标结点
isFind = true;
break;
}
temp = temp.next;
}
if (isFind){
//删除结点
temp.next = temp.next.next;
}else{
System.out.println("没有找到目标找到目标结点");
}
}
测试
public class LinkTest {
public static void main(String[] args) {
Node node1 = new Node(1,"nike",699);
Node node2 = new Node(2,"adidas",499);
Node node3 = new Node(3,"vans",399);
Node node4 = new Node(4,"lining",599);
SingleLinklist link = new SingleLinklist();
//测试按顺序插入
link.add(node1);
link.add(node2);
link.add(node3);
link.add(node4);
//测试删除
link.delete(node3);
link.show();
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/m0_60117382/article/details/121792727
内容来源于网络,如有侵权,请联系作者删除!