单链表

insertFirst:在表头插入一个新的链接点,时间复杂度为O(1)

deleteFirst:删除表头的链接点,时间复杂度为O(1)

有了这两个方法,就可以用单链表来实现一个栈了,见http://blog.csdn.net/a19881029/article/details/22579759

find:查找包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N)

remove:删除包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N)

  1. public class LinkedList {
  2. private class Data{
  3. private Object obj;
  4. private Data next = null;
  5. Data(Object obj){
  6. this.obj = obj;
  7. }
  8. }
  9. private Data first = null;
  10. public void insertFirst(Object obj){
  11. Data data = new Data(obj);
  12. data.next = first;
  13. first = data;
  14. }
  15. public Object deleteFirst() throws Exception{
  16. if(first == null)
  17. throw new Exception("empty!");
  18. Data temp = first;
  19. first = first.next;
  20. return temp.obj;
  21. }
  22. public Object find(Object obj) throws Exception{
  23. if(first == null)
  24. throw new Exception("LinkedList is empty!");
  25. Data cur = first;
  26. while(cur != null){
  27. if(cur.obj.equals(obj)){
  28. return cur.obj;
  29. }
  30. cur = cur.next;
  31. }
  32. return null;
  33. }
  34. public void remove(Object obj) throws Exception{
  35. if(first == null)
  36. throw new Exception("LinkedList is empty!");
  37. if(first.obj.equals(obj)){
  38. first = first.next;
  39. }else{
  40. Data pre = first;
  41. Data cur = first.next;
  42. while(cur != null){
  43. if(cur.obj.equals(obj)){
  44. pre.next = cur.next;
  45. }
  46. pre = cur;
  47. cur = cur.next;
  48. }
  49. }
  50. }
  51. public boolean isEmpty(){
  52. return (first == null);
  53. }
  54. public void display(){
  55. if(first == null)
  56. System.out.println("empty");
  57. Data cur = first;
  58. while(cur != null){
  59. System.out.print(cur.obj.toString() + " -> ");
  60. cur = cur.next;
  61. }
  62. System.out.print("\n");
  63. }
  64. public static void main(String[] args) throws Exception {
  65. LinkedList ll = new LinkedList();
  66. ll.insertFirst(4);
  67. ll.insertFirst(3);
  68. ll.insertFirst(2);
  69. ll.insertFirst(1);
  70. ll.display();
  71. ll.deleteFirst();
  72. ll.display();
  73. ll.remove(3);
  74. ll.display();
  75. System.out.println(ll.find(1));
  76. System.out.println(ll.find(4));
  77. }
  78. }
  1. 1 -> 2 -> 3 -> 4 ->
  2. 2 -> 3 -> 4 ->
  3. 2 -> 4 ->
  4. null
  5. 4

双端链表(不是双向链表):

与单向链表的不同之处在保存有对最后一个链接点的引用(last)

insertFirst:在表头插入一个新的链接点,时间复杂度O(1)

insertLast:在表尾插入一个新的链接点,时间复杂度O(1)

deleteFirst:删除表头的链接点,时间复杂度O(1)

deleteLast::删除表尾的链接点,由于只保存了表尾的链接点,而没有保存表尾的前一个链接点(这里就体现出双向链表的优势了),所以在删除表尾链接点时需要遍历以找到表尾链接点的前一个链接点,需查找N-1次,也就是O(N)

