首页 / JAVA / Java实现单向链表简单的增删查操作
Java实现单向链表简单的增删查操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java实现单向链表简单的增删查操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4366字,纯文字阅读大概需要7分钟。
内容图文
![Java实现单向链表简单的增删查操作](/upload/InfoBanner/zyjiaocheng/597/25c314e3ac84497d9d722b33c3e6fbb7.jpg)
目录
链表的概念及结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
就拿带头单向非循环链表来讲。
val就是数值,可以随便给。
next连接下一个地址,这样就可以串联起来了。
地址可以是无序的,不像顺序表一样它的地址必须是有序的。
创建节点类
class ListNode{
public int val;
public ListNode next;
//构造方法只需要传val值就可以了。
public ListNode(int val) {
this.val = val;
}
}
定义头节点
public ListNode head;
插入
头插法
public void headInsert(int val) {
//首先new一个节点
ListNode node = new ListNode(val);
//判断链表是否为空
if(empty()) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
}
尾插法
尾插法:如果链表为空就让头节点指向这个new出来的节点,如果链表不为空,定义一个cur,在cur.next不为空的情况下,让cur = cur.next,走到最后一个节点,让最后一节点的next指向node。
public void tailInsert(int val) {
ListNode node = new ListNode(val);
if(empty()) {
this.head = node;
} else {
ListNode cur = this.head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
任意位置插入
public void insert(int val,int n) {
ListNode node = new ListNode(val);
if(empty()) {
this.head = node;
}
if(n == 0) {
//使用头插法
headInsert(val);
return;
}
if(n == size()) {
//使用尾插法
tailInsert(val);
return;
}
ListNode cur = this.head;
int count = 1; //计数,与n相等时插入,默认下标从1开始
while(n - 1 != count) {
count++;
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
}
查找n位置节点
public ListNode findN(int n) {
if(this.head == null) return null;
if(n < 0 || n > size()) return null;
ListNode cur = this.head;
int count = 0;
while(cur != null) {
count++;
if(count == n) return cur;
cur = cur.next;
}
return null;
}
删除节点
public void delVal (int val) {
//判空
if(empty()) return;
if(this.head.val == val) {
this.head = this.head.next;
return;
}
ListNode prev = this.head;
ListNode cur = prev.next;
while(cur != null) {
if(cur.val == val) {
prev.next = cur.next;
} else {
prev = cur;
}
cur = cur.next;
}
}
其它方法
判空
public boolean empty() {
return this.head == null;
}
计算链表长度
public int size() {
int count = 0;
ListNode cur = this.head;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}
打印链表
public void display() {
ListNode cur = this.head;
while(cur != null) {
System.out.println(cur.val);
cur = cur.next;
}
}
完整代码
class ListNode{
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public class LinkedList {
public ListNode head;
//判断是否为空
public boolean empty() {
return this.head == null;
}
//计算链表长度
public int size() {
int count = 0;
ListNode cur = this.head;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}
//打印链表
public void display() {
ListNode cur = this.head;
while(cur != null) {
System.out.println(cur.val);
cur = cur.next;
}
}
//头插法
public void headInsert(int val) {
//首先new一个节点
ListNode node = new ListNode(val);
//判断链表是否为空
if(empty()) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
}
//尾插法
public void tailInsert(int val) {
ListNode node = new ListNode(val);
if(empty()) {
this.head = node;
} else {
ListNode cur = this.head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
//任意位置插入
public void insert(int val,int n) {
ListNode node = new ListNode(val);
if(empty()) {
this.head = node;
}
if(n == 0) {
//使用头插法
headInsert(val);
return;
}
if(n == size()) {
//使用尾插法
tailInsert(val);
return;
}
ListNode cur = this.head;
int count = 1; //计数,与n相等时插入,默认下标从1开始
while(n - 1 != count) {
count++;
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
}
//查找n位置的节点
public ListNode findN(int n) {
if(this.head == null) return null;
if(n < 0 || n > size()) return null;
ListNode cur = this.head;
int count = 0;
while(cur != null) {
count++;
if(count == n) return cur;
cur = cur.next;
}
return null;
}
//删除节点
public void delVal (int val) {
//判空
if(empty()) return;
if(this.head.val == val) {
this.head = this.head.next;
return;
}
ListNode prev = this.head;
ListNode cur = prev.next;
while(cur != null) {
if(cur.val == val) {
prev.next = cur.next;
} else {
prev = cur;
}
cur = cur.next;
}
}
public static void main(String[] args) {
LinkedList myList = new LinkedList();
myList.headInsert(2);
myList.headInsert(1);
myList.tailInsert(3);
myList.insert(0,0);
myList.insert(5,3);
System.out.println(myList.size());
System.out.println(myList.findN(3).val);
myList.delVal(0);
myList.display();
}
}
内容总结
以上是互联网集市为您收集整理的Java实现单向链表简单的增删查操作全部内容,希望文章能够帮你解决Java实现单向链表简单的增删查操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。