JAVA数据结构:链表
来源:互联网 发布:泰安软件开发公司 编辑:程序博客网 时间:2024/05/22 18:55
一个简单的单链表,它支持:
1.在链表头插入一个数据项
2.在链表头删除一个数据项
3.遍历链表显示它的内容
public class Link{ public int iData; public double dData; public Link next; public Link(int id,double dd){ iData = id; dData = dd; } public void display(){ System.out.print("{"+iData+","+dData+"}"); }}
public class LinkList{ private Link first; public void LinkList(){ first = null; } public boolean isEmpty(){ return (first == null); }}
LinkList的构造函数把first赋值成null,这不是必须的,引用类型在创建之初会自动赋值成null。first =null代表链表没有数据项。
insertFirst() ,在链表的表头插入一个新的节点
public void insertFirst(int id,double dd){ Link newLink = new Link(id,dd); newLink.next = first; first = newLink;}
deleteFirst(),删除表头节点
public Link deleteFirst(){ List temp = first; first = first.next; return temp;}
displayList()显示链表的所有内容
public void displayList(){ System.out.println("List (first-->last): "); List current = first; while(current != null){ current.dispalyLink(); 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; }}
在指定的链结点之后插入新节点
public void insertAfter(int key,int id,double dd ){ Link newLink = new Link(id,dd); Link findcurrent = find(key); if(findcurrent!=null){ newLink.next = findcurrent.next; findcurrent.next = newLink; } }
阅读全文
0 0
- Java数据结构(一) ---链表
- java数据结构 链表
- 数据结构JAVA--链表
- 数据结构--链表(java)
- 【Java数据结构】链表
- Java数据结构----链表
- java--数据结构--链表
- Java 数据结构(链表)
- Java数据结构-链表
- 《java数据结构》--链表
- JAVA数据结构:链表
- java数据结构--链表
- java 数据结构-链表
- Java数据结构和算法--链表
- Java数据结构和算法--链表
- Java数据结构和算法--链表
- Java中的数据结构(2)----链表
- Java 数据结构和算法 链表
- Fractal Picture With Newton-Raphson
- 使用数组实现堆排序
- 阿里云服务器带宽跑满怎么办,出网一直很高!
- ACM Amman Collegiate Programming Contest C. Street Lamps
- 浅析TCP之头部可选项
- JAVA数据结构:链表
- node,express,koa,koa2跨域问题
- VMware虚拟机安装VMware Tools
- 使用批处理批量复制文件并重命名
- Android根据当前时间获取前面的时间日期,或者之后的时间日期
- java关键字
- 使用Callable接口创建线程(Java5新增)
- liunx 系统(centos)mysql安装
- java--验证码实现