有了这几个方法就可以用双端链表来实现一个队列了,http://blog.csdn.net/a19881029/article/details/22654121

  1. public class FirstLastList {
  2. private class Data{
  3. private Object obj;
  4. private Data next = null;
  5. Data(Object obj){
  6. this.obj = obj;
  7. }
  8. }
  9. private Data first = null;
  10. private Data last = null;
  11. public void insertFirst(Object obj){
  12. Data data = new Data(obj);
  13. if(first == null)
  14. last = data;
  15. data.next = first;
  16. first = data;
  17. }
  18. public void insertLast(Object obj){
  19. Data data = new Data(obj);
  20. if(first == null){
  21. first = data;
  22. }else{
  23. last.next = data;
  24. }
  25. last = data;
  26. }
  27. public Object deleteFirst() throws Exception{
  28. if(first == null)
  29. throw new Exception("empty");
  30. Data temp = first;
  31. if(first.next == null)
  32. last = null;
  33. first = first.next;
  34. return temp.obj;
  35. }
  36. public void deleteLast() throws Exception{
  37. if(first == null)
  38. throw new Exception("empty");
  39. if(first.next == null){
  40. first = null;
  41. last = null;
  42. }else{
  43. Data temp = first;
  44. while(temp.next != null){
  45. if(temp.next == last){
  46. last = temp;
  47. last.next = null;
  48. break;
  49. }
  50. temp = temp.next;
  51. }
  52. }
  53. }
  54. public void display(){
  55. if(first == null)
  56. System.out.println("empty");
  57. Data cur = first;
  58. while(cur != null){
  59. System.out.print(cur.obj.toString() + " -> ");
  60. cur = cur.next;
  61. }
  62. System.out.print("\n");
  63. }
  64. public static void main(String[] args) throws Exception {
  65. FirstLastList fll = new FirstLastList();
  66. fll.insertFirst(2);
  67. fll.insertFirst(1);
  68. fll.display();
  69. fll.insertLast(3);
  70. fll.display();
  71. fll.deleteFirst();
  72. fll.display();
  73. fll.deleteLast();
  74. fll.display();
  75. }
  76. }
  1. 1 -> 2 ->
  2. 1 -> 2 -> 3 ->
  3. 2 -> 3 ->
  4. 2 ->

有序链表:链表中的数据按从小到大排列

  1. public class SortedList {
  2. private class Data{
  3. private Object obj;
  4. private Data next = null;
  5. Data(Object obj){
  6. this.obj = obj;
  7. }
  8. }
  9. private Data first = null;
  10. public void insert(Object obj){
  11. Data data = new Data(obj);
  12. Data pre = null;
  13. Data cur = first;
  14. while(cur != null && (Integer.valueOf(data.obj.toString())
  15. .intValue() > Integer.valueOf(cur.obj.toString())
  16. .intValue())){
  17. pre = cur;
  18. cur = cur.next;
  19. }
  20. if(pre == null)
  21. first = data;
  22. else
  23. pre.next = data;
  24. data.next = cur;
  25. }
  26. public Object deleteFirst() throws Exception{
  27. if(first == null)
  28. throw new Exception("empty!");
  29. Data temp = first;
  30. first = first.next;
  31. return temp.obj;
  32. }
  33. public void display(){
  34. if(first == null)
  35. System.out.println("empty");
  36. System.out.print("first -> last : ");
  37. Data cur = first;
  38. while(cur != null){
  39. System.out.print(cur.obj.toString() + " -> ");
  40. cur = cur.next;
  41. }
  42. System.out.print("\n");
  43. }
  44. public static void main(String[] args) throws Exception{
  45. SortedList sl = new SortedList();
  46. sl.insert(80);
  47. sl.insert(2);
  48. sl.insert(100);
  49. sl.display();
  50. System.out.println(sl.deleteFirst());
  51. sl.insert(33);
  52. sl.display();
  53. sl.insert(99);
  54. sl.display();
  55. }
  56. }
  1. first -> last : 2 -> 80 -> 100 ->
  2. 2
  3. first -> last : 33 -> 80 -> 100 ->
  4. first -> last : 33 -> 80 -> 99 -> 100 ->

表的插入和删除平均需要比较N/2次,即O(N),但是获取最小数据项只需O(1),因为其始终处于表头,对频繁操作最小数据项的应用,可以考虑使用有序链表实现,如:优先级队列

