特点
- 链表是以节点(node)的方式储存数据的,是链式储存
- 每个节点包括数据域(data),和next域,指向下一个节点
- 链表分为带头节点的链表,和不带头节点的链表,根据需求而定,上图为不带头节点的链表
- 单链表是有序的,但是在内存空间中是无序的
public class LinkedList<T> {
private int size;
private Node node;
/**
* 向单链表末尾追加元素
* @param data
*/
public void append(T data) {
if (node == null) {
node = new Node(data);
size = 1;
return;
}
Node temp = node;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new Node(data);
size++;
}
/**
* 在指定位置插入元素
* @param data 元素
* @param index 索引位置
*/
public void appendInIndex(T data,int index) {
if (index > size || index < 0) {
System.out.println("索引越界");
return;
}
Node newNode = new Node(data);
if (node == null) {
node = newNode;
size++;
return;
}
int i = 0;
Node temp = node;
Node last = node;
while (last.next != null) {
if (i == index & i == 0) {
node = newNode;
newNode.next = temp;
size++;
return;
} else if (i == index) {
last.next = newNode;
newNode.next = temp;
size++;
return;
}
last = temp;
temp = temp.next;
i ++;
}
last.next = newNode;
size++;
}
/**
* 取出指定索引元素
* @param index
* @return
*/
public T getIndex(int index) {
if (isEmpty()) {
throw new RuntimeException("链表为空");
} else if (index >= 0 & index < size) {
if (index == 0) {
return getHead();
}
int i = 0;
Node temp = node;
Node last = node;
while (i < size) {
if (i == index) {
Node next = temp.next;
last.next = next;
size --;
return temp.data;
}
i++;
last = temp;
temp = temp.next;
}
} else {
throw new RuntimeException("索引越界");
}
return null;
}
/**
* 取出头部元素
*/
public T getHead() {
if (isEmpty()) {
throw new RuntimeException("链表为空");
} else {
Node head = node;
node = head.next;
size--;
return head.data;
}
}
/**
* 删除指定索引的元素
* @param index
*/
public void delete(int index) {
this.getIndex(index);
}
/**
* 判断是否位空
* @return
*/
public Boolean isEmpty() {
return size < 1;
}
/**
* 返回链表元素总个数
* @return
*/
public int size() {
return this.size;
}
/**
* 修改指定索引的元素值
* @param data
* @param index
*/
public void update(T data,int index) {
if (isEmpty()) {
throw new RuntimeException("链表为空");
} else if (index >= 0 & index < size) {
int i = 0;
Node temp = node;
while (i < size) {
if (i == index) {
temp.data = data;
return;
}
i++;
temp = temp.next;
}
} else {
throw new RuntimeException("索引越界");
}
}
/**
* 打印链表元素
*/
public void show() {
if (node == null) {
System.out.println("链表为空");
return;
}
Node temp = node;
int i = 0;
System.out.println("index:"+ i +" value:" + temp.data);
while (temp.next != null) {
temp = temp.next;
i++;
System.out.println("index:"+ i +" value:" + temp.data);
}
}
/**
* 反转链表
*/
public void reversalList() {
Object[] nodes = new Object[size];
int i = 0;
Node temp = node;
while (temp.next != null) {
nodes[i] = temp;
i++;
temp = temp.next;
}
nodes[size-1] = temp;
node = temp;
for (int y = nodes.length - 2; y >= 0; y--) {
Node node = (Node)nodes[y];
node.next = null;
temp.next = node;
temp = node;
}
}
class Node{
private T data;
private Node next;
private Node(T data){
this.data = data;
}
}
}
import java.util.Scanner;
/**
* @author Haidnor
* @creat 2020-03-13-11:39
*/
public class LinkedListTEST {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList();
Scanner scanner = new Scanner(System.in);
Boolean loop = true;
int index;
int data;
while (loop) {
System.out.println("
s(show): 查看链表");
System.out.println("a(append): 在链表尾部加入数据");
System.out.println("ai(appendInIndex): 在链表指定索引位置插入数据");
System.out.println("di(delete): 删除指定索引位置的数据");
System.out.println("u(update): 修改指定索引位置的数据");
System.out.println("gi(getIndex): 取出指定索引位置的数据");
System.out.println("gh(getHead): 取出链表头数据");
System.out.println("r(reversalList): 反转链表");
System.out.println("size(size): 显示链表大小");
System.out.println("e(exit): 退出程序");
System.out.print(">>> 请输入指令:");
String command = scanner.next();
switch (command) {
case "s" :
list.show();
break;
case "a" :
System.out.print(">>>输入要插入的值:");
data = scanner.nextInt();
list.append(data);
break;
case "ai" :
System.out.print(">>>输入要插入的值:");
data = scanner.nextInt();
System.out.print("
>>>输入索引位置:");
index = scanner.nextInt();
try {
list.appendInIndex(data,index);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "di" :
System.out.print(">>>输入索引位置:");
index = scanner.nextInt();
try {
list.delete(index);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "u" :
System.out.print(">>>输入值:");
data = scanner.nextInt();
System.out.print("
>>>输入索引位置:");
index = scanner.nextInt();
try {
list.update(data,index);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "gi" :
System.out.print("
>>>输入索引位置:");
index = scanner.nextInt();
try {
data = list.getIndex(index);
System.out.println("data:" + data);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "gh" :
try {
data = list.getHead();
System.out.println("data:" + data);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "r" :
try {
list.reversalList();
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "size" :
data = list.size();
System.out.println("size:" + data);
break;
case "e" :
scanner.close();
loop = false;
break;
default :
break;
}
}
System.out.println("已终止程序!");
}
}