Java单链表、双端链表、有序链表实现
单链表:
insertFirst:在表头插入一个新的链接点,时间复杂度为O(1)
deleteFirst:删除表头的链接点,时间复杂度为O(1)
有了这两个方法,就可以用单链表来实现一个栈了,见http://blog.csdn.net/a19881029/article/details/22579759
find:查找包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N)
remove:删除包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N)
- public class LinkedList {
- private class Data{
- private Object obj;
- private Data next = null;
- Data(Object obj){
- this.obj = obj;
- }
- }
- private Data first = null;
- public void insertFirst(Object obj){
- Data data = new Data(obj);
- data.next = first;
- first = data;
- }
- public Object deleteFirst() throws Exception{
- if(first == null)
- throw new Exception("empty!");
- Data temp = first;
- first = first.next;
- return temp.obj;
- }
- public Object find(Object obj) throws Exception{
- if(first == null)
- throw new Exception("LinkedList is empty!");
- Data cur = first;
- while(cur != null){
- if(cur.obj.equals(obj)){
- return cur.obj;
- }
- cur = cur.next;
- }
- return null;
- }
- public void remove(Object obj) throws Exception{
- if(first == null)
- throw new Exception("LinkedList is empty!");
- if(first.obj.equals(obj)){
- first = first.next;
- }else{
- Data pre = first;
- Data cur = first.next;
- while(cur != null){
- if(cur.obj.equals(obj)){
- pre.next = cur.next;
- }
- pre = cur;
- cur = cur.next;
- }
- }
- }
- public boolean isEmpty(){
- return (first == null);
- }
- public void display(){
- if(first == null)
- System.out.println("empty");
- Data cur = first;
- while(cur != null){
- System.out.print(cur.obj.toString() + " -> ");
- cur = cur.next;
- }
- System.out.print("\n");
- }
- public static void main(String[] args) throws Exception {
- LinkedList ll = new LinkedList();
- ll.insertFirst(4);
- ll.insertFirst(3);
- ll.insertFirst(2);
- ll.insertFirst(1);
- ll.display();
- ll.deleteFirst();
- ll.display();
- ll.remove(3);
- ll.display();
- System.out.println(ll.find(1));
- System.out.println(ll.find(4));
- }
- }
- 1 -> 2 -> 3 -> 4 ->
- 2 -> 3 -> 4 ->
- 2 -> 4 ->
- null
- 4
双端链表(不是双向链表):
与单向链表的不同之处在保存有对最后一个链接点的引用(last)
insertFirst:在表头插入一个新的链接点,时间复杂度O(1)
insertLast:在表尾插入一个新的链接点,时间复杂度O(1)
deleteFirst:删除表头的链接点,时间复杂度O(1)
deleteLast::删除表尾的链接点,由于只保存了表尾的链接点,而没有保存表尾的前一个链接点(这里就体现出双向链表的优势了),所以在删除表尾链接点时需要遍历以找到表尾链接点的前一个链接点,需查找N-1次,也就是O(N)
有了这几个方法就可以用双端链表来实现一个队列了,http://blog.csdn.net/a19881029/article/details/22654121
- public class FirstLastList {
- private class Data{
- private Object obj;
- private Data next = null;
- Data(Object obj){
- this.obj = obj;
- }
- }
- private Data first = null;
- private Data last = null;
- public void insertFirst(Object obj){
- Data data = new Data(obj);
- if(first == null)
- last = data;
- data.next = first;
- first = data;
- }
- public void insertLast(Object obj){
- Data data = new Data(obj);
- if(first == null){
- first = data;
- }else{
- last.next = data;
- }
- last = data;
- }
- public Object deleteFirst() throws Exception{
- if(first == null)
- throw new Exception("empty");
- Data temp = first;
- if(first.next == null)
- last = null;
- first = first.next;
- return temp.obj;
- }
- public void deleteLast() throws Exception{
- if(first == null)
- throw new Exception("empty");
- if(first.next == null){
- first = null;
- last = null;
- }else{
- Data temp = first;
- while(temp.next != null){
- if(temp.next == last){
- last = temp;
- last.next = null;
- break;
- }
- temp = temp.next;
- }
- }
- }
- public void display(){
- if(first == null)
- System.out.println("empty");
- Data cur = first;
- while(cur != null){
- System.out.print(cur.obj.toString() + " -> ");
- cur = cur.next;
- }
- System.out.print("\n");
- }
- public static void main(String[] args) throws Exception {
- FirstLastList fll = new FirstLastList();
- fll.insertFirst(2);
- fll.insertFirst(1);
- fll.display();
- fll.insertLast(3);
- fll.display();
- fll.deleteFirst();
- fll.display();
- fll.deleteLast();
- fll.display();
- }
- }
- 1 -> 2 ->
- 1 -> 2 -> 3 ->
- 2 -> 3 ->
- 2 ->
有序链表:链表中的数据按从小到大排列
- public class SortedList {
- private class Data{
- private Object obj;
- private Data next = null;
- Data(Object obj){
- this.obj = obj;
- }
- }
- private Data first = null;
- public void insert(Object obj){
- Data data = new Data(obj);
- Data pre = null;
- Data cur = first;
- while(cur != null && (Integer.valueOf(data.obj.toString())
- .intValue() > Integer.valueOf(cur.obj.toString())
- .intValue())){
- pre = cur;
- cur = cur.next;
- }
- if(pre == null)
- first = data;
- else
- pre.next = data;
- data.next = cur;
- }
- public Object deleteFirst() throws Exception{
- if(first == null)
- throw new Exception("empty!");
- Data temp = first;
- first = first.next;
- return temp.obj;
- }
- public void display(){
- if(first == null)
- System.out.println("empty");
- System.out.print("first -> last : ");
- Data cur = first;
- while(cur != null){
- System.out.print(cur.obj.toString() + " -> ");
- cur = cur.next;
- }
- System.out.print("\n");
- }
- public static void main(String[] args) throws Exception{
- SortedList sl = new SortedList();
- sl.insert(80);
- sl.insert(2);
- sl.insert(100);
- sl.display();
- System.out.println(sl.deleteFirst());
- sl.insert(33);
- sl.display();
- sl.insert(99);
- sl.display();
- }
- }
- first -> last : 2 -> 80 -> 100 ->
- 2
- first -> last : 33 -> 80 -> 100 ->
- first -> last : 33 -> 80 -> 99 -> 100 ->
表的插入和删除平均需要比较N/2次,即O(N),但是获取最小数据项只需O(1),因为其始终处于表头,对频繁操作最小数据项的应用,可以考虑使用有序链表实现,如:优先级队列
和数组相比,链表的优势在于长度不受限制,并且在进行插入和删除操作时,不需要移动数据项,故尽管某些操作的时间复杂度与数组想同,实际效率上还是比数组要高很多
劣势在于随机访问,无法像数组那样直接通过下标找到特定的数据项
Java单链表、双端链表、有序链表实现的更多相关文章
- Java数据结构——用双端链表实现队列
//================================================= // File Name : LinkQueue_demo //---------------- ...
- java数据结构-09双端队列
一.相关概念: (Deque)双端队列能够在队头.队尾进行添加.删除等操作 二.接口设计: 三.代码实现 public class Deque<E> { private List< ...
- C++单链表反转、两有序链表合并仍有序
1 #include<iostream> 2 3 struct Node 4 { 5 int data; 6 Node *next; 7 }; 8 9 typedef struct Nod ...
- java数据结构——单链表、双端链表、双向链表(Linked List)
1.继续学习单链表,终于摆脱数组的魔爪了,单链表分为数据域(前突)和引用域(指针域)(后继),还有一个头结点(就好比一辆火车,我们只关心火车头,不关心其它车厢,只需知晓车头顺藤摸瓜即可),头结点没有前 ...
- JAVA基础——链表结构之双端链表
双端链表:双端链表与传统链表非常相似.只是新增了一个属性-即对最后一个链结点的引用 如上图所示:由于有着对最后一个链结点的直接引用.所以双端链表比传统链表在某些方面要方便.比如在尾部插入一个链结点.双 ...
- Java实现 LeetCode 21 合并两个有序链表
21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1 ...
- c# 有序链表合并 链表反转
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 算法练习之合并两个有序链表, 删除排序数组中的重复项,移除元素,实现strStr(),搜索插入位置,无重复字符的最长子串
最近在学习java,但是对于数据操作那部分还是不熟悉 因此决定找几个简单的算法写,用php和java分别实现 1.合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两 ...
- 代码题(14)— 合并有序链表、数组、合并K个排序链表
1.21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出 ...
- leecode刷题(23)-- 合并两个有序链表
leecode刷题(23)-- 合并两个有序链表 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2-> ...
随机推荐
- javascript 漏洞
1.javascript语言中,每一个对象都有一个对应的原型对象,称为prototype对象. 继承是基于原型的! 2.prototype对象的作用,就是定义所有实例对象共享的属性和方法! 3.“原 ...
- Rhino Mock
mock interfaces, delegates and classes, including those with parameterized constructors. set expecta ...
- iOS边练边学--多线程练习的多图片下载 以及 使用第三方框架(SDWebImage)的多图片下载
一.自己实现多图片下载应该注意的问题 沙盒缓存的问题 程序缓存的问题 cell重复利用显示图片混乱的问题 -- 用户拖拽快,下载图片慢导致的 解决图片混乱引入NSOperation集合的问题 资源下载 ...
- Java基础-转义字符
Java中的字符占两个字节.一些常用的转义字符: ①\r表示接受键盘输入,相当于按下了回车键: ②\n表示换行: ③\t表示制表符,相当于Table键: ④\b表示退格键,相当于Back Space键 ...
- 【POJ 2250】Compromise(最长公共子序列LCS)
题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...
- make xconfig时,出现“Unable to find any QT installation"错误
在make menuconfig的时候,提示是找不到libncurses,最后其实是找不到libncurses-dev,又因为debian系统装的是libncurses5,所以装了libncurses ...
- (原)String类两种实例化的区别
String有两种实例化方式,一种是通过直接赋值的方式,另外一种是使用标准的new调用构造方法完成实例化. public class StringDemo { public static void m ...
- c++标准库和stl关系
C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...
- The illustrated guide to a Ph.D.
- ResultSet
在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Java代码Statement stmt = con. ...