单链表

Java单链表、双端链表、有序链表实现-LMLPHP

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)

Java单链表、双端链表、有序链表实现-LMLPHP

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),因为其始终处于表头,对频繁操作最小数据项的应用,可以考虑使用有序链表实现,如:优先级队列

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

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

04-20 20:00
查看更多