package linerList;
public class LinkedList<T> implements ListInterface<T> {
Node head;
private int length;
public LinkedList(){
head = new Node();
this.length = 0;
}
@Override
public void Init(int initialSize) {
head.next = null;
}
@Override
public int length() {
return this.length;
}
@Override
public boolean isEmpty() {
return this.length == 0;
}
@Override
public int ElemIndex(T t) {//顺序表中是通过
//先获得头结点,从头结点开始遍历,找出地址相匹配的节点。
Node temp = head;//获得首元节点,也可以不要后面的next。
int index = 0;
//循环整个链表,最后一个节点的后一个next = null;
while (temp.next != null) {
if (temp.data.equals(t)) {//如果链表中的值,等于需要判断的值的时候,就返回索引。
return index;
}
index++;
temp = temp.next;//可以看成指针向后移动,也可以理解成重新赋值。java里面就是用引用来代替指针的。
}
return -1;//没有找到返回-1.
}
//查找出元素第一次出现的位置。
@Override
public T getElem(int index) throws Exception {//可以理解成获得第index个的元素的值。
Node temp = head.next;
if (index < 0 || index > length) {
throw new Exception("index 越界异常");
}
//因为我是没有索引的,所以我只能从头开始一个个找到index的位置。
for (int i = 0; i < index; i++) {
temp = temp.next;//一开始是一地个节点,然后向后移动index次,赋值之后就变成了index处的值。
}
return (T)temp;//temp已经发生变化了,这个时候就能直接转型了。
}
@Override
public void add(int index, T t) throws Exception {
if(index < 0|| index > length ){
throw new Exception("index.越界异常");
}
//首先要找到i节点的前一个位置。将这个位置上的节点指向新创建的节点,然后行的接待指向后一个节点。
Node temp = head;
for (int i = 0; i < index - 1; i++) {
temp = temp.next;//此时已经获取到前一个节点了
}
Node nextNode = temp.next;//找到当前的index处。
Node<T> newNode = new Node<T>();
newNode.data = t;//好几种构造器
temp.next = newNode;
newNode.next = nextNode;
length++;
}
@Override
public void add(T t) throws Exception {
add(length,t);//直接调用上面的方法,还可以直接遍历到最后一个直接在后面添加。
}
@Override
public void delete(int index) throws Exception {
if (index < 0 || index > length) {
throw new Exception("index 越界异常");
}
Node pre = head.next;
for (int i = 0; i < index - 1; i++) {
pre = pre.next;//此时已经获取到前一个节点了
}
Node current = pre.next;
Node nextNode = current.next;
pre.next = nextNode;
//这里也可以写成pre.next = current.next;
length--;
}
@Override
public void set(int index, T t) throws Exception {
if (index < 0 || index > length) {
throw new Exception("index 越界异常");
}
Node temp = head.next;
for (int i = 0;i < index ;i++){
temp = temp.next;
}
temp.data = t;//将index处的值变换成t.其他变量不变。
}
//编写一个能显示链表内容的
public String toString(){
String time = "";
Node temp = head.next;
while(temp != null){
time += temp.data + "";//转换成字符串
temp = temp.next;
}
return time;
}
}
接口部分:
package linerList;
public interface ListInterface<T> {
void Init(int initialSize);//初始化线性表
int length();
boolean isEmpty();//是否为空
int ElemIndex(T t);//找到编号
T getElem(int index) throws Exception;
void add(int index, T t) throws Exception;//根据索引插入
void add(T t) throws Exception;//在尾部插入
void delete(int index) throws Exception;
void set(int index, T t) throws Exception;
String toString();
}
节点部分:
package linerList;
public class Node<T> {
public T data;
public Node next;
public Node() {
}
public Node(T data) {
this.data = data;
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
终于理解了里面的:
这篇文章详细介绍了如何使用Java实现LinkedList类,包括初始化、长度操作、查找元素、插入、删除和设置元素等方法,以及ListInterface接口的实现。重点讲解了节点(Node)和头结点(head)的使用以及链表的操作原理。
1247

被折叠的 条评论
为什么被折叠?



