单链表的简单实现

1、单链表的存储结构
Java代码 复制代码 收藏代码
  1. /**
  2. * 单链表
  3. * @author fox
  4. *
  5. */
  6. public class Node {
  7. private Object nodeValue;
  8. private Node nextNode;
  9. public Node() {
  10. nodeValue = null;
  11. nextNode = null;
  12. }
  13. public Node(Object item) {
  14. nodeValue = item;
  15. nextNode = null;
  16. }
  17. public Node(Object item, Node next) {
  18. nodeValue = item;
  19. nextNode = next;
  20. }
  21. public Object getNodeValue() {
  22. return nodeValue;
  23. }
  24. public void setNodeValue(Object nodeValue) {
  25. this.nodeValue = nodeValue;
  26. }
  27. public Node getNextNode() {
  28. return nextNode;
  29. }
  30. public void setNextNode(Node nextNode) {
  31. this.nextNode = nextNode;
  32. }
  33. }

2、由于单链表是一种线性结构,所以他实现了LinearList接口。
关于LinearList接口,详情请查看http://fox-ed.iteye.com/blog/1775542
详细实现方法如下:
Java代码 复制代码 收藏代码
  1. /**
  2. * 单链表的实现
  3. * @author fox
  4. *
  5. */
  6. public class SingleLinkList implements LinearList {
  7. private Node head;
  8. public SingleLinkList() {
  9. head = null;
  10. }
  11. public SingleLinkList(Node node) {
  12. this.head = node;
  13. }
  14. public boolean isEmpty() {
  15. if(head == null) {
  16. return true;
  17. } else {
  18. return false;
  19. }
  20. }
  21. public int size() {
  22. int size = 0;
  23. Node temp = this.head;
  24. while(temp != null) {
  25. size++;
  26. temp = temp.getNextNode();
  27. }
  28. return size;
  29. }
  30. public Object get(int index) {
  31. checkIndex(index);
  32. Node temp = this.head;
  33. for(int i = 0; i < index; i++) {
  34. temp = temp.getNextNode();
  35. }
  36. return (Object)temp.getNodeValue();
  37. }
  38. public void set(int index, Object o) {
  39. checkIndex(index);
  40. Node node = this.head;
  41. for(int i = 0; i < index; i++) {
  42. node = node.getNextNode();
  43. }
  44. node.setNodeValue(o);
  45. }
  46. public boolean add(int index, Object o) {
  47. if(index < 0) {
  48. throw new IndexOutOfBoundsException("下标错误:"+index);
  49. }
  50. if(head == null) {
  51. head = new Node(o);
  52. } else {
  53. Node temp = this.head;
  54. if(index == 0) {
  55. this.head = new Node(o,temp);
  56. } else {
  57. int i =0;
  58. while(temp.getNextNode() != null && i < index - 1) {
  59. i++;
  60. temp = temp.getNextNode();
  61. }
  62. temp.setNextNode(new Node(o,temp.getNextNode()));
  63. }
  64. }
  65. return true;
  66. }
  67. public boolean add(Object o) {
  68. return add(Integer.MAX_VALUE,o);
  69. }
  70. public Object remove(int index) {
  71. checkIndex(index);
  72. Object oldObj = null;
  73. Node temp = this.head;
  74. if(index == 0) {
  75. oldObj = (Object)head.getNodeValue();
  76. this.head = temp.getNextNode();
  77. temp = null;
  78. } else {
  79. int i = 0;
  80. while(temp.getNextNode() != null && i < index - 1) {
  81. i++;
  82. temp = temp.getNextNode();
  83. }
  84. Node node = temp.getNextNode();
  85. oldObj = (Object) node.getNodeValue();
  86. temp.setNextNode(node.getNextNode());
  87. node = null;
  88. }
  89. return oldObj;
  90. }
  91. public void clear() {
  92. int size = this.size();
  93. if(size != 0) {
  94. for(int i = size - 1;i >= 0; i--) {
  95. this.remove(i);
  96. }
  97. }
  98. }
  99. private void checkIndex(int index) {
  100. if(index < 0 || index > this.size() - 1) {
  101. throw new IndexOutOfBoundsException("下标错误:"+index);
  102. }
  103. }
  104. }  
posted on 2013-02-10 19:02  蜜雪薇琪  阅读(176)  评论(0编辑  收藏  举报