数据结构--链表

链表是一种由节点组成的集合,每个节点包含数据部分(value)和只想下一个节点的链接(next)。JAVA中的LinkedList实现了List类,允许添加、删除和访问元素。

链表的创建:

//创建节点,--类(包含value、next)
package lianbiao;
public class ListNode {
	int value;
	ListNode next;
	public ListNode(int value) {
		super();
		this.value = value;
	}
	
	public String toString() {
		return "ListNode [value="+ value +",next="+ next +"]";
	}
}
package lianbiao;
import java.awt.List;
public class Test {
	public static void main(String[] args) {
		ListNode node = new ListNode(5);
		ListNode node1 = new ListNode(7);
		ListNode node2 = new ListNode(4);
		ListNode node3 = new ListNode(2);
		ListNode node4 = new ListNode(0);
		//手动构建链表
		node.next = node1;
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;
		System.out.println(node);

	}
}

链表的插入方法:头插法和尾插法

    //头插法
    public void insertHead(int data){
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;
    }

    //尾插法
    public void insertTail(int data){
        Node newNode = new Node(data);
        //链表为空
        if(head == null){
            head = newNode;
        }
        else{
            Node current = head;
            while(current.next!=null){
                current = current.next;
            }
            current.next = newNode;
        }
    }

链表的删除:

    public void delete(int data) {
        if (head == null){
             return;    
        }

        // 删除头节点
        if (head.data == data) {
            head = head.next;
            return;
        }

        Node current = head;
        while (current.next != null) {
            if (current.next.data == data) {
                current.next = current.next.next;
                return;
            }
            current = current.next;
        }
    }

链表的修改:

    // 修改元素
    public void update(int oldData, int newData) {
        Node current = head;
        while (current != null) {
            if (current.data == oldData) {
                current.data = newData;
                return;
            }
            current = current.next;
        }
    }

链表的访问

    // 访问元素
    public int get(int index) {
        Node current = head;
        int count = 0;
        while (current != null) {
            if (count == index) {
                return current.data;
            }
            count++;
            current = current.next;
        }
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + count);
    }

综合:

public class LinkedList {
    private Node head; // 头节点

    // 定义节点类
    private class Node {
        int data;
        Node next;
        // 节点类
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    // 头插法
    public void insertHead(int data) {
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;
    }

    // 尾插法
    public void insertTail(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    // 删除元素
    public void delete(int data) {
        if (head == null) return;

        // 删除头节点
        if (head.data == data) {
            head = head.next;
            return;
        }

        Node current = head;
        while (current.next != null) {
            if (current.next.data == data) {
                current.next = current.next.next;
                return;
            }
            current = current.next;
        }
    }

    // 修改元素
    public void update(int oldData, int newData) {
        Node current = head;
        while (current != null) {
            if (current.data == oldData) {
                current.data = newData;
                return;
            }
            current = current.next;
        }
    }

    // 访问元素
    public int get(int index) {
        Node current = head;
        int count = 0;
        while (current != null) {
            if (count == index) {
                return current.data;
            }
            count++;
            current = current.next;
        }
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + count);
    }

    public void print() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();

        // 头插法示例
        list.insertHead(10);
        list.insertHead(20);
        list.insertHead(30);
        System.out.print("链表(头插法):");
        list.print();

        // 尾插法示例
        list.insertTail(40);
        list.insertTail(50);
        System.out.print("链表(尾插法):");
        list.print();

        // 删除元素示例
        list.delete(20);
        System.out.print("删除元素20后的链表:");
        list.print();

        // 修改元素示例
        list.update(30, 30);
        System.out.print("修改元素30为30后的链表:");
        list.print();

        // 访问元素示例
        try {
            int data = list.get(1);
            System.out.print("访问索引1处的元素:" + data);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("访问索引失败:" + e.getMessage());
        }
    }
}

运行结果:

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值