首先建立一个节点模型
public class LinkNode {
/** 地址域 */
public LinkNode next;
/** 数据域 */
public int data;
}
其次建立一个链表模型,包含对链表的操作
public class MyLinked {
/** 模拟头结点 */
private static LinkNode head;
/** 记录链表的长度 */
public static int size;
/**
* 初始化一个链表
* @return
*/
public static LinkNode init() {
head = new LinkNode();
head.next=null;
return head;
}
/**
* 头插法创建一个长度为n的链表
* @param n
* @return
*/
public static LinkNode createLinkedH(int n) {
/** 获得头结点 */
LinkNode head = init();
size = n;
for (int i = 0; i < n; i++) {
LinkNode node = new LinkNode();
node.data = i+1;
node.next = head.next;
head.next = node;
}
return head;
}
/**
* 尾插法创建一个长度为n的链表
* @param n
* @return
*/
public static LinkNode createLinkedR(int n) {
/** 获得头结点 */
LinkNode head = init();
LinkNode rear = head;
size = n;
for (int i = 0; i < n; i++) {
LinkNode node = new LinkNode();
node.data = i+1;
node.next = null;
rear.next = node;
rear = node;
}
return head;
}
/**
* 查找某个值在链表中的位置
* @param num
* @return
*/
public static int searchData(int num, LinkNode link) {
head = link;
LinkNode p = head.next;
int i = 1;
while (p != null) {
if (p.data != num) {
p = p.next;
i++;
} else {
return i;
}
}
return -1;
}
/**
* 获得链表中第n个位置上的值
* @param n
* @param linkNode
* @return
*/
public static int getValue(int n, LinkNode linkNode) {
head = linkNode;
LinkNode p = head.next;
int i= 1;
while (p != null && i < n) {
i++;
p = p.next;
}
if (p == null || i > n) {
return -1;
}
return p.data;
}
/**
* 向链表第n的位置插入一个值,成功返回链表的长度,失败返回-1
* @param n
* @param value
* @param linkNode
* @return
*/
public static int insertData(int n, int value, LinkNode linkNode) {
head = linkNode;
/** 头结点 */
LinkNode p = head;
int i = 0;
while ( p != null && i < n-1) {
i++;
p = p.next;
}
if (p == null || i > n-1) {
return -1;
}
LinkNode node = new LinkNode();
node.data = value;
node.next = p.next;
p.next = node;
size++;
return size;
}
/**
* 从链表中删除一个节点
* @param n
* @param linkNode
* @return
*/
public static int deleteData(int n, LinkNode linkNode) {
head = linkNode;
LinkNode p = head;
int i = 0;
while (p != null && i < n-1) {
p = p.next;
i++;
}
if (p == null || i > n-1) {
return -1;
}
LinkNode node = p.next;
p.next = p.next.next;
node = null;
size--;
return size;
}
}
最终测试
public class Test {
public static void main(String[] args) {
m7();
}
private static void m7() {
LinkNode linkedR = MyLinked.createLinkedR(6);
int i = MyLinked.deleteData(6, linkedR);
System.out.println(i);
}
private static void m6() {
LinkNode linkedR = MyLinked.createLinkedR(6);
int i = MyLinked.insertData(8, 66, linkedR);
System.out.println(i);
}
private static void m5() {
LinkNode linkedR = MyLinked.createLinkedR(9);
int value = MyLinked.getValue(10, linkedR);
System.out.println(value);
}
private static void m4() {
LinkNode linkedR = MyLinked.createLinkedR(9);
int data = MyLinked.searchData(5, linkedR);
System.out.println(data);
}
private static void m3() {
LinkNode head = MyLinked.createLinkedR(7);
LinkNode p = head.next;
while (p != null) {
System.out.println(p.data);
p = p.next;
}
}
private static void m2() {
LinkNode linkedH = MyLinked.createLinkedH(5);
LinkNode p = linkedH.next;
while (p != null) {
System.out.println(p.data);
p = p.next;
}
}
private static void m1() {
LinkNode init = MyLinked.init();
System.out.println(init.data);
System.out.println(init.next);
}
}