当前位置:网站首页>链表第三篇
链表第三篇
2022-04-22 04:31:00 【Martin__Liu】
代码:
package Package01;
public class Slist {
public static void main(String[] args) throws Exception{
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.insert(3,0);
myLinkedList.insert(2,1);
myLinkedList.insert(4,2);
myLinkedList.insert(4,3);
myLinkedList.output();
System.out.println("++++++++++");
myLinkedList.insert(4,4);
myLinkedList.insert(4,5);
myLinkedList.insert(4,6);
myLinkedList.output();
System.out.println("++++++++++");
myLinkedList.remove(0);
myLinkedList.output();
}
// private static class Node{ //定义了一个内部类
// int data;
// Node next; //next为Node类型的对象,这里的next就可以引用Node类型的对象
//
// }
}
class MyLinkedList{
//头节点指针
private Node head;
//尾节点指针
private Node last;
//链表实际长度
private int size;
//向链表里面添加元素函数
public void insert(int data, int index) throws Exception{
if(index<0||index>size){
throw new IndexOutOfBoundsException("超出链表的范围!");
} //注意这里提出的异常是 IndexOutOfBoundsException
Node insertNode = new Node(data); //咱们新建一个节点,用于指向所到的节点的位置,方便遍历
if(size==0){
//空链表
head = insertNode;
last = insertNode;
}else if(index==0){
//插入头部
}else if(index==size){
//索引插入的位置是size,那么就是指的是最后的一个位置
//插入尾部
last.next = insertNode;
last = insertNode;
}else{
//插入中间位置
Node preNode = get(index-1);
insertNode.next = preNode.next;
preNode.next = insertNode;
}
size++;
}
//在链表里面删除元素
public Node remove(int index) throws Exception {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("超出链表的范围!");
} //注意这里提出的异常是 IndexOutOfBoundsException
Node removedNode = null;
if (index == 0) {
//删除头节点
removedNode = head; //试一下,如果没有这一句会出现什么问题
head = head.next;
}else if(index==size-1){
//删除尾节点
Node preNode = get(index-1);
removedNode = preNode.next; //试一下,如果没有这一句会出现什么问题
preNode.next = null;
last = preNode;
}else{
//删除中间节点
Node preNode = get(index-1);
Node nextNode = get(index+1);
removedNode = preNode.next;
preNode.next = nextNode;
}
size--;
return removedNode;
}
//通过索引得到第几个节点的函数
public Node get(int index)throws Exception{
if(index<0||index>=size){
throw new IndexOutOfBoundsException("超出链表的节点范围");
}
Node temp = head;
for(int i=0; i<index; i++){
temp = temp.next;
}
return temp;
}
public void output(){
Node temp = head;
while(temp!=null){
System.out.println(temp.data);
temp = temp.next;
}
}
private static class Node{
//定义了一个内部类
int data;
Node next; //next为Node类型的对象,这里的next就可以引用Node类型的对象
Node(int data){
this.data = data;
} //创建构造函数
}
}
输出:
"C:\Program Files\Java\jdk1.8.0_152\bin\java.exe" "-javaagent:D:\软件下载\idea\idea\IntelliJ IDEA 2021.1\lib\idea_rt.jar=56911:D:\软件下载\idea\idea\IntelliJ IDEA 2021.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;E:\刘建成\java\project\HSP\out\production\Modulate03" Package01.Slist
3
2
4
4
++++++++++
3
2
4
4
4
4
4
++++++++++
2
4
4
4
4
4
Process finished with exit code 0
版权声明
本文为[Martin__Liu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_25355771/article/details/124306594
边栏推荐
- How to solve the problem that the table association is not displayed when importing SQL from powerdesipowerdesigner
- 02-SparkSQL
- L1-046 divide singles (20 points)
- iptables使用
- Matplotlib draw 3dbox
- 论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
- AT32 MCU Audio 24bit例程
- Redis 的过期数据会被立马删除么?
- 软件测试成行业“薪”贵?
- L1-048 matrix a multiplied by B (15 points)
猜你喜欢

(sip-1-phone registration) analysis of the whole process of IP phone registration to PBX telephone exchange through SIP Protocol - how to see the message in Wireshark

LeetCode_ Rectangle_ Difficulties_ 391. Perfect rectangle

05-Aggregation

WinPcap get device list

SCI论文写作--IEEE期刊的Word模板(LaTeX也有)

06-Datetimes

sqlilabs(25a-26)

Experts have information | Zhang Zuyou: Tencent cloud devsecops practice and open source governance exploration

01-Read&Write

06-Datetimes
随机推荐
JVM简记
Using random function to change the random range
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
7_ Data analysis - Evaluation
02-SparkSQL
2022山东省安全员C证特种作业证考试题库及答案
Cursor iterator mode
Opensca version upgrade | opensca v1.0 Release of version 0.4
L1-052 2018 we want to win (5 points)
jeesite导出Excel
When calling a function, what about passing parameters~
Zuo Chengyun - Dachang question brushing class - the minimum number of exchanges of one character on the left and another character on the right
[experience] Why does the IP address of HP printer start with 169.254
L1-054 blessing arrived (15 points)
ES6 practical tips
01 knapsack problem (two-dimensional array solution and one bit array optimization)
Cilcate environment construction
Filebeat collects log data and transfers it to redis. Different es indexes are created according to log fields through logstash
SCI论文写作--IEEE期刊的Word模板(LaTeX也有)
When the SQL statement is executed by hyperf, the parameter will have two single quotes