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

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

原创 2014年03月31日 23:45:35

  • 65040

单链表:

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

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

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

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

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

[java] view plain copy

  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. }

[plain] view plain copy

  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

[java] view plain copy

  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. }

[plain] view plain copy

  1. 1 -> 2 ->
  2. 1 -> 2 -> 3 ->
  3. 2 -> 3 ->
  4. 2 ->

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

[java] view plain copy

  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. }

[plain] view plain copy

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

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

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

原文地址:https://www.cnblogs.com/xc1234/p/8611374.html

时间: 2024-12-21 02:06:21

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

递归逆转链表和递归合并有序链表的代码

#include <stdio.h> struct node { int val; struct node *pNext; }; struct node *gen() { struct node *pHead = NULL; for(int i = 10; i > 0; i--){ struct node * p = malloc(sizeof(struct node)); p -> val = i; p -> pNext = pHead; pHead = p; } retu

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

//================================================= // File Name : LinkQueue_demo //------------------------------------------------------------------------------ // Author : Common //类名:FirstLastList //属性: //方法: class FirstLastList_long{ private Lin

链表--合并两个有序链表

leetcode 21 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 还是采用递归的方法,先贴出代码: public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if( l1 == null ) { return l2; } if( l2 == null ) {

将两个有序链表合并成一个有序链表

问题定义: 写一个函数SortedMerge函数,该函数有两个参数,都是递增的链表,函数的功能就是合并这两个递增的链表为一个递增的链表,SortedMerge的返回值是新的链表.新链表由前两个链表按元素递增顺序合并而成,也就是说它不会创建新的元素. 比如:这里有两个链表,分别是 list1: 5->10->15 list2: 2->3->20 SortedMerge函数返回一个指向新链表的指针,新链表应该是如下这样的:2->3->5->10->15->

java中的双端队列及使用

直接上代码吧. package collections; import java.util.Deque; import java.util.LinkedList; /** * @Package collections * @date 2017-11-28下午5:53:32 */ public class DequeTest { /** * @param args */ public static void main(String[] args) { Deque<String> deque =

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

1 #include<iostream> 2 3 struct Node 4 { 5 int data; 6 Node *next; 7 }; 8 9 typedef struct Node Node; 10 11 Node *Reverse(Node *head) 12 { 13 if (NULL == head || NULL == head->next) 14 return head; 15 Node *p1 = head; 16 Node *p2 = p1->next; 1

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

双端链表:双端链表与传统链表非常相似.只是新增了一个属性-即对最后一个链结点的引用 如上图所示:由于有着对最后一个链结点的直接引用.所以双端链表比传统链表在某些方面要方便.比如在尾部插入一个链结点.双端链表可以进行直接操作 但传统链表只能通过next节点循环找到最后链结点操作.所以双端链表适合制造队列. 下面的双端链表类.有几个重要方法. insertFirst(插入首链结点) 这个方法与上篇博文的单链表是基本一样的.唯一区别就是,多了个last引用的操作.正常由于last是指向尾链结点的引用,

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinkedListTest { class Program { static void Main(string[] args) { LinkList<int> linkA = new LinkList<int>(); linkA.A

两个有序链表合成一个有序链表

RT.... 无聊帮朋友撸个 C++ 作业.. = = 1 /* 2 无聊帮朋友撸个作业...... \ = v = / 3 4 两个有序链表合成为一个有序链表. 5 要求: 6 1. 每个链表元素里的值不超过int范围. 7 2. 两个链表要求为升序(从小到大). 8 9 10 2016.1.5 author : 加德满都的猫会爬树 11 12 */ 13 14 #include <iostream> 15 using namespace std; 16 const int MAX = 10