数据结构与算法(四)链表
来源:互联网 发布:西安软件培训班 编辑:程序博客网 时间:2024/05/22 16:38
链表
类比为一列火车,每个车厢为一个结点,结点包括两个功能,1. 存储结点数据;2. 存储下一个结点的引用 。
链结点:
/* * 链结点,相当于是车厢 */public class Node {//数据域:保存结点里的数据public long data;//结点域==指针域:保存下一个结点的引用public Node next;public Node(long value){this.data=value;}/* * 显示方法 */public void display(){System.out.print(data+" ");}}
链表:
/* * 链表,相当于火车 */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;}}
public class TestLinkList {public static void main(String[] args) {// TODO Auto-generated method stubLinkList linkList=new LinkList();linkList.insertFirst(34);linkList.insertFirst(23);linkList.insertFirst(12);linkList.insertFirst(0);linkList.insertFirst(-1);//linkList.display();//linkList.deleteFirst();////System.out.println();//linkList.display();////Node node=linkList.find(23);//node.display();Node node1=linkList.delete(0);node1.display();System.out.println();linkList.display();}}
0 0
- 数据结构与算法(四)链表
- 数据结构与算法四
- 学习JavaScript数据结构与算法(四)——链表
- 【数据结构与算法】四 链表 反转
- Java数据结构与算法分析《四》链表
- 数据结构与算法之四(链表)
- 算法与数据结构笔记四
- 面试(四) 数据结构与算法
- 数据结构与算法之四 搜索算法
- 数据结构与算法-基础数据结构(链表)
- 数据结构与算法之链表(四) 约瑟夫环
- 数据结构与算法javascript描述(四) 冒泡排序
- 数据结构与算法Javascript描述(四)双向链表
- 数据结构与算法学习(四)队列结构Queue
- 别样数据结构与算法学习(四)树
- 数据结构与算法JavaScript (四) :串(BF)
- <四>java数据结构与算法 插入排序
- 四、数据结构与算法--其他排序
- JDBC的PreparedStatement用法
- vue.js 过渡效果之css过渡
- springboot--支付宝条码支付的实现
- AngularJS 1.x入门·简介
- 特征描述子(feature descriptor) —— HOG(方向梯度直方图)
- 数据结构与算法(四)链表
- 欢迎使用CSDN-markdown编辑器
- 在https中引用http的api资源中涉及的一些问题
- Java的Socket编程
- 计算机硬件接口
- java执行shell命令中有空格的处理方法
- Linux下C语言多线程(第二篇)线程互斥pthread_mutex_t()
- 快速判断一个数是否是2的n次方,若是,求n
- Android 使用内容解析者往短信数据库里插入一条数据