单向链表LinkedSinglyList
LinkedSinglyList就是线性结构链式存储方式的具体实现,称为单链表
package shixianClass02;
import org.omg.CORBA.NO_IMPLEMENT;
import shixianClass.ArrayList;
import shujujiegou_interface.List;
import java.util.Comparator;
import java.util.Iterator;
//单向链表
public class LinkedSinglyList<E> implements List<E> {
//定义出一个结点 内部类
private class Node{
E data; //数据域 用来存储数据的
Node next; // 指针域 用于存储下一个结点对象的地址
public Node(){
this(null,null);
}
public Node(E data){
this(data,null);
}
public Node(E data,Node next){
this.data=data;
this.next=next;
}
@Override
public String toString() {
return data.toString();
}
}
private Node head;//是链表当中的头指针 指向第一个结点对象
private Node tail; //是链表当中的尾指针 指向最后一个结点对象
private int size;//是链表当中有效元素的个数
public LinkedSinglyList(){
head=null;
tail=null;
size=0;
}
public LinkedSinglyList(E [] arr){
for(E e:arr){
add(e);
}
}
//默认在链表的尾部添加元素
@Override
public void add(E element) {
add(size,element);
}
//在链表中指定角标位置添加元素
@Override
public void add(int index, E element) {
if(index<0||index>size){
throw new IndexOutOfBoundsException("add index out of bounds");
}
//创建出一个新的结点对象
Node node=new Node(element);
//链表为空
if(isEmpty()){
head=node;
tail=node;
} else if (index==0) {
//在表头添加
node.next=head;
head=node;
} else if (index==size) {
//在表尾添加
tail.next=node;
tail=node;
}else {
//在表中间添加
Node p=head;
for (int i=0;i<index-1;i++){
p=p.next;
}
node.next=p.next;
p.next=node;
}
size++;
}
//删除链表中指定元素
@Override
public void remove(E element) {
int index=indexOf(element);
if(index!=-1){
remove(index);
}
}
//删除链表中指定角标处的元素
@Override
public E remove(int index) {
if(index<0||index>=size){
throw new IndexOutOfBoundsException("remove index out of bounds");
}
E ret=null;
//1、当链表只剩一个元素
if(size==1){
ret=head.data;
head=null;
tail=null;
}else if(index==0){
//2、删除表头
Node del=head;
ret=del.data;
head=del.next;
del.next=null;
}else if (index==size-1){
//3、删除表尾
Node p=head;
while (p.next!=tail){
p=p.next;
}
ret=tail.data;
p.next=null;
tail=p;
}else {
//4、删除中间某一个元素
Node p=head;
for(int i=0;i<index-1;i++){
p=p.next;
}
Node del=p.next;
ret=del.data;
p.next=del.next;
del.next=null;
}
size--;
return ret;
}
//获取链表中指定角标处的元素
@Override
public E get(int index) {
if(index<0||index>=size){
throw new ArrayIndexOutOfBoundsException("get index out of bounds");
}
//1、获取头
if(index==0){
return head.data;
}else if (index==size-1){
//获取尾巴
return tail.data;
}else {
//获取中间
Node p=head;
for (int i=0;i<index;i++){
p=p.next;
}
return p.data;
}
}
//修改链表中指定角标处的元素
@Override
public E set(int index, E element) {
if(index<0||index>=size){
throw new ArrayIndexOutOfBoundsException("set index out of bounds");
}
E ret=null;
if(index==0){
//修改头
ret=head.data;
head.data=element;
}else if (index==size-1){
//修改尾
ret=tail.data;
tail.data=element;
}else {
//修改中间
Node p=head;
for (int i=0;i<index;i++){
p=p.next;
}
ret=p.data;
p.data=element;
}
return ret;
}
//链表中有效元素的个数
@Override
public int size() {
return size;
}
//查找元素在链表中第一次出现的角标
@Override
public int indexOf(E element) {
Node p=head;
int index=0;
while (!p.data.equals(element)){
p=p.next;
index++;
if(p==null){
return -1;
}
}
return index;
}
//在链表中判断是否包含element
@Override
public boolean contains(E element) {
return indexOf(element)!=-1;
}
@Override
public boolean isEmpty() {
return size==0 && head==null && tail==null;
}
//清空
@Override
public void clear() {
head=null;
tail=null;
size=0;
}
@Override
public void sort(Comparator<E> c) {
if(c==null){
throw new IllegalArgumentException("comparator can not be null");
}
/*int j=0;
E e=null;
for(int i=1;i<size;i++){
e=get(i);
//j-1>=0 j>=1 j>0
for(j=i;j>0&&c.compare(get(j-1),e)>0;j--){//O(n^2)
set(j,get(j-1));//o(n^3)
}
set(j,e);
}*/
//改进 选择排序
if(size==0||size==1){
return;
}
Node nodeA=head;
Node nodeB=nodeA.next;
while (true){
while (true){
if(c.compare(nodeA.data,nodeB.data)>0){
swap(nodeA,nodeB);
}
if (nodeB==tail){
break;
}
nodeB=nodeB.next;
}
if(nodeA.next==tail){
break;
}
nodeA=nodeA.next;
nodeB=nodeA.next;
}
}
//两个结点排序
private void swap(Node nodeA, Node nodeB) {
E temp=nodeA.data;
nodeA.data=nodeB.data;
nodeB.data=temp;
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
if (fromIndex<0){
throw new IndexOutOfBoundsException("fromIndex must>0");
}
if(toIndex>=size){
throw new IndexOutOfBoundsException("toIndex must>size");
}
if (fromIndex>toIndex){
throw new IndexOutOfBoundsException("fromIndex must<=toIndex");
}
LinkedSinglyList<E> subList=new LinkedSinglyList<>();
/*for (int i=fromIndex;i<=toIndex;i++){
subList.add(get(i));//默认表尾添加
}*/
//改进
Node nodeA=head;
for (int i=0;i<fromIndex;i++){
nodeA=nodeA.next;
}
Node nodeB=head;
for (int i=0;i<toIndex;i++){
nodeB=nodeB.next;
}
Node p=nodeA;
while (true){
subList.add(p.data);
if (p==nodeB){
break;
}
p=p.next;
}
return subList;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder(String.format("LinkedSinglyList:%d[",size));
if (isEmpty()){
sb.append(']');
}else {
Node p=head;
while (true){
sb.append(p.data);
if (p==tail){
sb.append(']');
break;
}
sb.append(',');
p=p.next;
}
}
return sb.toString();
}
@Override
public Iterator<E> iterator() {
return new LinkedListIterator();
}
class LinkedListIterator implements Iterator<E>{
private Node cur=head;
@Override
public boolean hasNext() {
return cur!=null;
}
@Override
public E next() {
E ret=cur.data;
cur=cur.next;
return ret;
}
}
}
测试类
package ceshi;
import shixianClass02.LinkedSinglyList;
import java.util.Comparator;
import java.util.Iterator;
public class TestLinkedSinglyList {
public static void main(String[] args) {
LinkedSinglyList<Integer> list=new LinkedSinglyList<>();
System.out.println(list);
for (int i=1;i<=10;i++){
list.add(i);
}
System.out.println(list);
list.add(0,666);
list.add(0,777);
list.add(0,888);
list.add(5,999);
System.out.println(list);
int len=list.size();
for (int i=0;i<len;i++){
list.remove(0);
}
System.out.println(list);
list.add(10);
list.add(9);
list.add(8);
list.add(7);
list.add(6);
System.out.println(list);
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println(list);
System.out.println(list.subList(0,2));
Iterator<Integer> it=list.iterator();
while (it.hasNext()){
System.out.print(it.next()+" ");
}
}
}
执行结果
LinkedSinglyList:0[]
LinkedSinglyList:10[1,2,3,4,5,6,7,8,9,10]
LinkedSinglyList:14[888,777,666,1,2,999,3,4,5,6,7,8,9,10]
LinkedSinglyList:0[]
LinkedSinglyList:5[10,9,8,7,6]
LinkedSinglyList:5[6,7,8,9,10]
LinkedSinglyList:3[6,7,8]
6 7 8 9 10