实现基础的单链表及链表反转

    1. public class Node<T> {
    2. private T data;
    3. private Node<T> next;
    4. public Node(T data, Node<T> next) {
    5. this.data = data;
    6. this.next = next;
    7. }
    8. public T getData() {
    9. return data;
    10. }
    11. public void setData(T data) {
    12. this.data = data;
    13. }
    14. public Node<T> getNext() {
    15. return next;
    16. }
    17. public void setNext(Node<T> next) {
    18. this.next = next;
    19. }
    20. }
    1. public class SingleLinkedList<T> {
    2. private int size;
    3. private Node<T> head;
    4. /**
    5. * 添加
    6. * @param v
    7. */
    8. public void add(T v){
    9. if(size>0){
    10. Node<T> node=new Node<>(v,null);
    11. Node lastNode = getLastNode();
    12. if(lastNode!=null){
    13. lastNode.setNext(node);
    14. size++;
    15. }
    16. }else {
    17. if(head==null){
    18. head=new Node<>(v,null);
    19. }else {
    20. head.setData(v);
    21. }
    22. size++;
    23. }
    24. }
    25. public SingleLinkedList(){
    26. size=0;
    27. head=new Node<>(null,null);
    28. }
    29. /**
    30. * 具体位置添加
    31. * @param position
    32. * @param v
    33. */
    34. public void insert(int position, T v){
    35. if(position>=0 && position<size){
    36. Node insertNode=new Node(v,null);
    37. Node node=head;
    38. Node pre=null;
    39. if(position==0){
    40. insertNode.setNext(head);
    41. head=insertNode;
    42. size++;
    43. }else {
    44. for(int x=0;x<position;x++){
    45. pre=node;
    46. node=node.getNext();
    47. }
    48. pre.setNext(insertNode);
    49. insertNode.setNext(node);
    50. size++;
    51. }
    52. }
    53. }
    54. /**
    55. * 删除指定位置
    56. * @param position
    57. * @return
    58. */
    59. public T remove(int position)
    60. {
    61. if(position>=0 && position<size){
    62. Node<T> node=head;
    63. Node<T> pre=null;
    64. Node<T> next=null;
    65. T value=null;
    66. if(position==0){
    67. value= node.getData();
    68. node= node.getNext();
    69. head.setNext(null);
    70. head.setData(null);
    71. head=node;
    72. size--;
    73. }else {
    74. for(int x=0;x<position;x++){
    75. //找到删除的节点
    76. pre=node;//当前要删除的上个节点
    77. node=node.getNext();//当前节点
    78. next=node.getNext();//当前需要删除的下个节点
    79. }
    80. value= (T) node.getData();
    81. node.setNext(null);
    82. node.setData(null);
    83. node=null;
    84. pre.setNext(next);
    85. size--;
    86. }
    87. return value;
    88. }
    89. return null;
    90. }
    91. /**
    92. * 获取指定位置节点
    93. * @param position
    94. * @return
    95. */
    96. public Node getNode(int position){
    97. if(position>=0 && position<size){
    98. Node node=head;
    99. for(int x=0;x<position;x++){
    100. node=node.getNext();
    101. }
    102. return node;
    103. }else {
    104. return null;
    105. }
    106. }
    107. public Node getHead(){
    108. return head;
    109. }
    110. /**
    111. * 获取最后一个节点
    112. * @return
    113. */
    114. public Node getLastNode(){
    115. Node node=head;
    116. while (node !=null && node.getNext()!=null){
    117. node= node.getNext();
    118. }
    119. return node;
    120. }
    121. /**
    122. * 清空
    123. */
    124. public void clear(){
    125. for (int x=0;x<size;x++){
    126. Node next=head.getNext();
    127. head.setNext(null);
    128. head.setData(null);
    129. head= next;
    130. }
    131. size=0;
    132. }
    133. /**
    134. * 查看第一个元素的数据
    135. * @return
    136. */
    137. public T peek(){
    138. if(size>0 && head!=null){
    139. return head.getData();
    140. }
    141. return null;
    142. }
    143. /**
    144. * 反转打印
    145. */
    146. public Node reversal(){
    147. if (head == null){
    148. return head;
    149. }
    150. Node node=head;
    151. Node pre=null;
    152. Node cur=node;
    153. Node next=node.getNext();
    154. //1->2->3->4->5
    155. while (next!=null){
    156. next=cur.getNext();//记录当前节点的下一个节点
    157. cur.setNext(pre);//将当前节点的下一个节点指向当前节点的上一个节点//交换位置
    158. pre=cur;//前一个节点后移到当前节点
    159. cur=next;//当前节点设置成后一个节点
    160. }
    161. return pre;
    162. }
    163. /**
    164. * 正序打印
    165. */
    166. public void print(Node n){
    167. Node<T> node=n;
    168. while (node!=null){
    169. String data = (String) node.getData();
    170. System.out.println("data:"+data);
    171. node=node.getNext();
    172. }
    173. }
    174. public boolean isEmpty(){
    175. if(size==0 ){
    176. return true;
    177. }
    178. return false;
    179. }
    180. public int getSize(){
    181. return size;
    182. }
    183. }

    双链表实现及反转

    1. public class DoublyLinkedList<V> {
    2. private Node<V> first;
    3. private Node<V> last;
    4. private int size;
    5. public static class Node<V>{
    6. V data;
    7. Node<V> pre;
    8. Node<V> next;
    9. public Node(V data, Node<V> pre, Node<V> next) {
    10. this.data = data;
    11. this.pre = pre;
    12. this.next = next;
    13. }
    14. public Node<V> getPre() {
    15. return pre;
    16. }
    17. public void setPre(Node<V> pre) {
    18. this.pre = pre;
    19. }
    20. public Node<V> getNext() {
    21. return next;
    22. }
    23. public void setNext(Node<V> next) {
    24. this.next = next;
    25. }
    26. public V getData() {
    27. return data;
    28. }
    29. public void setData(V data) {
    30. this.data = data;
    31. }
    32. }
    33. public void add(V e){
    34. Node<V> l=last;
    35. Node<V> newNode=new Node<>(e,l,null);
    36. last=newNode;
    37. if(l==null){
    38. first=newNode;
    39. }else {
    40. l.next = newNode;
    41. }
    42. size++;
    43. }
    44. /**
    45. * 反转
    46. */
    47. public Node<V> reversal(){
    48. if(first!=null){
    49. Node<V> node=first;
    50. Node<V> pre=null;
    51. Node<V> cur=node;
    52. Node<V> next=cur.getNext();
    53. while (next!=null){
    54. next=cur.getNext();
    55. cur.setNext(pre);
    56. cur.setPre(next);
    57. pre=cur;
    58. cur=next;
    59. }
    60. return pre;
    61. }
    62. return null;
    63. }
    64. /**
    65. * 具体位置添加
    66. * @param position
    67. * @param v
    68. */
    69. public void insert(int position, V v){
    70. if(position>=0 && position<size){
    71. Node pre=null;
    72. if(position==0){
    73. // Node node=first;
    74. Node insertNode=new Node(v,null,first);
    75. first.setPre(insertNode);
    76. first=insertNode;
    77. size++;
    78. }else {
    79. Node insertNode=new Node(v,null,null);
    80. Node node=first;
    81. for(int x=0;x<position;x++){
    82. pre=node;
    83. node=node.getNext();
    84. }
    85. pre.setNext(insertNode);
    86. insertNode.setPre(pre);
    87. insertNode.setNext(node);
    88. node.setPre(insertNode);
    89. size++;
    90. }
    91. }
    92. }
    93. /**
    94. * 删除指定位置
    95. * @param position
    96. * @return
    97. */
    98. public V remove(int position)
    99. {
    100. if(position>=0 && position<size){
    101. Node<V> node=first;
    102. Node<V> pre=null;
    103. Node<V> next=null;
    104. V value=null;
    105. if(position==0){
    106. value= node.getData();
    107. node= node.getNext();
    108. first.setNext(null);
    109. first.setData(null);
    110. first=node;
    111. size--;
    112. }else {
    113. for(int x=0;x<position;x++){
    114. //找到删除的节点
    115. pre=node;//当前要删除的上个节点
    116. node=node.getNext();//当前节点
    117. next=node.getNext();//当前需要删除的下个节点
    118. }
    119. value= (V) node.getData();
    120. node.setNext(null);
    121. node.setData(null);
    122. node.setPre(null);
    123. node=null;
    124. pre.setNext(next);
    125. next.setPre(pre);
    126. size--;
    127. }
    128. return value;
    129. }
    130. return null;
    131. }
    132. /**
    133. * 获取指定位置节点
    134. * @param position
    135. * @return
    136. */
    137. public Node<V> getNode(int position){
    138. if(position>=0 && position<size){
    139. Node<V> node=first;
    140. for(int x=0;x<position;x++){
    141. node=node.getNext();
    142. }
    143. return node;
    144. }else {
    145. return null;
    146. }
    147. }
    148. public boolean isEmpty(){
    149. if(size==0 ){
    150. return true;
    151. }
    152. return false;
    153. }
    154. public int getSize(){
    155. return size;
    156. }
    157. /**
    158. * 查看第一个元素的数据
    159. * @return
    160. */
    161. public V peek(){
    162. if(size>0 && first!=null){
    163. return first.getData();
    164. }
    165. return null;
    166. }
    167. /**
    168. * 清空
    169. *
    170. */
    171. public void clear(){
    172. while (first!=null){
    173. Node<V> next=first.getNext();
    174. first.setNext(null);
    175. first.setData(null);
    176. first.setPre(null);
    177. first= next;
    178. }
    179. size=0;
    180. }
    181. public void print(){
    182. Node<V> node=first;
    183. while (node !=null){
    184. System.out.println("data="+node.getData());
    185. node=node.getNext();
    186. }
    187. }
    188. public void print2(Node<V> node){
    189. while (node !=null){
    190. System.out.println("data2="+node.getData());
    191. node=node.getNext();
    192. }
    193. }
    194. }