1 package Eric.ADT; 2 3 import java.util.Scanner; 4 5 import org.junit.Test; 6 7 /** 8 * <p> 9 * Title:MyLinkedList 10 * </p> 11 * <p> 12 * Description:用java实现单链表的基本操作 13 * </p> 14 * <p> 15 * Location:Frostburg 16 * </p> 17 * 18 * @author: Eric.Chen 19 * @date:2017年9月20日下午8:51:25 20 */ 21 public class MyLinkedList { 22 class Node {// 节点 23 public Object value = null;// 节点的数据域 24 public Node next = null;// 节点的指针域 25 } 26 27 private Node header = null;// 头结点 28 29 void initList()// 初始化链表 30 { 31 header = new Node(); 32 header.value = null; 33 header.next = null; 34 } 35 36 // 插入链表 37 void tailInsertList(Object element) {// 尾插法 38 Node e = new Node();// 创建一个新节点 39 e.value = element; 40 if (header.next == null)// 说明这是第一次插入数据 41 { 42 header.next = e; 43 } else// 说明这不是第一次插入数据 44 45 { 46 Node temp = header; 47 while (temp.next != null) { 48 temp = temp.next; 49 } 50 temp.next = e; 51 } 52 53 } 54 55 void headInsertList(Object element)// 用头插法将元素放到链表中 56 { 57 Node e = new Node(); 58 e.value = element; 59 if (header.next == null) { 60 header.next = e; 61 } else { 62 e.next = header.next; 63 header.next = e; 64 65 } 66 } 67 68 // 将element元素从链表中删除 69 void deleteList(Object element) { 70 Node temp = header; 71 while (temp.next != null) { 72 if (temp.next.value.equals(element))// 找到要删除的值 73 { 74 temp.next = temp.next.next;// 移动指针指向 75 } else { 76 temp = temp.next;// 继续往下搜索 77 } 78 } 79 80 } 81 82 int size() {// 链表长度 83 int i = 0; 84 if (header.next == null) 85 return 0; 86 else { 87 Node temp = header; 88 while (temp.next != null) { 89 temp = temp.next; 90 i++; 91 } 92 } 93 return i; 94 95 } 96 97 void printAllValues() {// 打印链表中所有数据 98 Node temp = header; 99 System.out.println("输出链表所有数据:"); 100 while (temp.next != null) { 101 System.out.print(temp.next.value + " "); 102 temp = temp.next; 103 } 104 } 105 106 public Object getElement(int i)// 获取指定位置上的元素 107 { 108 if (i <= 0 || i > size()) { 109 return null; 110 } 111 112 Node temp = header; 113 int count = 1; 114 Object index = null; 115 while (temp.next != null) { 116 if (count == i) { 117 index = temp.next.value; 118 } 119 temp = temp.next; 120 count++; 121 } 122 123 return index; 124 } 125 126 public boolean isContain(Object Ele)// 判断链表中是否含有指定元素 127 { 128 Node temp = header; 129 int count = 1; 130 int index = -1; 131 while (temp.next != null) { 132 if (temp.next.value == Ele) { 133 index = count; 134 } 135 temp = temp.next; 136 count++; 137 } 138 if (index == -1) 139 return false; 140 else 141 return true; 142 } 143 144 public int getIndex(Object Ele) {// 获取指定元素的位置 145 Node temp = header; 146 int count = 1; 147 int index = 0; 148 while (temp.next != null) { 149 if (temp.next.value == Ele) { 150 index = count; 151 } 152 temp = temp.next; 153 count++; 154 } 155 return index; 156 } 157 //测试代码 158 public static void main(String[] args) { 159 MyLinkedList list = new MyLinkedList(); 160 Scanner sc = new Scanner(System.in); 161 list.initList(); 162 for (int i = 0; i < 10; i++) { 163 list.tailInsertList(i); 164 } 165 list.printAllValues(); 166 System.out.println(); 167 int size = list.size(); 168 System.out.println("链表的长度为:" + size); 169 list.headInsertList(99); 170 list.printAllValues(); 171 System.out.println(); 172 int size2 = list.size(); 173 System.out.println("链表的长度为:" + size2); 174 /* 175 * System.out.println("请输入要查询的元素的位置:"); int index1 = sc.nextInt(); 176 * Object ele = list.getElement(index1); System.out.println("链表中第" + 177 * index1 + "个元素是:" + ele); System.out.println("请输入要查询的元素:"); Object 178 * value = sc.nextInt(); int index2 = list.getIndex(value); 179 * System.out.println("元素" + value + "的位置是:" + index2); 180 * System.out.println("请输入你要检验是否存在与链表中的元素:"); Object checkedElem = 181 * sc.nextInt(); boolean ifcontain = list.isContain(checkedElem); if 182 * (ifcontain) { System.out.println("该链表中有此元素!"); } else { 183 * System.out.println("该链表中没有此元素!"); } 184 */ 185 } 186 }