java 链表操作,删除尾结点,指定位置结点
2018-01-17 12:30
274 查看
这两天随便看了看 java 的链表,发现了一些有意思的事情。
一个单向连边一般只有一个头结点,头结点的结点指向,然后是链表的大小了。
若要对其他结点进行操作,必须对链表进行遍历,找到这个结点,然后进行相关操作。
遍历的代码一般是:
链表的相关操作:
1. 添加新的头结点
2. 删除尾结点
3. 删除指定位置结点
4. 在指定位置添加结点
5. 查找链表里是否含有某个值
我自己编写的一个单向链表如下:
输出结果:
4 3 2 1
4 3 2
4 2
一个单向连边一般只有一个头结点,头结点的结点指向,然后是链表的大小了。
若要对其他结点进行操作,必须对链表进行遍历,找到这个结点,然后进行相关操作。
遍历的代码一般是:
for (Node x = first; x != null; x = x.next){ // 处理 x.item; }
链表的相关操作:
1. 添加新的头结点
2. 删除尾结点
3. 删除指定位置结点
4. 在指定位置添加结点
5. 查找链表里是否含有某个值
我自己编写的一个单向链表如下:
/** * @author chen zhen * @version 创建时间:2018年1月17日 上午10:56:50 * @value 类说明: 链表的一些操作 */ public class MyLink { private Node first; // 一个头结点 private int N; // 链表长度 public MyLink() { first = null; N = 0; } /** * 自定义一个节点类 */ public static class Node{ int item; Node next; } // 链表表头添加一个新的结点 public void addNode(int i) { Node temp = first; first = new Node(); first.item = i; first.next = temp; N++; } // 删除一个尾结点 public void deleteLastNode() { if (N == 0) return; if (N == 1) first = null; else for (Node x = first; x != null; x = x.next) { if (x.next.next == null) x.next = null; } N--; } // 删除指定位置的结点 public void deletePosition(int position) { if (position == 1) { first = null; return; } if (position > N) System.out.println("the position is too large"); else { int index = 1; // 记录遍历的位置 for (Node x = first; x != null; x = x.next) { if (index + 1 == position) { x.next = x.next.next; break; } index++; } } } //在指定位置添加结点 public void addToPosition(int position, int value) { if (position > N + 1) System.out.println("the position is too large"); else { int index = 1; for (Node x = first; x != null; x = x.next) { if (index + 1 == position) { Node temp = new Node(); temp.item = value; temp.next = x.next; x.next = temp; break; } index++; } } } // 查找链表里是否含有某个值 public boolean find(int key) { for (Node x = first; x != null; x = x.next) { if (x.item == key) return true; } return false; } // 定义一个 toString,方便打印 public String toString() { StringBuilder str = new StringBuilder(); for (Node x = first; x != null; x = x.next) { str.append(x.item); str.append(" "); } return str.toString(); } public static void main(String[] args) { MyLink link = new MyLink(); link.addNode(1); link.addNode(2); link.addNode(3); link.addNode(4); System.out.println(link.toString()); link.deleteLastNode(); System.out.println(link.toString()); link.deletePosition(2); System.out.println(link.toString()); link.addToPosition(2, 10); System.out.println(link.toString()); System.out.println(link.find(2)); } }
输出结果:
4 3 2 1
4 3 2
4 2
相关文章推荐
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- 数据结构与算法分析笔记与总结(java实现)--链表14:删除链表中所有指定值结点问题
- 头结点链表的添加结点,删除结点,链表逆序,删除指定数据等操作的实现
- YTU.3174: 链表基本操作---指定结点删除
- leetcode解题之237# Delete Node in a Linked List Java版 (删除链表中指定的结点)
- java实现创建链表以及插入节点,查找结点,删除节点等操作
- 单链表的基本操作(尾插,尾删,头插,头删,查找,指定位置插入、删除,指定元素删除、全部删除等)
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 剑指offer:在O(1)时间删除链表结点(java)
- java实现链表指定值得删除,查找第二部分代码
- Java上传图片到指定位置,删除图片
- 在O(1)时间内删除链表指定结点
- 关于线性表的链式存储(链表)的创建,头插法,尾插法,删除链表结点的操作
- java单链表基本操作(二)--指定位置增加节点
- 剑指offer算法题之单链表的删除结点操作--面试题13:在O(1)时间删除链表结点
- 数据结构(三) Java实现静态链表的顺序新增、指定位置新增和删除
- 链表中删除指定结点的两种方式
- Java-集合 list对象进行指定位置插入、删除、迭代、遍历输出(具体习题讲解)
- 删除链表中重复的结点(java版)
- 【链表面试题】删除无头单链表的非尾节点,插入一个元素到无头链表指定位置