java实现带头结点的单链表的插入、删除、求长度
2018-09-18 15:53
246 查看
前段时间知乎面试,面试官出了一道题目实现单链表的插入、删除、计算长度,当时勉强写出来了,但是不是用的面向对象的思路写的,面试官叫我改成面向对象的方式。当时突然想到AQS的底层实现方式,就想仿照改一下。结果面试官说时间原因,就不写了。。。。。
AQS里面有一个头结点head和一个尾节点tail,刚开始head和tail都是没有初始化的,后面第一个进入阻塞对的第一个节点会在enq方法中初始化head,AQS中阻塞队列是不算head节点,head节点可以理解为当前拥有锁的节点。想了解更多的可以去看看AQS和ReentrantLock的源码。
思想我们也可以借鉴AQS,实现一个带头结点的单链表(注意头结点是不算在链表中),具体的代码如下:
[code]package cn.cqupt.linked; class Node { int data; Node next; Node() { } Node(int data) { this.data = data; this.next = null; } } public class LinkedOp { // 头结点 private Node head; // 链表的长度 private int size; // 构造函数初始化头结点,也可以在插入第一个节点的时候初始化 LinkedOp(){ head = new Node(); } // 头插法 public void insertFromHead(int data){ // 将data封装为Node节点 Node node = new Node(data); // 如果链表为空,直接跟在head后面 if(head.next == null){ head.next = node; size++; }else{ // 链表不为空,头插法插入节点 node.next = head.next; head.next = node; size++; } } // 尾插法 public void insertFromLast(int data) { Node node = new Node(data); if (head.next == null) { head.next = node; size++; } else { // 这里要注意的是,while的判断是current.next是否为空 // 意思 就是current最后肯定是指向链表的最后一个节点 Node current = head.next; while (current.next != null) { current = current.next; } // 直接跟到最后 current.next = node; // 长度+1 size++; } } // 获取链表长度 public int getSize(){ return size; } // 删除节点 public void remove(int data) { if (head.next == null){ System.out.println("当前链表为空"); return; } Node current = head; while (current.next != null) { if(current.next.data == data){ // current.next = current.next.next; Node removeNode = current.next; current.next = removeNode.next; removeNode = null;//help GC size--; return; }else{ current = current.next; } } if(current.next == null){ System.out.println("该链表中没有这个节点"); } } // 删除指定下标的节点 public void removeIndex(int index){ if(index < 0 || index > size){ System.out.println("请输入正确的下标"); return; } if (head.next == null){ System.out.println("当前链表为空"); return; } int startIndex = 1; Node current = head; while(current.next != null && startIndex < index){ current = current.next; startIndex++; } // 删除节点 Node removeNode = current.next; current.next = removeNode.next; removeNode = null;//help GC size--; } // 遍历链表 public void printNode() { if (head.next == null) { System.out.println("链表为空!"); return; } Node current = head.next; while (current.next != null) { System.out.print(current.data + "--->"); current = current.next; } System.out.println(current.data); System.out.println("链表长度为:"+size); } public static void main(String[] args) { // 创建一个链表对象,创建时候会初始化头结点 LinkedOp link = new LinkedOp(); // 先打印链表 link.printNode(); // 头插法插入1、2 link.insertFromHead(1); link.insertFromHead(2); // 尾插法插入3、4 link.insertFromLast(3); link.insertFromLast(4); // 打印链表 link.printNode(); //System.out.println(link.getSize()); // 删除节点为4的 link.remove(4); link.printNode(); // 删除第二个节点,节点从1开始 link.removeIndex(2); link.printNode(); } }
阅读更多
相关文章推荐
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- java实现创建链表以及插入节点,查找结点,删除节点等操作
- Java实现单链表的插入、删除、计算链表的长度和输出链表
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 数据结构Java实现 --单向链表的插入和删除
- C语言实现单链表节点的删除(带头结点)
- 链表的初始化、遍历、删除、插入、判断是否为空、求长度 C语言实现
- 链表:删除链表中重复的结点(java实现)
- 剑指offer--面试题13:在O(1)时间删除链表结点--Java实现
- 数据结构 P38 算法实现 在带头结点的单链表的第i个元素插入元素e
- 数据结构与算法分析笔记与总结(java实现)--链表9:有序环形链表中插入新结点问题
- C语言实现单链表节点的删除(带头结点)
- (2)单链表的操作 ① 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数
- 链表的创建,插入,删除,查询,合并,清空,销毁(带头结点)
- 用JAVA语言,编写一个链表类(双向链表),实现插入,删除,查找操作
- 计算带头结点单链表的长度 计算单链表的长度,实现单链表的打印
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- 每天一个小程序(2)——带头结点的链表的创建以及插入和删除
- 指定长度的单链表,删除和插入第i个结点
- 数据结构与算法分析笔记与总结(java实现)--链表8:对排序链表删除重复结点问题