在数据结构的世界中,链表是一种非常基础且重要的数据结构。今天,我们将深入探讨一个自定义的链表实现 - CustomLinkedList
类。
一、链表的节点定义
在 CustomLinkedList
类中,首先定义了一个内部类 Node
来表示链表的节点。每个节点包含了数据 data
和指向下一个节点的引用 next
。通过构造函数 Node(Object data)
,可以方便地创建一个新的节点,并初始化数据和将 next
指针指向空。
class Node{
Object data;
Node next;
public Node(Object data){
this.data = data;
this.next = null;
}
}
二、链表的基本属性与构造函数
CustomLinkedList
类具有两个重要的属性:head
表示链表的头节点,size
记录链表中节点的数量。
构造函数 CustomLinkedList()
用于初始化一个空的链表,将 head
设置为 null
,size
设置为 0
。
private Node head;
private int size;
public CustomLinkedList(){
head = null;
size = 0;
}
三、链表的基本操作方法
1. 判空操作 isEmpty()
通过判断 size
是否为 0
,来确定链表是否为空。
public boolean isEmpty(){
return size == 0;
}
2. 获取链表大小 size()
返回链表中节点的数量。
public int size(){
return size;
}
3. 新增节点 add(Object data)
用于向链表中添加新的节点。如果链表为空,将新节点设置为头节点;否则,遍历链表找到最后一个节点,将新节点添加到其后面。
public void add(Object 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;
}
size++;
}
4. 获取指定索引的节点数据 get(int index)
首先检查索引是否合法,如果索引超出范围,将抛出 IndexOutOfBoundsException
异常。然后通过遍历链表,找到指定索引的节点,并返回其数据。
public E get(int index){
if (index < 0 || index >= size){
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
Node current = head;
for(int i = 0; i < index; i++){
current = current.next;
}
return (E) current.data;
}
5. 删除指定索引的节点 remove(int index)
同样先检查索引的合法性。如果要删除的是头节点,直接将 head
指向下一个节点;否则,找到要删除节点的前一个节点,将其 next
指针指向要删除节点的下一个节点。
public void remove(int index){
if (index < 0 || index >= size){
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
if(index == 0){
head = head.next;
}else {
Node current = head;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = current.next.next;
}
size--;
}
6. 清空链表 clear()
将 head
设置为 null
,并将 size
重置为 0
,实现链表的清空操作。
public void clear(){
head = null;
size = 0;
}
7. 打印链表 print()
通过遍历链表,将每个节点的数据打印出来,最后打印 null
表示链表结束。
public void print(){
Node current = head;
while(current!= null){
System.out.println(current.data);
current = current.next;
}
System.out.println("null");
}
四、示例演示
在 main
方法中,我们对自定义的链表进行了一系列的操作,包括添加元素、删除元素、获取元素、获取链表大小、清空链表和打印链表等,来验证我们的链表实现的正确性和功能。
public static void main(String[] args) {
CustomLinkedList<Integer> list = new CustomLinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
System.out.println("Initial LinkedList:");
list.print(); // 打印初始链表
list.remove(1); // 删除第二个元素
System.out.println("\nAfter removing element at index 1:");
list.print(); // 打印删除后的链表
System.out.println("\nElement at index 1: " + list.get(1)); // 获取索引为 1 的元素
System.out.println("Size of LinkedList: " + list.size()); // 获取链表大小
list.clear(); // 清空链表
System.out.println("\nAfter clearing the LinkedList:");
list.print(); // 打印清空后的链表
}
通过这段自定义链表的实现与示例演示,我们对链表的基本概念和操作有了更深入的理解和认识。希望这个自定义链表对您在数据结构的学习和应用中有所帮助!
四、可以直接运行的代码
ublic class CustomLinkedList<E> {
class Node{
//定义数据
Object data;
//定义next节点
Node next;
public Node(Object data){
//构造函数
this.data = data;
//next指针指向空
this.next = null;
}
}
/**
* 数据节点
*/
private Node head;
/**
* 数据大小
*/
private int size;
/**
* 构造函数
*/
public CustomLinkedList(){
head = null;
size = 0;
}
/**
* 判空
* @return
*/
public boolean isEmpty(){
return size == 0;
}
/**
* 获取大小
* @return
*/
public int size(){
return size;
}
/**
* 新增
* @param data
*/
public void add(Object data){
//创建一个新的节点
Node newNode = new Node(data);
//如果是第一个元素
if(head == null){
//那么将newNode给第一个元素
head = newNode;
}else {
//如果不是,读取head
Node current = head;
//循环链表,这也是它插入效率低的原因
while(current.next != null){
//寻找最后一个元素
current = current.next;
}
//找到最后一个元素复制新的节点
current.next = newNode;
}
size++;
}
public E get(int index){
if (index < 0 || index >= size){
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
Node current = head;
for(int i = 0; i < index; i++){
current = current.next;
}
return (E) current.data;
}
/**
* 删除
* @param index
*/
public void remove(int index){
if (index < 0 || index >= size){
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
if(index == 0){
head = head.next;
}else {
Node current = head;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = current.next.next;
}
size--;
}
/**
* 清理
*/
public void clear(){
head = null;
size = 0;
}
/**
* 打印
*/
public void print(){
Node current = head;
while(current != null){
System.out.println(current.data);
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
CustomLinkedList<Integer> list = new CustomLinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
System.out.println("Initial LinkedList:");
list.print(); // 打印初始链表
list.remove(1); // 删除第二个元素
System.out.println("\nAfter removing element at index 1:");
list.print(); // 打印删除后的链表
System.out.println("\nElement at index 1: " + list.get(1)); // 获取索引为1的元素
System.out.println("Size of LinkedList: " + list.size()); // 获取链表大小
list.clear(); // 清空链表
System.out.println("\nAfter clearing the LinkedList:");
list.print(); // 打印清空后的链表
}
}
关注公众号:资小库,问题快速答疑解惑