import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; /* 双向链表:相当于单向链表,在每个节点除了记录下一个节点地址,还会记录上一个节点地址 分析: 1、先要有一个链表的类-------MyLinkedList 2、链表内有属性---节点 3、行为---增删改查 4、测试 */ public class Demo1 { public static void main(String[] args) { LinkedList<String> linkedList = new LinkedList<>(); MyLinkedList<String> myLinkedList = new MyLinkedList(); myLinkedList.add("java"); myLinkedList.add("java"); myLinkedList.add("c++"); myLinkedList.add("html"); myLinkedList.add("c"); myLinkedList.add("c"); myLinkedList.add("c++"); //myLinkedList.add(2,"mysql"); //String element = myLinkedList.delWithIndex(1); //System.out.println("删除的元素为:"+element); //myLinkedList.delWithElement("c"); //System.out.println(myLinkedList.select("c")); //myLinkedList.clean(); System.out.println(myLinkedList.set(2,"python")); System.out.println(myLinkedList); } } /** * * 创建链表类---描述双向链表 * 使用链表数据结构描述集合中的Linked List(模拟) */ class MyLinkedList<E>{ //创建节点类----创建内部类,链表中的元素以节点形式存储数据 class Node{ //数据域 E element; //指针域--保存上一个节点地址 Node pervious; //指针域--保存下一个节点地址 Node next; public Node(E element){ this.element = element; } } //创建节点 //创建首节点 private Node first; //创建尾节点 private Node last; //表示节点个数 private int count; /** * 通过指定下标修改元素,返回被修改的元素 * @param index * @param ele * @return */ public E set(int index,E ele){ Node node = this.first; for (int i = 0; i < index-1; i++) { node = node.next; } E a = node.element; node.element = ele; return a; } /** * 清空双链表 */ public void clean(){ int a = count; for (int i = 0; i < a; i++) { --count; } } public List<Integer> select(E ele){ Node node = this.first; List<Integer> list = new ArrayList<>(); for (int i = 0; i < count; i++) { if (node.element.equals(ele)){ list.add(i); node = node.next; }else node = node.next; } return list; } /** * * @param ele----要删除的所有元素 */ public void delWithElement(E ele){ Node node = first.next; boolean flag = false; int a = count;//统计比较次数,优先去除中间部分,最后去除首尾 for (int i = 0; i < a-2; i++) { if (node.element.equals(ele)){ node.pervious.next = node.next; node.next.pervious = node.pervious; node = node.next; flag = true; count--; }else{ node = node.next; } } if (this.first.element.equals(ele)){ flag = true; this.first = this.first.next; count--; } if (last.element.equals(ele)){ flag = true; this.last.pervious = this.last; count--; } if (flag == true){ System.out.println("删除成功!"); }else { System.out.println("没有找到指定元素!"); } System.out.println(count); } /** * * @param index 指定下标 * @return 被删除元素 */ public E delWithIndex(int index){ Node node = this.first; if (index < 0 && index >count){ throw new IndexOutOfBoundsException("count:"+count+" ,index"+index); } if (index == 0){ this.first = node.next; }else if (index == count){ this.last.pervious = this.last; }else { for (int i = 0; i < index; i++) { node = node.next; } node.pervious.next = node.next; node.next.pervious = node.pervious; } count--; return node.element; } /** *在当前集合加一个元素 */ public void add(E element){ //实例化一个节点,用来存储这个节点存储的元素 Node node = new Node(element); //判断当前链表是否为空 if(count == 0){ this.first = node; }else { this.last.next = node; node.pervious = this.last; } this.last = node;//让Last指向当前的尾节点 count++;//让节点点数+1 } /** * 指点下标插入一个元素 * @param index 下标 * @param element 插入元素 */ public void add(int index,E element){ //实例化一个节点,用来存储这个节点存储的元素 Node node = new Node(element); //节点关联 if (index == 0){ //首位插入 this.first.pervious = node; node.next = this.first; this.first = node; }else if (index == count){ //最后插入 this.last.next = node; node.pervious = this.last; this.last = node; }else { //中间插入 //先获取当前下标的节点,实际将node放在target前面 Node target = getNode(index); //节点关联 node.next = target; node.pervious = target.pervious; target.pervious.next = node; target.pervious = node; } count++; } /** * 通过下标获得指定接节点 * @param index 指定下标 * @return 下标对应节点 */ public Node getNode(int index){ //1、判断下标合法性 if (index < 0 || index >= count){ throw new IndexOutOfBoundsException("size():"+count+" ,index"+index); } //2、循环向下查找指定节点 Node node = this.first; for (int i = 0; i < index; i++) { node = node.next; } return node; } /** * */ /** * 重写ToString */ @Override public String toString() { if (count == 0){ return "[]"; } //1、实例化一个String Buffer对象,用来装最后的元素信息 StringBuffer stringBuffer = new StringBuffer("["); //2、遍历每个节点 Node node = this.first; for (int i = 0; i < count; i++) { //元素拼接 stringBuffer.append(node.element).append(", "); //节点向后移动一位节点 node = node.next; } stringBuffer.replace(stringBuffer.length()-2,stringBuffer.length(),"]"); return stringBuffer.toString(); } }
12-28
245