<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 ...
随机推荐
-
LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
-
.net 插件开发
http://blog.csdn.net/mailtogst/article/details/2073696
-
NoSQL数据库的分布式模型
NoSQL数据库的分布式模型 单一服务器 在一个服务器完全能够胜任工作时就没必要考虑分布式,这样部署简单,维护也方便很多: 分片 特点 数据的各个部分存放在集群的不同服务器中: 比如按字母来划分:以a ...
-
用shell求两个文件的差集
假设有两个文件a.file和b.file,分别代表集合A和集合B. a.file的内容如下: abcde b.file的内容如下: cdefg 可以用grep命令 grep命令是常用来搜索文本内容的, ...
-
java如何调用webservice接口
java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用.理论上 ...
-
设计模式的征途—8.桥接(Bridge)模式
在现实生活中,我们常常会用到两种或多种类型的笔,比如毛笔和蜡笔.假设我们需要大.中.小三种类型的画笔来绘制12中不同的颜色,如果我们使用蜡笔,需要准备3*12=36支.但如果使用毛笔的话,只需要提供3 ...
-
vue 2 仿IOS 滚轮选择器 从入门到精通 (一)
大家好,由于最近从事的是微信公众号和APP内嵌 H5开发,避免不了开发一些和native相同的操作功能,就如接下来说的 仿IOS滚轮选择器. 先来个截图: 接下来具体介绍如何实现的.能力有限避免不了错 ...
-
JUC——TimeUnit工具类(二)
TimeUnit工具类 在java.util.concurrent开发包里面提供有一个TimeUnit类,这个类单独看它的描述是一个时间单元类.该类是一个枚举类,这也是juc开包里面唯一的一个枚举类. ...
-
C# 使用int.TryParse,Convert.ToInt32,(int)将浮点类型转换整数时的区别
int.TryParse,Convert.ToInt32,(int) 这几种类型在将浮点类型转换整数时是有差别 Convert.ToInt32则会进行四舍五入 int.TryParse只能转换整数,即 ...
-
Ubuntu 14.04 配置 Java SE jdk-7u55 (转载)
转自:http://blog.csdn.net/tecn14/article/details/24797545 JDK 目前最新版为jdk-8u5,这次没有选择安装最新的jdk8,而是要安装jdk7 ...