数据结构4:链表专题
来源:互联网 发布:能够翻译文档软件 编辑:程序博客网 时间:2024/06/07 03:26
链表实现类
package ch04;/* * 链表,相当于火车 */public class LinkList { //头结点 private Node first; public LinkList() { first = null; } /** * 插入一个结点,在头结点后进行插入 */ public void insertFirst(long value) { Node node = new Node(value); node.next = first; first = node; } /** * 删除一个结点,在头结点后进行删除 */ public Node deleteFirst() { Node tmp = first; first = tmp.next; return tmp; } /** * 显示方法 */ public void display() { Node current = first; while(current != null) { current.display(); current = current.next; } System.out.println(); } /** * 查找方法 */ public Node find(long value) { Node current = first; while(current.data != value) { if(current.next == null) { return null; } current = current.next; } return current; } /** * 删除方法,根据数据域来进行删除 */ public Node delete(long value) { Node current = first; Node previous = first; while(current.data != value) { if(current.next == null) { return null; } previous = current; current = current.next; } if(current == first) { first = first.next; } else { previous.next = current.next; } return current; }}
定义节点类
package ch04;/* * 链结点,相当于是车厢 */public class Node { //数据域 public long data; //指针域 public Node next; public Node(long value) { this.data = value; } /** * 显示方法 */ public void display() { System.out.print(data + " "); }}
链表测试类
package ch04;public class TestLinkList { public static void main(String[] args) { LinkList linkList = new LinkList(); linkList.insertFirst(34); linkList.insertFirst(23); linkList.insertFirst(12); linkList.insertFirst(0); linkList.insertFirst(-1);// linkList.display();// // linkList.deleteFirst();// linkList.display();// // Node node = linkList.find(23);// node.display(); Node node1 = linkList.delete(0); node1.display(); System.out.println(); linkList.display(); }}
阅读全文
1 0
- 数据结构4:链表专题
- 【数据结构】LintCode链表专题总结
- 暑假训练4-数据结构专题
- 面试题目 链表专题 - 数据结构与算法
- 数据结构专题
- 数据结构专题
- 数据结构专题
- 数据结构专题
- 数据结构专题
- 【数据结构专题】线性表之顺序表
- 数据结构专题——线性表
- 【数据结构专题】线性表之单链表
- 数据结构与算法专题之线性表——链表(二)双向链表
- 数据结构与算法专题之线性表——链表(三)循环链表
- 数据结构和算法设计专题之---判断两个链表是否相交并找出交点
- java数据结构链表,堆栈,队列,串相关专题分析与扯谈-概述
- java数据结构链表,堆栈,队列相关专题分析与扯谈-链表
- java数据结构链表,堆栈,队列相关专题分析与扯谈-堆栈
- 遇见问题解决步骤
- bzoj 1001
- 安装TensorFlow-gpu
- Eclipse导入Maven项目详解(新手初学)
- C语言的C89标准与C99标准
- 数据结构4:链表专题
- 如何提取图片的所有像素的颜色RGBa值?
- cocos2d-x-3.2 打包Apk
- 差分放大电路四种接法的性能比较
- 解决MySQL不允许(其它IP)从远程访问的方法
- java基础之java内存模型
- gdb使用
- 一遍记住Java常用的八种排序算法与代码实现
- 爬虫自带抓取当日A股所有股票代码,名称,最新价,涨幅,涨价,成交量,成交额,今开盘,昨开盘,最低价,最高价