学习数据结构很重要!!!所以先上代码为敬,给自己一个flag,在一个月内过完一遍数据结构,并将基础操作类的代码上传到博客上。
这是一份还是不太健壮的代码,但是对于刷题,工作中基本的需求是满足的。
/*
* 定义一个链表节点的类
*/
public class ListNode {
public ListNode next;
public char value;
public ListNode(char value) {
this.value = value;
this.next = null;
}
}
/*
*定义一个链表操作的接口
*/
public class operateList {
private static ListNode head;
/*
* 减少类的实例化次数
*/
private operateList() {}
/*
* 创建链表
*/
public static ListNode createList(char value) {
head = new ListNode(value);
head.next = null;
return head;
}
//插入链表节点(前向插入)
public static ListNode forwardInsertListNode(char value) {
ListNode newListNode = new ListNode(value);
newListNode.next = head;
head = newListNode;
return head;
}
//插入链表节点(后向插入)
public static ListNode backwardInsertListNode(char value) {
ListNode transitionPointer = head;
while(transitionPointer.next!=null) {
transitionPointer = transitionPointer.next;
}
ListNode newListNode = new ListNode(value);
transitionPointer.next = newListNode;
transitionPointer = transitionPointer.next;
newListNode.next = null;
return head;
}
/*
* 打印链表节点信息
*/
public static void printList() {
ListNode readPointer = head;
while (readPointer!=null) {
System.out.println(readPointer.value);
readPointer = readPointer.next;
}
}
/*
* 删除指定数据的节点
*/
public static void deleteListNode(char value) {
ListNode deletePointer = head.next;
ListNode vListNode = head;
while(deletePointer.next.next!=null) {
while(deletePointer.value!=value&&vListNode.value!=value) {
vListNode = deletePointer;
deletePointer = deletePointer.next;
}
vListNode.next = deletePointer.next;
ListNode temp = deletePointer.next;
deletePointer.next = null;
deletePointer = temp;
}
}
/*
* 函数重载。删除指定位置的节点
*/
public static void deleteListNode(ListNode pointer) {
ListNode deletePointer = head;
while(deletePointer.next!=pointer) {
deletePointer = deletePointer.next;
}
deletePointer.next = pointer.next;
pointer.next = null;
}
public static void quaryListNode(char value) {
ListNode quaryPointer = head;
int bit = 0;
while(quaryPointer.value!=value&&quaryPointer.next!=null) {
quaryPointer = quaryPointer.next;
bit++;
}
System.out.println(bit);
}
/*
* @param data 插入的字符串
* @param model 选择链表的初始化模式
* 0:前向插入
* 1:后向插入
*/
public static void fillList(String data,int model) {
char[] dataArray = data.toCharArray();
switch(model) {
case 0:
for(int i=0;i<dataArray.length;i++) {
forwardInsertListNode(dataArray[i]);
}
break;
case 1:
for(int i=0;i<dataArray.length;i++) {
backwardInsertListNode(dataArray[i]);
}
break;
}
}
/*
* 指定位置插入节点
*@param where 插入的位置
*@param data 插入的数据
*/
public static void insertListNode(int where,char data) {
ListNode insertNode = new ListNode(data);
ListNode pre = head;
for(int i=0;i<where-1&&null!=pre.next;pre=pre.next,i++);
insertNode.next = pre.next;
pre.next = insertNode;
}
/*
* @function 反转链表
*/
public static void reserveList() {
ListNode pre = head;
ListNode current = head.next;
ListNode next = null;
while(current!=null) {
next = current.next;
current.next = pre;
pre = current;
current = next;
}
head.next = null;
head = pre;
}
}
有错误请在评论区告知,谢谢各位大佬的围观l