和数组相比,链表的优势在于长度不受限制,并且在进行插入和删除操作时,不需要移动数据项,故尽管某些操作的时间复杂度与数组想同,实际效率上还是比数组要高很多

劣势在于随机访问,无法像数组那样直接通过下标找到特定的数据项

Java单链表、双端链表、有序链表实现的更多相关文章

  1. Java数据结构——用双端链表实现队列

    //================================================= // File Name : LinkQueue_demo //---------------- ...

  2. java数据结构-09双端队列

    一.相关概念: (Deque)双端队列能够在队头.队尾进行添加.删除等操作  二.接口设计:  三.代码实现 public class Deque<E> { private List< ...

  3. C++单链表反转、两有序链表合并仍有序

    1 #include<iostream> 2 3 struct Node 4 { 5 int data; 6 Node *next; 7 }; 8 9 typedef struct Nod ...

  4. java数据结构——单链表、双端链表、双向链表(Linked List)

    1.继续学习单链表,终于摆脱数组的魔爪了,单链表分为数据域(前突)和引用域(指针域)(后继),还有一个头结点(就好比一辆火车,我们只关心火车头,不关心其它车厢,只需知晓车头顺藤摸瓜即可),头结点没有前 ...

  5. JAVA基础——链表结构之双端链表

    双端链表:双端链表与传统链表非常相似.只是新增了一个属性-即对最后一个链结点的引用 如上图所示:由于有着对最后一个链结点的直接引用.所以双端链表比传统链表在某些方面要方便.比如在尾部插入一个链结点.双 ...

  6. Java实现 LeetCode 21 合并两个有序链表

    21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1 ...

  7. c# 有序链表合并 链表反转

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 算法练习之合并两个有序链表, 删除排序数组中的重复项,移除元素,实现strStr(),搜索插入位置,无重复字符的最长子串

    最近在学习java,但是对于数据操作那部分还是不熟悉 因此决定找几个简单的算法写,用php和java分别实现 1.合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两 ...

  9. 代码题(14)— 合并有序链表、数组、合并K个排序链表

    1.21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出 ...

  10. leecode刷题(23)-- 合并两个有序链表

    leecode刷题(23)-- 合并两个有序链表 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2-> ...

随机推荐

  1. 阿里面试回来,想和Java程序员谈一谈(转载)

    引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的对话都给记下来.LZ自己当初面试完以后,除了记住一些聊过的知识点以外,具体的内容 ...

  2. hdu1231 最大连续子序列

    #include<stdio.h> #include<string.h> #define maxn 10010 int a[maxn],dp[maxn]; int main() ...

  3. 【HDU 1009】FatMouse' Trade

    题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the ware ...

  4. Oracle查看锁表

     查看锁表进程SQL语句1: select sess.sid,     sess.serial#,     lo.oracle_username,     lo.os_user_name,     a ...

  5. Oracle自定义函数1

    用户定义函数是存储在数据库中的代码块,可以把值返回到调用程序.调用时如同系统函数一样,如max(value)函数,其中,value被称为参数.函数参数有3种类型. IN 参数类型:表示输入给函数的参数 ...

  6. .net String.Format数字格式化输出

    内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...

  7. codeforces 359D 二分答案+RMQ

    上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...

  8. Tarjan算法详解理解集合

    [功能] Tarjan算法的用途之一是,求一个有向图G=(V,E)里极大强连通分量.强连通分量是指有向图G里顶点间能互相到达的子图.而如果一个强连通分量已经没有被其它强通分量完全包含的话,那么这个强连 ...

  9. 面试题52:缺少i的乘积数组

    vector<int> multiply(const vector<int>& A) { int len = A.size(); vector<); result ...

  10. ssh中getHibernateTemplate()的方法

    spring接着又把业务类中的查询也封装成了find() //用来实现分页 /*HibernateTemplate ht=this.getHibernateTemplate(); DetachedCr ...