写在前面:熟能生巧,古人诚不欺我!
- 结点类
package list;
/**
* Create by IDEA
* User: zhangqi
* Date: 2019/2/26
* Desc: 单链表结点
*/
public class LinkNode {
//指针域
LinkNode next;
//数据域
Object data;
/**
* 初始化头结点
* @param head
*/
public LinkNode(LinkNode head) {
this.next = head;
}
/**
* 初始化元素结点
* @param head
* @param data
*/
public LinkNode(LinkNode head, Object data) {
this.next = head;
this.data = data;
}
public LinkNode getNext() {
return next;
}
public void setNext(LinkNode next) {
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
@Override
public String toString() {
return data.toString();
}
}
- 单链表抽象数据类型
package list;
/**
* Create by IDEA
* User: zhangqi
* Date: 2019/2/26
* Desc: 单链表抽象数据类型
*/
public interface SimpleLink {
/**
* 获取链表长度
* @return
*/
int size();
/**
* 判断链表是否为空
* @return
*/
boolean isEmpty();
/**
* 获取结点数据
* @param index
* @return
*/
Object getNodedata(int index) throws Exception;
/**
* 删掉结点
* @param index
*/
void deleteNode(int index) throws Exception;
/**
* 插入结点
* @param index
*/
void insertNode(int index, Object data) throws Exception;
}
- 单链表实现类
package list;
/**
* Create by IDEA
* User: zhangqi
* Date: 2019/2/26
* Desc: 单链表实现类
*/
public class SimpleLinkImpl implements SimpleLink {
//链表长度
int size;
//头指针
LinkNode head;
//当前结点
LinkNode current;
/** 初始化一个空链表,此时当前结点就是头结点,链表长度为0 */
public SimpleLinkImpl() {
/**
* 此时当前结点和头结点重合,不能使用以下方式进行初始化
* this.head = new LinkNode(null); this.current = new LinkNode(null);
*/
this.head = current = new LinkNode(null);
this.size = 0;
}
/**
* 获取要操作结点位置,首元素结点下标为0,则假设头结点下标为-1
* @param index
* @throws Exception
*/
public void location(int index) throws Exception {
/** 大于链表长度或者在头结点之前 */
if (index < -1 || index > size-1) throw new Exception("输入索引有误");
/** 表示插入或者删除第一个元素结点 */
if(index == -1) return;
current = head.next;
int j = 0;
while (current != null && j < index) {
current = current.next;
j++;
}
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Object getNodedata(int index) throws Exception {
location(index);
return current.getData();
}
@Override
public void deleteNode(int index) throws Exception {
if (isEmpty()) {
throw new Exception("链表为空,无法删除!");
}
if (index < 0 || index > size) {
throw new Exception("参数有误!");
}
/** 定位到要操作结点的前一个结点 */
location(index-1);
current.setNext(current.next.next);
size--;
}
@Override
public void insertNode(int index, Object data) throws Exception {
if (index < 0 || index > size) {
throw new Exception("参数有误!");
}
/** 定位到要操作结点的前一个结点 */
location(index-1);
current.setNext(new LinkNode(current.next,data));
size++;
}
}
- 测试单链表
package list;
import java.util.Random;
/**
* Create by IDEA
* User: zhangqi
* Date: 2019/2/26
* Desc:
*/
public class SimpleLinkTest {
public static void main(String[] args) throws Exception {
SimpleLinkImpl link = new SimpleLinkImpl();
for(int i=0;i<10;i++) {
Random random = new Random();
int num = random.nextInt(100);
link.insertNode(i,num);
System.out.print(num + " ");
}
System.out.println("SIZE: "+link.size());
link.deleteNode(4);
for(int j=0;j<link.size();j++) {
System.out.print(link.getNodedata(j) + " ");
}
System.out.println("SIZE: "+link.size());
}
}
- 测试结果

https://www.cnblogs.com/smyhvae/p/4761593.html,感谢这位博主,感谢!