单链表的实现(Java实现)
package singlelinklist; public class Node { int item; Node next; public Node(int item) { this.item = item; this.next = null; } }
package singlelinklist; public class SingleLinkList { Node head; public SingleLinkList() { } public SingleLinkList(Node head) { this.head = head; } // 链表是否为空 public static boolean is_empty(Node head) { return head == null ? true : false; } // 链表长度 public static int length(Node head) { if (is_empty(head)) { return 0; } int length = 1; Node cur = head; while (cur.next != null) { length++; cur = cur.next; } return length; } // 遍历整个链表 public static void travel(Node head) { Node cur = head; System.out.println("链表遍历开始!!"); while (cur != null) { System.out.print(cur.item + " "); cur = cur.next; } System.out.println("\n链表遍历结束!!"); } // 链表头部添加元素 public static void add(SingleLinkList list, int item) { Node node = new Node(item); if (is_empty(list.head)) { list.head = node; } else { node.next = list.head; list.head = node; } } // 链表尾部添加元素 public static void append(SingleLinkList list, int item) { Node node = new Node(item); if (is_empty(list.head)) { list.head = node; } else { Node cur = list.head; while (cur.next != null) { cur = cur.next; } cur.next = node; } } // 指定位置添加元素 public static void insert(SingleLinkList list, int pos, int item) { if (pos <= 0) { add(list, item); } else if (pos >= length(list.head)) { append(list, item); } else { Node node = new Node(item); Node pre = list.head; for (int i = 0; i < pos - 1; i++) { pre = pre.next; } node.next = pre.next; pre.next = node; } } // 删除节点 public static void remove(SingleLinkList list, int item) { Node pre = null; Node cur = list.head; while (cur != null) { if (cur.item == item) { if (cur == list.head) { list.head = cur.next; } else { pre.next = cur.next; } break; } else { pre = cur; cur = cur.next; } } } // 查找节点是否存在 public static boolean search(Node head, int item) { Node cur = head; while (cur != null) { if (cur.item == item) { return true; } else { cur = cur.next; } } return false; } }
package singlelinklist; public class SingleLinkListTest { public static void main(String[] args) { Node head = new Node(100); SingleLinkList list = new SingleLinkList(head); if (SingleLinkList.is_empty(list.head)) { System.out.println("链表为空,添加元素麻利的~"); System.out .println("************************************************"); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); System.out.println("头部插入节点"); SingleLinkList.add(list, 10); SingleLinkList.add(list, 20); SingleLinkList.add(list, 30); SingleLinkList.add(list, 15); SingleLinkList.add(list, 25); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); System.out.println("尾部插入几个节点"); SingleLinkList.append(list, 100); SingleLinkList.append(list, 200); SingleLinkList.append(list, 500); SingleLinkList.append(list, 400); SingleLinkList.append(list, 300); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); SingleLinkList.insert(list, 1, 77); SingleLinkList.insert(list, 0, 88); SingleLinkList.insert(list, SingleLinkList.length(list.head) - 1, 66); SingleLinkList.insert(list, SingleLinkList.length(list.head), 55); SingleLinkList.insert(list, 5, 44); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); System.out.println("删除几个节点试试...."); SingleLinkList.remove(list, 88); SingleLinkList.remove(list, 55); SingleLinkList.remove(list, 300); SingleLinkList.remove(list, 44); SingleLinkList.remove(list, 25); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); } else { System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); System.out.println("头部插入节点"); SingleLinkList.add(list, 10); SingleLinkList.add(list, 20); SingleLinkList.add(list, 30); SingleLinkList.add(list, 15); SingleLinkList.add(list, 25); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); System.out.println("尾部插入几个节点"); SingleLinkList.append(list, 100); SingleLinkList.append(list, 200); SingleLinkList.append(list, 500); SingleLinkList.append(list, 400); SingleLinkList.append(list, 300); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); SingleLinkList.insert(list, 1, 77); SingleLinkList.insert(list, 0, 88); SingleLinkList.insert(list, SingleLinkList.length(list.head) - 1, 66); SingleLinkList.insert(list, SingleLinkList.length(list.head), 55); SingleLinkList.insert(list, 5, 44); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out .println("************************************************"); System.out.println("删除几个节点试试...."); SingleLinkList.remove(list, 88); SingleLinkList.remove(list, 55); SingleLinkList.remove(list, 300); SingleLinkList.remove(list, 44); SingleLinkList.remove(list, 25); System.out.println("链表的长度为:" + SingleLinkList.length(list.head)); SingleLinkList.travel(list.head); System.out.println(SingleLinkList.search(list.head, 15)); } } }