Java数据结构——链表-单链表
<1>链表

<2>引用和基本类型

<3>单链表

//=================================================
// File Name : LinkList_demo
//------------------------------------------------------------------------------
// Author : Common //类名:Link
//属性:
//方法:
class Link{ //链节点类
public int iData;
public double dData;
public Link next; //链表中下一个节点的引用 public Link(int iData, double dData) {
super();
this.iData = iData;
this.dData = dData;
} public void displayLink(){ //显示当前节点的值
System.out.println("iData="+iData+","+"dData"+dData);
} } //类名:LinkList
//属性:
//方法:
class LinkList{
private Link first; //只需要第一个节点,从第一个节点出发即可定位所有节点 public LinkList() { //构造函数
this.first = null;
} public boolean isEmpty(){
return (first == null);
} public void insertFirst(int id,double dd){ //插入元素是从链表的头开始插入
Link newLink = new Link(id,dd);
newLink.next = first;
first = newLink;
} public Link deleteFirst(){
Link temp = first; //暂存first
first = first.next; //把next设为first
return temp; //返回原来的first
} public void displayList(){
System.out.println("List(first-->last):");
Link current = first; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.next;
}
}
} //主类
//Function : LinkList_demo
public class LinkList_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
LinkList theList = new LinkList();
theList.insertFirst(11, 11.1);
theList.insertFirst(22, 22.2);
theList.insertFirst(33, 33.3);
theList.insertFirst(44, 44.4);
theList.insertFirst(55, 55.5);
theList.displayList();
while(!theList.isEmpty()){
Link aLink = theList.deleteFirst();
System.out.print("delete:");
aLink.displayLink();
}
theList.displayList();
} }
//=================================================
// File Name : LinkList_demo
//------------------------------------------------------------------------------
// Author : Common //类名:Link
//属性:
//方法:
class Link{ //链节点类
public int iData;
public double dData;
public Link next; //链表中下一个节点的引用 public Link(int iData, double dData) {
super();
this.iData = iData;
this.dData = dData;
} public void displayLink(){ //显示当前节点的值
System.out.println("iData="+iData+","+"dData"+dData);
} } //类名:LinkList
//属性:
//方法:
class LinkList{
private Link first; //只需要第一个节点,从第一个节点出发即可定位所有节点 public LinkList() { //构造函数
this.first = null;
} public boolean isEmpty(){
return (first == null);
} public void insertFirst(int id,double dd){ //插入元素是从链表的头开始插入
Link newLink = new Link(id,dd);
newLink.next = first;
first = newLink;
} public Link deleteFirst(){
Link temp = first; //暂存first
first = first.next; //把next设为first
return temp; //返回原来的first
} public void displayList(){
System.out.println("List(first-->last):");
Link current = first; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.next;
}
} public Link find(int key){ //查找指定的关键字
Link current = first;
while(current.iData != key){
if(current.next == null)
return null;
else
current = current.next;
}
return current;
} public Link delete(int key){ //如果current的值匹配,则删除
Link current = first;
Link previous = first;
//没有匹配到值
while(current.iData != key){
if(current.next == null)
return null;
else{ //pre和cur向后移动
previous = current;
current = current.next;
}
}
//匹配到值
if(current == first) //只有一个first,并匹配,则把first设成first.next
first = first.next;
else //current的值匹配,则删除,并把cur的next赋给pre的next
previous.next = current.next;
return current;
}
} //主类
//Function : LinkList_demo
public class LinkList_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
LinkList theList = new LinkList();
theList.insertFirst(11, 11.1);
theList.insertFirst(22, 22.2);
theList.insertFirst(33, 33.3);
theList.insertFirst(44, 44.4);
theList.insertFirst(55, 55.5);
theList.displayList(); Link f = theList.find(22);
if(f != null){
System.out.print("找到:");
f.displayLink();
}
else
System.out.print("没有找到"); Link d = theList.delete(32);
if(d != null){
System.out.print("删除:");
d.displayLink();
}
else
System.out.print("没有找到匹配的删除"); } }
Java数据结构——链表-单链表的更多相关文章
- Java数据结构之单链表
这篇文章主要讲解了通过java实现单链表的操作,一般我们开始学习链表的时候,都是使用C语言,C语言中我们可以通过结构体来定义节点,但是在Java中,我们没有结构体,我们使用的是通过类来定义我们所需要的 ...
- Java数据结构-03单链表(二)
在之前我们封装了一些操作在接口类中,并在抽象类实现了相同的方法.下面我们开始写代码: 无头结点单链表:(注意下面的AbstractList是之前抽取的类,不是java.util包下的类) public ...
- 图解Java数据结构之单链表
本篇文章介绍数据结构中的单链表. 链表(Linked List)介绍 链表可分为三类: 单链表 双向链表 循环列表 下面具体分析三个链表的应用. 单链表 链表是有序的列表,它在内存中存储方式如下: 虽 ...
- Java数据结构-02单链表(一)
一.链式存储: ①简述:线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素,这组存储单元可以是连续的,也可以是不连续的.存储单元由两部分组成,数据源和指针,数据源放数据,指针指向下个 ...
- 数据结构之单链表的实现-java
一.单链表基本概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元(一般是非连续存储单元)存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素data + 指针next ...
- Python数据结构之单链表
Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...
- javascript数据结构之单链表
下面是用javascript实现的单链表,但是在输出的时候insert方法中存在问题,chrome的console报错说不能读取空的属性,调试了很久都没有通过,先在这里存着,以后再来修改一下. //数 ...
- 数据结构(一) 单链表的实现-JAVA
数据结构还是很重要的,就算不是那种很牛逼的,但起码得知道基础的东西,这一系列就算是复习一下以前学过的数据结构和填补自己在这一块的知识的空缺.加油.珍惜校园中自由学习的时光.按照链表.栈.队列.排序.数 ...
- 数据结构(2):单链表学习使用java实现
单链表是单向链表,它指向一个位置: 单链表常用使用场景:根据序号排序,然后存储起来. 代码Demo: package com.Exercise.DataStructure_Algorithm.Sing ...
- 数据结构之单链表(基于Java实现)
链表:在计算机中用一组任意的存储单元存储线性表的数据元素称为链式存储结构,这组存储结构可以是连续的,也可以是不连续的,因此在存储数据元素时可以动态分配内存. 注:在java中没有指针的概念,可以理解为 ...
随机推荐
- Day One(Beta)
站立式会议 站立式会议内容总结 331 今天:自己摸索了BaseAdapter删除元素的三种方式更新效率逐步上升 明天:学习webkit的webview的使用 442 今天:书籍评价界面,计划删除功能 ...
- Android怎么使用字体图标 自定义FontTextView字体图标控件-- 使用方法
首先我想说明一下字体图标的好处,最大的好处就是自适应了,而且是使用TextView 不用去切图,是矢量图 灵活调用 第一步我要说明一下一般字体图标的来源,我这里使用的是 --阿里巴巴矢量图标库 -网 ...
- iOS开发中的错误整理,百思项目'我的'模块,tableFooterViewHeight的问题.提醒自己对KVO和Block的运用欠缺
一.错误分析:由于tableFooterView中的数据是通过请求服务器后得到的,tableFooterViewHeight也是根据请求过来的数据经过布局子控件而计算出来的.(注意:计算高度是在子线程 ...
- 【BZOJ 1096】【ZJOI 2007】仓库建设 DP+斜率优化
后缀自动机看不懂啊QAQ 放弃了还是看点更有用的东西吧,比如斜率优化DP 先水一道 #include<cstdio> #include<cstring> #include< ...
- Spring 在web 容器中的启动过程
1.对于一个web 应用,其部署在web 容器中,web 容器提供其一个全局的上下文环境,这个上下文就是 ServletContext ,其后面的spring IoC 容器提供宿主环境 2.在web. ...
- Js 校验时间、比较时间 和转换时间格式
function checkDate(obj){ var strDate = obj.value; var nowDate = new Date(); var a=/^(\d{1,4})(-|\/)( ...
- 用 phylomatic 软件生成的进化树
用 phylomatic 软件生成的进化树 Phylomatic是在线软件,可以利用植物名录,按照APGIII的被子植物科的拓扑结构,生成进化树. 参考 张金龙博士 工作目录 setwd(" ...
- xml序列化方式
public static class MySerializeXmlHelper { static MySerializeXmlHelper() { } private static object _ ...
- Leetcode Perfect Square
这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...
- 【bzoj2242】 SDOI2011—计算器
http://www.lydsy.com/JudgeOnline/problem.php?id=2242 (题目链接) 题意 给出y,z,p.求:1.yz mod p:2.xy=z(mod p):3. ...