8. 实现LinkedList所有API

package collection;
import java.util.LinkedList;

public class LInkedListPractice {
	public static void main(String[] args) throws IllegalAccessException {
		LinkedListDemo list = new LinkedListDemo();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("e");
		list.add(3,"d");
//		list.set(2, "g");
//		System.out.println(list.isEmpty());
//		System.out.println(list.get(3));
//		System.out.println(list.contains("d"));
//		list.clear();
//		System.out.println(list.size());
//		list.remove(2);
//		list.remove("a");
//		int index = list.indexOf("e");
//		System.out.println(index);
		
		LinkedListDemo lld = list.subList(1, 3);
		System.out.println(lld);
		System.out.println(list);
	}
}

class LinkedListDemo{
	//定义长度
	private int size = 0;
	//头结点
	private Node first;
	//尾结点
	private Node last;
	
	//定义Node内部类
	private class Node{
		//保存
		String data;
		//上一个结点
		Node prev;
		//下一个结点
		Node next;
		public Node(String data,Node prev,Node next){
			this.data = data;
			this.prev = prev;
			this.next = next;
		}
	}
	
	//添加元素方法
	public void add(String str) {
		//创建新结点
		Node node = new Node(str,null,null);
		//如果链表中没有结点
		if(size == 0){
			this.first = node;
		}else{
			//添加到链表尾部
			this.last.next = node;
			node.prev = this.last;
		}
		this.last = node;
		//长度+1
		size++;
	}
	
	//插入元素方法
	public void add(int index,String str){
		// 判断越界
		this.out(index);
		// 插入到尾部
		if (index == size){
			add(str);
			return;
		}
		// 创建新结点
		Node node = new Node(str,null,null);
		// 插入到头部
		if (index == 0){
			// 1.将first结点的prev设置为新结点
			this.first.prev = node;
			// 2.将新结点的next设置为当前的first
			node.next = this.first;
			// 3.将first设置为新结点
			this.first = node;
		}else{// 插入到中间
			// 1.寻找插入位置的结点
			Node no = getNode(index);
			// 2.将插入位置结点的prev结点的next设置为新结点
			no.prev.next = node;
			// 3.将新结点的prev设置为插入位置结点的上一个结点
			node.prev = no.prev;
			// 4.将新结点的next设置为插入位置的结点
			node.next = no;
			// 5.将插入位置的prev设置为新结点
			no.prev = node;
		}
		// 4.长度加1
		size++;
	}
	
	//删除指定索引出的结点
	public void remove(int index){
		//判断越界
		this.out(index);
		if(index == 0){
			//先将头结点设置为当前头结点下一位
			this.first = this.first.next;
			//当前头结点的prev设置为null
			this.first.prev = null;
		}else if(index == size -1){//删除尾结点
			//先将尾结点设置为当前尾结点的上一位
			this.last = this.last.prev;
			//当前尾结点的next设置为null
			this.last.next = null;
		}else{
			//1.寻找到要被删除的结点
			Node no = getNode(index);
			//2.将要删除的结点的上一个结点的next指向要被删除的结点的next
			no.prev.next = no.next;
			//3.将要被删除的结点的下一个结点的prev指向要被删除的结点的上一个
			no.next.prev = no.prev;
		}
		size--;
	}
	
	//删除指定元素的结点
	public void remove(String str){
		//寻找str对应的索引
		int index = indexOf(str);
		if(index != -1){
			remove(index);
		}
	}
	
	//如果没有找到对应的元素,那么返回-1
	public int indexOf(String str){
		//获取头结点
		Node node = this.first;
		for (int i = 0; i < size; i++) {
			//比较节点中元素是否和str相等
			if(node.data == str && node.data != null){
				return i;
			}
			node = node.next;
		}
		return -1;
	}
	
	//获取长度
	public int size(){
		return size;
	}
	
	//清空链表
	public void clear(){
		this.first = this.last = null;
		size = 0;
	}
	
	//判断是否包含
	public boolean contains(String str){
		return indexOf(str) != -1;
	}
	
	//get方法
	public String get(int index){
		//判断越界
		this.out(index);
		return getNode(index).data;
	}
	
	//判断是否为空
	public boolean isEmpty(){
		return size == 0;
	}
	
	//
	public void set (int index,String str){
		//判断越界
		this.out(index);
		getNode(index).data = str;
	}
	
	//截取子列表
	public LinkedListDemo subList(int fromIndex,int toIndex) throws IllegalAccessException{
		//判断越界
		this.out(fromIndex);
		this.out(toIndex);
		//判断
		if(toIndex < fromIndex){
			throw new IllegalAccessException();
		}
		//创建新链表
		LinkedListDemo sublist = new LinkedListDemo();
		//1.根据fromIndex寻找对应的结点
		Node no1 = getNode(fromIndex);
		//2.
//		for (int i = fromIndex; i < toIndex; i++) {
//			sublist.add(no1.data);
//			no1 = no1.next;
//		}
		Node no2 = getNode(toIndex);
		sublist.first = no1.prev;
		no1.prev = null;
		sublist.last = no2.prev.next;
		no2.prev.next = null;
		return sublist;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		//获取头结点
		Node node = this.first;
		for (int i = 0; i < size; i++) {
			if(i == size -1){
				sb.append(node.data);
			}else{
				sb.append(node.data).append(",");
			}
			node = node.next;
		}
		sb.append("]");
		return sb.toString();
	}
	
	private Node getNode(int index){
		Node no = this.first;
		for (int i = 0; i < index; i++) {
			no = no.next;
		}
		return no;
	}
	
	private void out(int index){
		if(index > size || index < 0){
			throw new ArrayIndexOutOfBoundsException();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼鱼大头鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值