单链表:

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.mycompany.data.structure.line;
  7. /**
  8. *单链表
  9. * @author wyman
  10. */
  11. public class SingleLinkedList<T> {
  12. T data;
  13. SingleLinkedList next;
  14. public SingleLinkedList<T> initList() {
  15. return new SingleLinkedList<T>(); //To change body of generated methods, choose Tools | Templates.
  16. }
  17. //over 不知道对不对
  18. public SingleLinkedList<T> initList(int i) {
  19. SingleLinkedList Head = new SingleLinkedList<T>();
  20. SingleLinkedList tmp = Head;
  21. while (i > 0) {
  22. tmp.next = new SingleLinkedList();
  23. tmp = tmp.next;
  24. i--;
  25. }
  26. return Head; //To change body of generated methods, choose Tools | Templates.
  27. }
  28. //over
  29. public static void clearList(SingleLinkedList head) {
  30. head.next = null; //To change body of generated methods, choose Tools | Templates.
  31. }
  32. //over
  33. public static boolean listEmpty(SingleLinkedList head) {
  34. if (null != head.next) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. //over
  40. public int locateElem(SingleLinkedList head, T t) {
  41. int i = 1;
  42. SingleLinkedList node = head.next;
  43. while (true) {
  44. if (node == null) {
  45. return 0;
  46. }
  47. if (node == t) {
  48. return i;
  49. }
  50. node = node.next;
  51. i++;
  52. }
  53. }
  54. //over
  55. public T getElem(SingleLinkedList head, int i) {
  56. SingleLinkedList node = head.next;
  57. while (i > 1) {
  58. node = node.next;
  59. if (null == node) {
  60. break;
  61. }
  62. i--;
  63. }
  64. return (T) node;
  65. }
  66. //over
  67. public T priorElem(SingleLinkedList head, T t) {
  68. SingleLinkedList node = head.next;
  69. if (node == t) {
  70. return null;
  71. }
  72. while (true) {
  73. if (node == null) {
  74. return null;
  75. }
  76. if (node.next == t) {
  77. return (T) node;
  78. }
  79. node = node.next;
  80. }
  81. }
  82. //over
  83. public T nextrElem(SingleLinkedList head, T t) {
  84. SingleLinkedList node = head.next;
  85. while (true) {
  86. if (node == t) {
  87. return (T) node.next;
  88. }
  89. if (node.next == null) {
  90. return null;
  91. }
  92. node = node.next;
  93. }
  94. }
  95. public boolean listInsert(SingleLinkedList head, int i, T t) {
  96. if (i < 1) {
  97. return false;
  98. }
  99. SingleLinkedList node = head;
  100. for (int index = 1; index < i; index++) {
  101. if (node.next == null) {
  102. return false;
  103. }
  104. node = node.next;
  105. }
  106. SingleLinkedList after_insert_node = node.next;
  107. SingleLinkedList insertNode = (SingleLinkedList) t;
  108. node.next = insertNode;
  109. insertNode.next = after_insert_node;
  110. return true;
  111. }
  112. public T listDelete(SingleLinkedList head, int target) {
  113. SingleLinkedList node = head;
  114. int node_index = 0;
  115. while (true) {
  116. if (null == node) {
  117. return null;
  118. }
  119. if (node_index == target - 1) {
  120. SingleLinkedList deleteNode = node.next;
  121. node.next = deleteNode.next;
  122. return (T) deleteNode;
  123. }
  124. node = node.next;
  125. node_index++;
  126. }
  127. }
  128. public void listTraverse() {
  129. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  130. }
  131. public int getSize(SingleLinkedList head) {
  132. int i = 0;
  133. SingleLinkedList node = head.next;
  134. while (true) {
  135. if (null == node) {
  136. break;
  137. }
  138. i++;
  139. node = node.next;
  140. }
  141. return i;
  142. }
  143. public void DestoryList(SingleLinkedList head) {
  144. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  145. }
  146. }

静态链表:数组表示的单链表

  1. /**
  2. *静态链表(用数组存取)
  3. * @author wyman
  4. */
  5. public class StaticListElement {
  6. int cur;
  7. Object data;
  8. public StaticListElement() {
  9. }
  10. public StaticListElement(int cur, Object data) {
  11. this.cur = cur;
  12. this.data = data;
  13. }
  14. }
  15. /*
  16. * To change this license header, choose License Headers in Project Properties.
  17. * To change this template file, choose Tools | Templates
  18. * and open the template in the editor.
  19. */
  20. package com.mycompany.data.structure.line;
  21. /**
  22. *
  23. * @author wyman
  24. */
  25. public class StaticList {
  26. StaticListElement[] sle;
  27. int size;
  28. //over 不
  29. public StaticList initList(int i) {
  30. StaticList Slist = new StaticList();
  31. Slist.sle = new StaticListElement[i];
  32. Slist.size = i;
  33. return Slist;
  34. }
  35. //over
  36. public static void clearList(StaticList Slist) {
  37. Slist.size = 0; //To change body of generated methods, choose Tools | Templates.
  38. }
  39. //over
  40. public static boolean listEmpty(StaticList Slist) {
  41. if (0 < Slist.size) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. //over
  47. public int locateElem(StaticList Slist, StaticListElement t) {
  48. for (int i = 0; i < Slist.size; i++) {
  49. if (t.data == Slist.sle[i].data) {
  50. return Slist.sle[i].cur;
  51. }
  52. }
  53. return 0;
  54. }
  55. //over
  56. public StaticListElement getElem(StaticList Slist, int k) {
  57. if (k <= 0 || k > size) {
  58. return null;
  59. }
  60. StaticListElement head = Slist.sle[0];
  61. StaticListElement tmp = head;
  62. while (tmp.cur != 0) {
  63. if (k == tmp.cur) {
  64. return tmp;
  65. }
  66. tmp = Slist.sle[tmp.cur];
  67. }
  68. return null;
  69. }
  70. //over
  71. public StaticListElement priorElem(StaticList Slist, StaticListElement t) {
  72. for (int i = 0; i < Slist.size; i++) {
  73. if (t.data == Slist.sle[i].data) {
  74. return getElem(Slist, Slist.sle[i].cur - 1);
  75. }
  76. }
  77. return null;
  78. }
  79. //over
  80. public StaticListElement nextrElem(StaticList Slist, StaticListElement t) {
  81. for (int i = 0; i < Slist.size; i++) {
  82. if (t.data == Slist.sle[i].data) {
  83. return getElem(Slist, Slist.sle[i].cur + 1);
  84. }
  85. }
  86. return null;
  87. }
  88. public boolean listInsert(StaticList Slist, int i, StaticListElement t) {
  89. if (i < 1 || i > Slist.size + 1) {
  90. return false;
  91. }
  92. //若空间满了创建一个新的数组
  93. StaticListElement before_node = getElem(Slist, i - 1);
  94. StaticListElement after_node = getElem(Slist, i);
  95. Slist.sle[i] = t;
  96. t.cur = before_node.cur;
  97. before_node.cur = i;
  98. return true;
  99. }
  100. public StaticListElement listDelete(StaticList Slist, int i) {
  101. if (i < 1 || i > Slist.size ) {
  102. return null;
  103. }
  104. StaticListElement target_node = getElem(Slist, i - 1);
  105. StaticListElement pre_node=priorElem(Slist, target_node);
  106. pre_node.cur=target_node.cur;
  107. return target_node;
  108. }
  109. public void listTraverse() {
  110. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  111. }
  112. public int getSize(StaticList Slist) {
  113. return Slist.size-1;
  114. }
  115. public void DestoryList(SingleLinkedList head) {
  116. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  117. }
  118. }

循环链表:

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.mycompany.data.structure.line;
  7. /**
  8. *
  9. * @author wyman
  10. */
  11. public class circularLinkedList<T> {
  12. T data;
  13. circularLinkedList next;
  14. public circularLinkedList<T> initList() {
  15. return new circularLinkedList<T>(); //To change body of generated methods, choose Tools | Templates.
  16. }
  17. //over 不知道对不对
  18. public circularLinkedList initList(int i) {
  19. circularLinkedList head = new circularLinkedList<T>();
  20. circularLinkedList tmp = head;
  21. while (i > 0) {
  22. tmp.next = new circularLinkedList();
  23. tmp = tmp.next;
  24. i--;
  25. }
  26. tmp.next = head;
  27. return head; //To change body of generated methods, choose Tools | Templates.
  28. }
  29. //over
  30. public static void clearList(circularLinkedList head) {
  31. head.next = null; //To change body of generated methods, choose Tools | Templates.
  32. }
  33. //over
  34. public static boolean listEmpty(circularLinkedList head) {
  35. if (null != head.next) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. //over
  41. public int locateElem(circularLinkedList head, T t) {
  42. int i = 1;
  43. circularLinkedList node = head.next;
  44. while (true) {
  45. if (node == head) {
  46. return 0;
  47. }
  48. if (node == t) {
  49. return i;
  50. }
  51. node = node.next;
  52. i++;
  53. }
  54. }
  55. //over
  56. public T getElem(circularLinkedList head, int i) {
  57. circularLinkedList node = head.next;
  58. while (i > 1) {
  59. node = node.next;
  60. if (head == node) {
  61. break;
  62. }
  63. i--;
  64. }
  65. return (T) node;
  66. }
  67. //over
  68. public T priorElem(circularLinkedList head, T t) {
  69. circularLinkedList node = head.next;
  70. if (node == t && node == head) {
  71. return null;
  72. }
  73. while (true) {
  74. if (node == head) {
  75. return null;
  76. }
  77. if (node.next == t) {
  78. return (T) node;
  79. }
  80. node = node.next;
  81. }
  82. }
  83. //over
  84. public T nextrElem(circularLinkedList head, T t) {
  85. circularLinkedList node = head.next;
  86. if (node == head) {
  87. return null;
  88. }
  89. while (true) {
  90. if (node == t) {
  91. return (T) node.next;
  92. }
  93. if (node.next == head) {
  94. return null;
  95. }
  96. node = node.next;
  97. }
  98. }
  99. public boolean listInsert(circularLinkedList head, int i, T t) {
  100. if (i < 1) {
  101. return false;
  102. }
  103. circularLinkedList node = head;
  104. for (int index = 1; index < i; index++) {
  105. if (node.next == head) {
  106. return false;
  107. }
  108. node = node.next;
  109. }
  110. circularLinkedList after_insert_node = node.next;
  111. circularLinkedList insertNode = (circularLinkedList) t;
  112. node.next = insertNode;
  113. insertNode.next = after_insert_node;
  114. return true;
  115. }
  116. public T listDelete(circularLinkedList head, int target) {
  117. circularLinkedList node = head;
  118. int node_index = 0;
  119. while (node.next != head && node_index < target - 1) {
  120. node = node.next;
  121. node_index++;
  122. }
  123. if (node_index == target - 1) {
  124. circularLinkedList deleteNode = node.next;
  125. node.next = deleteNode.next;
  126. return (T) deleteNode;
  127. }
  128. return null;
  129. }
  130. public void listTraverse() {
  131. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  132. }
  133. public int getSize(circularLinkedList head) {
  134. int i = 0;
  135. circularLinkedList node = head.next;
  136. while (true) {
  137. if (head == node) {
  138. break;
  139. }
  140. i++;
  141. node = node.next;
  142. }
  143. return i+1;
  144. }
  145. public void DestoryList(SingleLinkedList head) {
  146. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  147. }
  148. }

双向链表:

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.mycompany.data.structure.line;
  7. /**
  8. *
  9. * @author wyman
  10. */
  11. public class DuLinkList<T> {
  12. T data;
  13. DuLinkList next;
  14. DuLinkList pre;
  15. public DuLinkList<T> initList() {
  16. return new DuLinkList<T>(); //To change body of generated methods, choose Tools | Templates.
  17. }
  18. //over 不知道对不对
  19. public DuLinkList initList(int i) {
  20. DuLinkList head = new DuLinkList<T>();
  21. DuLinkList tmp = head;
  22. while (i > 0) {
  23. DuLinkList node=new DuLinkList();
  24. node.pre=tmp;
  25. tmp.next = node;
  26. tmp = tmp.next;
  27. i--;
  28. }
  29. tmp.next = head;
  30. return head; //To change body of generated methods, choose Tools | Templates.
  31. }
  32. //over
  33. public static void clearList(DuLinkList head) {
  34. head.next = head;
  35. head.pre=head;//To change body of generated methods, choose Tools | Templates.
  36. }
  37. //over
  38. public static boolean listEmpty(DuLinkList head) {
  39. if (head != head.next) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. //over
  45. public int locateElem(DuLinkList head, T t) {
  46. int i = 1;
  47. DuLinkList node = head.next;
  48. while (true) {
  49. if (node == head) {
  50. return 0;
  51. }
  52. if (node == t) {
  53. return i;
  54. }
  55. node = node.next;
  56. i++;
  57. }
  58. }
  59. //over
  60. public T getElem(DuLinkList head, int i) {
  61. DuLinkList node = head;
  62. while (i > 0) {
  63. node = node.next;
  64. if (head == node) {
  65. break;
  66. }
  67. i--;
  68. }
  69. return (T) node;
  70. }
  71. //over
  72. public T priorElem(DuLinkList head, T t) {
  73. return (T) ((DuLinkList)t).pre;
  74. }
  75. //over
  76. public T nextrElem(DuLinkList head, T t) {
  77. return (T) ((DuLinkList)t).next;
  78. }
  79. public boolean listInsert(DuLinkList head, int i, T t) {
  80. if (i < 1) {
  81. return false;
  82. }
  83. DuLinkList node = head;
  84. for (int index = 1; index < i; index++) {
  85. if (node.next == head) {
  86. return false;
  87. }
  88. node = node.next;
  89. }
  90. DuLinkList after_insert_node = node.next;
  91. DuLinkList insertNode = (DuLinkList) t;
  92. insertNode.pre=node;
  93. node.next = insertNode;
  94. insertNode.next = after_insert_node;
  95. after_insert_node.pre=insertNode;
  96. return true;
  97. }
  98. public T listDelete(DuLinkList head, int target) {
  99. DuLinkList node = head;
  100. int node_index = 0;
  101. while (node.next != head && node_index < target - 1) {
  102. node = node.next;
  103. node_index++;
  104. }
  105. if (node_index == target - 1) {
  106. DuLinkList deleteNode = node.next;
  107. node.next = deleteNode.next;
  108. deleteNode.next.pre=node;
  109. return (T) deleteNode;
  110. }
  111. return null;
  112. }
  113. public void listTraverse() {
  114. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  115. }
  116. public int getSize(DuLinkList head) {
  117. int i = 0;
  118. DuLinkList node = head.next;
  119. while (true) {
  120. if (head == node) {
  121. break;
  122. }
  123. i++;
  124. node = node.next;
  125. }
  126. return i+1;
  127. }
  128. public void DestoryList(SingleLinkedList head) {
  129. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  130. }
  131. }