/**
* 单链表操作
* Create by Administrator
* 2018/6/14 0014
* 下午 2:05
**/
public class Link { public int iData;
public double dData;
public Link next; public Link(int id, double dd) {
this.iData = id;
this.dData = dd;
} public void displayLink() {
System.out.print("{" + iData + " , " + dData + "} ");
}
} class LinkList { private Link first; public LinkList() {
this.first = null;
} public boolean isEmpty(){ //判断链表是否为空
return (first == null);
} 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){ //删除指定对象
Link current = first;
Link previous = first;
while (current.iData != key){
if(current.next == null){
return null;
}else {
previous = current;
current = current.next;
}
}
if(current == first){
first = first.next;
}else {
previous.next = current.next;
}
return current;
} public void insertFirst(int id,double dd){
Link link = new Link(id, dd);
link.next = first; //储存对象的引用
first = link; //把当前对象赋值给first
} public Link deleteFrst() { //删除首链表,正好和添加相反
if(!isEmpty()){
Link temp = first;
first = first.next;
return temp;
}
return null;
} public void displayList(){
System.out.print("List (first-->last): ");
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println("");
} public static void main(String[] args) {
LinkList theList = new LinkList(); theList.insertFirst(22,2.99);
theList.insertFirst(44,4.99);
theList.insertFirst(66,6.99);
theList.insertFirst(88,8.99);

        theList.displayList();

//        while (!theList.isEmpty()){
// Link aLink = theList.deleteFrst();
// System.out.print("Deleted");
// aLink.displayLink();
// System.out.println("");
// } Link f = theList.find(44);
if(f != null){
System.out.print("find Link: ");
f.displayLink();
System.out.println("");
Link result = theList.delete(f.iData);
if(result != null){
System.out.print("delete success ");
result.displayLink();
}
}
System.out.println("");
theList.displayList();
}
}

  

java学习之—链表(1)的更多相关文章

  1. Java学习之链表

    数据结构学了,java实现下 package com.gh.Link; /** * 链表的实现 * @author ganhang * */ public class Links { public s ...

  2. Java学习笔记--链表

    心在山东身在吴,飘蓬江海漫嗟吁. 他时若遂凌云志, 敢笑黄巢不丈夫. --水浒传 先上源代码,LinkedList类: private static class Node<E> { E i ...

  3. java学习之—链表(4)

    /** * 使用链表实现队列 * Create by Administrator * 2018/6/19 0019 * 下午 4:37 **/ public class Link { public l ...

  4. java学习之—链表(3)

    /** * 使用链表实现队列 * Create by Administrator * 2018/6/19 0019 * 下午 4:37 **/ public class Link { public l ...

  5. java学习之—链表(2)

    /** * 双端链表操作 * Create by Administrator * 2018/6/14 0014 * 下午 2:05 **/ class Link1 { public long dDat ...

  6. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  7. 第八周java学习总结

    学号 20175206 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章:泛型与集合框架 主要内容 泛型 链表 堆栈 散列映射 树集 树映射 重点和难点 重点:泛型和集合的使 ...

  8. java学习(五)

    学号 20189214 <Java程序设计>第五周学习总结 教材学习内容总结 输入输出 文件系统可以包含3种类型的对象:文件.目录和符号链接. 一个文件或路径是一个java.io.File ...

  9. 20165236 第六周Java学习总结

    20165236 第六周Java学习总结 一. 第八章内容: 1.String 类: String对象.常量对象:字符串并置: 常用方法: length,equals,startsWith,compa ...

随机推荐

  1. UVA1620-Lazy Susan(思维+逆序对)

    Problem UVA1620-Lazy Susan Accept: 81  Submit: 375Time Limit: 3000 mSec Problem Description There ar ...

  2. CSAPP:第三章程序的机器级表示2

    CSAPP:程序的机器级表示2 关键点:算术.逻辑操作 算术逻辑操作1.加载有效地址2.一元二元操作3.移位操作 算术逻辑操作   如图列出了x86-64的一些整数和逻辑操作,大多数操作分成了指令类( ...

  3. nginx服务器下 PHP 出现 502 解决方案

    https://blog.csdn.net/qq_34625397/article/details/51744859 nginx出现502有很多原因,但大部分原因可以归结为资源数量不够用,也就是说后端 ...

  4. .NET中使用Redis 转发

    .NET中使用Redis   Redis是一个用的比较广泛的Key/Value的内存数据库,新浪微博.Github.StackOverflow 等大型应用中都用其作为缓存,Redis的官网为http: ...

  5. docker 6 docker运行的底层原理

    docker是一个client-server结构的系统,docker守护进程运行在主机上,然后通过socket连接从客户端访问,守护进程从客户端接收命令并管理运行在主机上的容器,是一个运行时的环境,就 ...

  6. Python IDLE 增加清屏功能

    (Python2,Python3 通用) 保存如下代码到 ClearWindow.py """ Clear Window Extension Version: 0.2 A ...

  7. 初学Python——RabbitMQ的安装

    记录踩坑之路,本篇文章主要摘抄自CSDN博客https://blog.csdn.net/weixin_39735923/article/details/79288578 Windows10环境下安装R ...

  8. 读写分离子系统 - C# SQL分发子系统 - Entity Framework支持

    A2D Framework增加了EF支持,加上原先支持ADO.NET: 支持EF方式 支持ADO.NET方式 这次来讲如何让Entity Framework变成nb的读写分离 1. 先设计EF模型, ...

  9. CentOS 7+nginx+PHP+php-fpm

    根据网上资料配置: location ~ \.php$ { #include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index in ...

  10. python-re模块-54

    import re # findall # search # match ret = re.findall('[a-z]+', 'eva egon yuan') # 返回所有满足匹配条件的结果,放在列 ...