数据结构之LinkList
1、结构:
2、Link代码:
public class Link {
public int iData;
public double dData;
public Link next;
public Link(int id,double dd){
iData = id;
dData = dd;
}
public void displayLink(){
System.out.println("{" + iData + " , " + dData + " } ");
}
}
3、LinkList代码:
public class LinkList {
private Link first;
public LinkList(){
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.next;
return temp;
}
public void displayList(){
System.out.println("List (first -- > last);");
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println(" ");
}
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;
}
}
4、运行代码:
public class LinkList2App {
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();
Link f = theList.find(44);
if(f != null){
System.out.println("Found link with key " + f.iData);
}else{
System.out.println("Can't find link");
}
Link d = theList.delete(66);
if( d != null){
System.out.println("Deleted link with key " + d.iData);
}else{
System.out.println("Can't delete link");
}
theList.displayList();
}
}
5、结果:
List (first -- > last);
{88 , 8.99 }
{66 , 6.99 }
{44 , 4.99 }
{22 , 2.99 }
Found link with key 44
Deleted link with key 66
List (first -- > last);
{88 , 8.99 }
{44 , 4.99 }
{22 , 2.99 }
Reference:
[1] Robert Lalore(著) 计晓云,赵研,曾希,狄小菡(译), Java数据结构和算法(第二版),中国电力出版社,2004 :131-150
数据结构之LinkList的更多相关文章
- [数据结构]链表LinkList
目录 1.3 链表 1.3.1 头插法建立单链表 1.3.2 限制链表长度建立单链表 1.3.3 尾插法建立单链表 1.3.4 按序号查找单链表 1.3.5 按值查找单链表 1.3.6 链表的插入 1 ...
- java容器的数据结构-ArrayList,LinkList,HashMap
ArrayList: 初始容量为10,底层实现是一个数组,Object[] elementData 自动扩容机制,当添加一个元素时,数组长度超过了elementData.leng,则会按照1.5倍进行 ...
- Java:List,ArrayList和LinkList的区别
1.大学数据结构中ArrayList是实现了基于动态数组的数据结构,LinkList基于链表的数据结构 2.对于随机访问get和set,ArrayList优于LinkList,因为LinkedList ...
- Android——ArrayList 、LinkList、List 区别 & 迭代器iterator的使用 & HashMap、Hashtable、LinkedHashMap、TreeMap
ArrayList .LinkList.List 区别 & 迭代器iterator的使用 & HashMap.Hashtable.LinkedHashMap.TreeMap 一.几个 ...
- JAVA框架面试题
至少写出3种ssh框架中常用的注解 @RequestMapping springMvc中访问地址映射 @ResponseBody springMvc中返回视图 @Table hibernate中实体类 ...
- Junit 注解 类加载器 .动态代理 jdbc 连接池 DButils 事务 Arraylist Linklist hashset 异常 哈希表的数据结构,存储过程 Map Object String Stringbufere File类 文件过滤器_原理分析 flush方法和close方法 序列号冲突问题
Junit 注解 3).其它注意事项: 1).@Test运行的方法,不能有形参: 2).@Test运行的方法,不能有返回值: 3).@Test运行的方法,不能是静态方法: 4).在一个类中,可以同时定 ...
- [数据结构]链表相关的实现LinkList.cpp
目录 LinkList.cpp //链表相关操作的实现 LinkList.h LinkListManager.cpp //链表相关实现函数的调用 LinkListManager.h LinkList. ...
- 数据结构自己实现——Linklist
//单???链???表??? #include <iostream> using namespace std; typedef char datatype; typedef struct ...
- Java数据结构与算法(5) - ch05链表(LinkList)
双端链表与传统链表非常相似,但是它有一个新增的特性:即对最后一个链节点的引用,就像对第一个连接点的引用一样.注意与双向链表进行区别.
随机推荐
- 连接mysql报"ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server"
最近在服务器上部署好的应用突然间连接不上mysql数据库,报错“ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this M ...
- Win10 Microsoft Store 微软商店 Error 0x00000193 解决方法
0x00 前言 最近使用 CFW 过程中使用 Fiddle Web Debug 设置 Store 的回环代理的过程中发现无论是否使用代理,Store 都无法访问网络的问题,在最下面的提示中出现了 0x ...
- 提高用git下载代码时的成功率
在用git clone下载一些比较大的仓库时,经常会遇到由于仓库体积过大,网络也不稳定,导致下了半截就中断了,可以参考如下的下载方法. 先用创建一个空目录,然后用git init初始化,然后用git ...
- k8s的pod或者ns资源一直terminating删除办法
假设你要删掉的ns资源,发现一直删不了处于terminating状态 1.首先试一下先把这个ns的所有pod都删掉kubectl delete pod --all -n <terminating ...
- vue.js生成横向拓扑图
1.前端代码 <link href="https://magicbox.bk.tencent.com/static_api/v3/assets/bootstrap-3.3.4/css/ ...
- Unity 渲染教程(二):着色器基础
转载:https://www.jianshu.com/p/7db167704056 这是关于渲染基础的系列教程的第二部分.这个渲染基础的系列教程的第一部分是有关矩阵的内容.在这篇文章中我们将编写我们的 ...
- 使用adb/Linux获取网关ip
ip route list table
- 项目Beta冲刺(3/7)(追光的人)(2019.5.25)
所属课程 软件工程1916 作业要求 Beta冲刺博客汇总 团队名称 追光的人 作业目标 描述Beta冲刺每日的scrum和PM报告两部分 队员学号 队员博客 221600219 小墨 https:/ ...
- laravel5.1框架基础之Blade模板继承简单使用方法分析
本文实例讲述了laravel5.1框架基础之Blade模板继承简单使用方法.分享给大家供大家参考,具体如下: 模板继承什么用? 自然是增强基础页面的复用,有利于页面文档的条理,也便于更改多处使用的内容 ...
- python应用-一组数的最大值,最小值,平均数
def foo(n): c=[] for _ in range (n): var=randint(60,100) c.append(var) print(c) total=0 max = c[0] m ...