**
线性表的链式实现
** 为LinkListWithSize类增加: 1、实现就地逆置函数: public void reverse() 2、public void removeRange(int from, int to) 删除位置[from, to]范围的数据 3、public int lastInsdex(AnyType x) 返回x最后一次出现的位置
public void reverse() {
Node<T> p = head;
for (int left = 0, right = size()-1;left<right;right--,left++){
Node<T> q= head;
for (int i = 0; i < right; i++)
q = q.next;
T temp = p.data;
p.data = q.data;
q.data = temp;
p = p.next;
}
}
public void removeRange(int from, int to) {
if (from>to)
return ;
checkElementIndex(from);
checkElementIndex(to);
Node<T> p = head;
for (int i = 0; i < from; i++)
p = p.next;
for(int i = from;i<=to;i++)
remove(from);
}
public int lastInsdex(T x) {
int number = -1;
int index = -1;
for (Node<T> p = head; p != null; p = p.next) {
index++;
if (p.data.equals(x))
number = index;
}
return number;
}
不用remove的实现方法
public void removeRange(int from, int to) {
checkElementIndex(from);
checkElementIndex(to);
if (from>to)
return;
if (from == 0)
for (; from <= to; from++)
head = head.next;
else{
Node<T> p = head;
for (int i = 0; i < from-1; i++)
p = p.next;
Node<T> q = p;
for (int i = from;i <= to;i++)
q = q.next;
p.next=q.next;
}
}
类内原有代码
public void removeRange(int from, int to) {
if (from>to)
return ;
checkElementIndex(from);
checkElementIndex(to);
Node<T> p = head;
for (int i = 0; i < from; i++)
p = p.next;
for(int i = from;i<=to;i++)
remove(from);
}
public int lastInsdex(T x) {
int number = -1;
int index = -1;
for (Node<T> p = head; p != null; p = p.next) {
index++;
if (p.data.equals(x))
number = index;
}
return number;
}
private static class Node<T> {
T data;
Node<T> next;
Node(T data, Node<T> next) {
this.data = data;
this.next = next;
}
}
private Node<T> head;
private int size;
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public int indexOf(T x) {
int index = 0;
for (Node<T> p = head; p != null; p = p.next) {
if (p.data.equals(x))
return index;
++index;
}
return -1;
}
private void checkPositionIndex(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException(String.valueOf(index));
}
private void checkElementIndex(int index) {
if (index < 0 || index > size - 1)
throw new IndexOutOfBoundsException(String.valueOf(index));
}
public T get(int index) {
checkElementIndex(index);
Node<T> p = head;
for (int i = 0; i < index; i++)
p = p.next;
return p.data;
}
public void add(int index, T x) {
checkPositionIndex(index);
if (index == 0) {
head = new Node<>(x, head);
} else {
Node<T> p = head;
for (int i = 0; i < index - 1; i++)
p = p.next;
p.next = new Node<>(x, p.next);
}
++size;
}
public T remove(int index) {
checkElementIndex(index);
Node<T> p = head;
if (index == 0) {
head = head.next;
} else {
for (int i = 0; i < index - 1; i++)
p = p.next;
Node<T> q = p;
p = p.next;
q.next = p.next;
}
T value = p.data;
p.data = null;
p.next = null;
--size;
return value;
}
public void clear() {
while (head != null) {
Node<T> q = head;
head = head.next;
q.data = null;
q.next = null;
}
size = 0;
}
public String toString() {
StringBuilder str = new StringBuilder(getClass().getName() + ":");
for (T elem : this)
str.append(elem).append(" ");
return str + "\n";
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof LinkedListWithSize<?> rhd) {
if (this.size != rhd.size)
return false;
for (Node<?> p = head, q = rhd.head; p != null; p = p.next, q = q.next) {
if (!p.data.equals(q.data))
return false;
}
return true;
} else
return false;
}
public int hashCode() {
int result = Integer.valueOf(size).hashCode();
for (Node<T> p = head; p != null; p = p.next)
result = result * 31 + p.data.hashCode();
return result;
}
@SuppressWarnings("unchecked")
private LinkedListWithSize<T> superClone() {
try {
return (LinkedListWithSize<T>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
public Object clone() {
LinkedListWithSize<T> v = superClone();
if (v.head == null) {
} else {
v.head = new Node<>(head.data, null);
for (Node<T> p = head.next, q = v.head; p != null; p = p.next, q = q.next)
q.next = new Node<>(p.data, null);
}
return v;
}
public Iterator<T> iterator() {
return new Itr();
}
private class Itr implements Iterator<T> {
private Node<T> curPos;
public Itr() {
curPos = head;
}
public boolean hasNext() {
return curPos != null;
}
public T next() {
T data = curPos.data;
curPos = curPos.next;
return data;
}
}
测试方法:
public static void main(String[] args) {
LinkedListWithSize<Integer> lk = new LinkedListWithSize<>();
lk.add(0, 1);
lk.add(0, 2);
lk.add(0, 3);
lk.add(0, 4);
lk.reverse();
System.out.println(lk);
System.out.println(lk.lastInsdex(2));
System.out.println(lk.lastInsdex(4));
System.out.println(lk.lastInsdex(5));
lk.removeRange(0,1);
System.out.println(lk);
}
|