数据结构之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)
双端链表与传统链表非常相似,但是它有一个新增的特性:即对最后一个链节点的引用,就像对第一个连接点的引用一样.注意与双向链表进行区别.
随机推荐
- Qt Graphics-View的打印功能实现
本文来研究一下Qt Graphics-View的打印功能实现. 在Qt的官方文档中介绍了Graphics-View的打印相关内容. Qt中对打印的支持是有一个独立的printsupport模块来完成的 ...
- Android studio module生成jar包,module中引用的第三方库没有被引用,导致java.lang.NoClassDefFoundError错误。
android studio 创建了一个Module生成jar包,这个module中有引用一些第三方的类库,比如 gson,volley等. 但是生成的jar包里,并没有将gson,volley等第三 ...
- git的基本使用和多人协作合并管理
1.代码版本控制工具 git 分布式 svn 集中式 2.配置git 配置用户名以及邮箱账号,用于记录用户信息 git config --global user.name 'wudaxun' git ...
- mac下比较好用的svn软件,SVN客户端CornerStone 2.7.10 破解版
一.已经破解,可以直接使用. dmg文件无密码,也不需要注册机.直接使用即可 二. 界面... 三.添加repository 点击左侧栏中REPOSITORY那一栏的+选择添加reposito ...
- Redis支持的数据类型
String字符串: 格式: set key value string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 . string类型是Redi ...
- Copy Files from Windows 10 to wsl
Method 1 reboot( close wsl window and reopen ) Method 2 – Windows System Drive as a Mount point Wind ...
- Java基础--static关键字
不管是平时阅读源代码,还是笔试.面试中,static关键字还是经常被问道,这篇文章主要来重新复习一下该关键字. 一.static用途 static方便在没有创建对象的时候调用方法或者变量. stati ...
- Kotlin匿名函数与闭包详解
Lambda表达式实例演练: 继续先来编写一些Lambda表达式相关的代码: 接下来想从上面的字符串数组中找到带有"h"的字符串并打印出来: 如果学习了Java8的Lambda表达 ...
- 配置VScode c语言环境
vscode 提示 <sys/socket.h>找不到, 原来是不同平台上,头文件不一样. 参考:https://blog.csdn.net/qq_28581077/article/det ...
- Redis的入门
什么是NOSQL? NOSQL(Not Only SQL)不仅仅是数据库,是一种全新的理念,泛指非关系型的数据库. 为什么需要NOSQL? 随着互联网的高速崛起,网站的用户群的增加,访问量的上升,传统 ...