<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. Visual Studio 2013启用AnkSVN

    1. 官网下载AnkSVN  https://ankhsvn.open.collab.net/ 2. 安装 3. 启用 When you enable AnkhSVN as a VS.NET sour ...

  2. CEPH浅析”系列之三——CEPH的设计思想

    Ceph针对的目标应用场景 理解Ceph的设计思想,首先还是要了解Sage设计Ceph时所针对的目标应用场景,换言之,"做这东西的目的是啥?" 事实上,Ceph最初针对的目标应用场 ...

  3. HTML5基础知识(2)--标题标签的使用

    1.HTML文档中包含各种级别的标题,各种级别的标题由<h1>到<h6>元素来定义,<h1>至<h6>标题标记中的字母h是英文headline的简称.其 ...

  4. android 之fragment创建

    1.使用xml标签 1.1定义两个重要属性  <fragment         android:id="@+id/fregment_top"        android: ...

  5. zabbix自动发现功能实现批量web url监控

    需求: 现在有大量url需要监控,形式如http://www.baidu.com ,要求url状态不为200即报警. 需求详细分析: 大量的url,且url经常变化,现在监控用的是zabbix,如果手 ...

  6. 通过自定义Attribute及泛型extension封装数据验证过程

    需求来源: 在日常工作中,业务流程往往就是大量持续的数据流转.加工.展现过程,在这个过程中,不可避免的就是数据验证的工作.数据验证工作是个很枯燥的重复劳动,没有什么技术含量,需要的就是对业务流程的准确 ...

  7. linux下mysql基本命令

    1, 创建mysqld数据库的管理用户:             要把root用户设置为管理员,我们应该运行下面的命令:    # mysqladmin -u root password 一般情况下, ...

  8. selenium 3 对我们的影响

    The major change in Selenium 3.0 is we're removing the original Selenium Core implementation and rep ...

  9. 利用Quartz2D推图的另一个方法 (使用CGMutalePathRef进行分层次)

    可以利用 CGMutablePathRef 创建每个不同图形,然后再一起添加到CGContext中 - (void)drawRect:(CGRect)rect { CGContextRef ctx = ...

  10. CODE[VS] 1346 HelloWorld编译器

    1346 HelloWorld编译器  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 黄金 Gold     题目描述 Description 请编程判断一个负责打印HelloWo ...