<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数据结构——链表-单链表的更多相关文章

  1. Java数据结构之单链表

    这篇文章主要讲解了通过java实现单链表的操作,一般我们开始学习链表的时候,都是使用C语言,C语言中我们可以通过结构体来定义节点,但是在Java中,我们没有结构体,我们使用的是通过类来定义我们所需要的 ...

  2. Java数据结构-03单链表(二)

    在之前我们封装了一些操作在接口类中,并在抽象类实现了相同的方法.下面我们开始写代码: 无头结点单链表:(注意下面的AbstractList是之前抽取的类,不是java.util包下的类) public ...

  3. 图解Java数据结构之单链表

    本篇文章介绍数据结构中的单链表. 链表(Linked List)介绍 链表可分为三类: 单链表 双向链表 循环列表 下面具体分析三个链表的应用. 单链表 链表是有序的列表,它在内存中存储方式如下: 虽 ...

  4. Java数据结构-02单链表(一)

    一.链式存储: ①简述:线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素,这组存储单元可以是连续的,也可以是不连续的.存储单元由两部分组成,数据源和指针,数据源放数据,指针指向下个 ...

  5. 数据结构之单链表的实现-java

    一.单链表基本概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元(一般是非连续存储单元)存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素data + 指针next ...

  6. Python数据结构之单链表

    Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...

  7. javascript数据结构之单链表

    下面是用javascript实现的单链表,但是在输出的时候insert方法中存在问题,chrome的console报错说不能读取空的属性,调试了很久都没有通过,先在这里存着,以后再来修改一下. //数 ...

  8. 数据结构(一) 单链表的实现-JAVA

    数据结构还是很重要的,就算不是那种很牛逼的,但起码得知道基础的东西,这一系列就算是复习一下以前学过的数据结构和填补自己在这一块的知识的空缺.加油.珍惜校园中自由学习的时光.按照链表.栈.队列.排序.数 ...

  9. 数据结构(2):单链表学习使用java实现

    单链表是单向链表,它指向一个位置: 单链表常用使用场景:根据序号排序,然后存储起来. 代码Demo: package com.Exercise.DataStructure_Algorithm.Sing ...

  10. 数据结构之单链表(基于Java实现)

    链表:在计算机中用一组任意的存储单元存储线性表的数据元素称为链式存储结构,这组存储结构可以是连续的,也可以是不连续的,因此在存储数据元素时可以动态分配内存. 注:在java中没有指针的概念,可以理解为 ...

随机推荐

  1. 作业成绩 final 20161124-1201 09:00

    final阶段,20161124-1201 09:00. 申诉截止时间 20161206 12:00,微信联系杨贵福. 凡描述需求或BUG时,应给出以下4项: 你期待看到的现象如何 你实际看到的现象 ...

  2. CAP理论

    自打引入CAP理论的十几年里,设计师和研究者已经以它为理论基础探索了各式各样新颖的分布式系统,甚至到了滥用的程度.NoSQL运动也将CAP理论当作对抗传统关系型数据库的依据. CAP理论主张任何基于网 ...

  3. java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 错误

    你的ArrayList 是一个没有值的对象(不是null),也就是里面什么对象也没有存(即:arrayList.size()==0).但是,你有取它下标为0值的操作.所以,数组越界了!!比如array ...

  4. ArcGIS 帮助(10.2、10.2.1 和 10.2.2)收集

    帮助首页 [Oracle基础] 快速浏览:Oracle 地理数据库 什么是 Oracle Spatial? 设置到 Oracle 的连接 存储在 Oracle 地理数据库中的系统表 结合企业级地理数据 ...

  5. 【转】Timer还是Handler

    在我们Android开发过程中,经常需要执行一些短周期的定时任务,这时候有两个选择Timer或者Handler.然而个人认为:Handler在多个方面比Timer更为优秀,更推荐使用. 一.易用性 1 ...

  6. BZOJ 1110: [POI2007]砝码Odw

    1110: [POI2007]砝码Odw Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 547  Solved: 296[Submit][Status ...

  7. 【BZOJ-1717】Milk Patterns产奶的模式 后缀数组

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 881  Solved:  ...

  8. Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function

    目录 . 引言 . C/C++运行库 . 静态Glibc && 可执行文件 入口/终止函数 . 动态Glibc && 可执行文件 入口/终止函数 . 静态Glibc & ...

  9. C# ref的应用

    之前一直只是知道有这么个参数修饰符,也知道用来干嘛的,但是基本就没用上.这几天好好整理了一下,发现ref的修饰符可以帮助简化很多代码.让我更深入的了解到面向对象的深沉含义. 自定义一个类中,类中的方法 ...

  10. [JAVA 多种方式读取文件]

    package com.file; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream ...