diff --git "a/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/DoubleLinkedList.java" "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/DoubleLinkedList.java" new file mode 100644 index 0000000000000000000000000000000000000000..db1e6b2ab87c964f2bb5c8355152a6a7382c461e --- /dev/null +++ "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/DoubleLinkedList.java" @@ -0,0 +1,176 @@ +public class DoubleLinkedList { + //声明为public,方便存取 + //头指针 + public Node first; + //尾指针 + public Node last; + //链表中实际存储的数据的数目 + public int size; + + //初始化 + public DoubleLinkedList() { + this.first = null; + this.last = null; + this.size = 0; + } + + //得到链表容量 + public int size() { + return size; + } + + //判断链表是否为空 + public boolean isEmpty() { + return size == 0; + } + + //添加头节点 + public void addFirst(int data) { + //创建新节点 + Node newNode = new Node(data); + //判断链表是否为空 + if(isEmpty()) { + last = newNode; + }else { + first.previous = newNode; + newNode.next = first; + } + first = newNode; + size ++; + } + + //添加尾节点 + public void addLast(int data) { + //创建新节点 + Node newNode = new Node(data); + //判断链表是否为空 + if(isEmpty()) { + first = newNode; + }else { + last.next = newNode; + newNode.previous = last; + } + last = newNode; + size ++; + } + + //删除头结点,并返回头结点 + public Node deleteFirst() { + if(isEmpty()) { + System.out.println("链表为空!"); + return null; + } + Node temp = first; + if(size == 1) { + //如果链表中只有一个元素 + last = null; + }else { + first.next.previous = null; + } + first = first.next; + size --; + return temp; + } + + //删除尾节点,并返回尾节点 + public Node deleteLast() { + if(isEmpty()) { + System.out.println("链表为空!"); + return null; + } + Node temp = last; + if(size == 1) { + //如果只有一个元素 + first = null; + }else { + last.previous.next = null; + } + last = last.previous; + size --; + return temp; + } + + //将节点插入到指定值为key的节点后面 + public void insert(int key,int value) { + //创建要插入的新节点 + Node newNode = new Node(value); + //创建要插入节点位置上原来的节点 + Node current = first; + if(isEmpty()) { + System.out.println("没有值为" + key + "的值!"); + return; + } + while(current.data != key) { + if(current == null) { + System.out.println("没有值为" + key + "的值!"); + return; + } + //往下遍历 + current = current.next; + } + current.next.previous = newNode; + newNode.next = current.next; + current.next = newNode; + newNode.previous = current; + size ++; + } + + //删除特定的节点,并返回该节点 + public Node deleteNode(int value) { + if(isEmpty()) { + System.out.println("没有值为" + value + "的值!"); + } + //创建要删除节点 + Node current = first; + while(current.data != value) { + if(current == null) { + System.out.println("没有值为" + value + "的值!"); + } + //继续向下遍历 + current = current.next; + } + //如果要删除的节点为首节点 + if(current == first) { + deleteFirst(); + }else if(current == last) { + //如果要删除的节点为尾节点 + deleteLast(); + }else { + current.previous.next = current.next; + current.next.previous = current.previous; + } + size --; + return current; + } + + + //正向遍历链表 + public void traverseForward() { + Node current = first; + while(current != null) { + System.out.println(current.data); + current = current.next; + } + } + //反向遍历链表 + public void traverseBackwrad() { + Node current = last; + while(current != null) { + System.out.println(current.data); + current = current.previous; + } + } +} +class Node{ + //声明为public,方便存取 + //指向前一个节点 + public Node previous; + //指向后一个节点 + public Node next; + //数据域 + public int data; + + public Node(int data) { + this.data = data; + } +} diff --git "a/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/LinkedList.class" "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/LinkedList.class" new file mode 100644 index 0000000000000000000000000000000000000000..d018942cfbf5d641a023df78ac6820284d8d8165 Binary files /dev/null and "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/LinkedList.class" differ diff --git "a/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/LinkedList.java" "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/LinkedList.java" new file mode 100644 index 0000000000000000000000000000000000000000..7c3820fafc64fbe2693cd51794a89d8821f6d693 --- /dev/null +++ "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/LinkedList.java" @@ -0,0 +1,132 @@ +public class LinkedList { + private Node first;// 定义头节点 + private int nItems;// 定义单链表中实际的数据的数目 + + // 初始化 + public LinkedList() { + this.first = null; + this.nItems = 0; + } + + // 添加头节点 + public void addFirst(int data) { + // 新建节点 + Node newNode = new Node(data); + // 将新节点的下一个节点指向旧的头节点 + newNode.next = first; + // 将新节点设为头节点 + first = newNode; + + nItems++; + } + + // 删除头结点 + public boolean deleteFirst() { + // 判断链表是否为空 + if (isEmpty()) { + System.out.println("链表为空!"); + return false; + } + first = first.next; + nItems--; + return true; + } + + // 有序链表的插入,这样简单排序就可以用链表来实现,复杂度为O(N) + public void add(int data) { + // 创建新节点 + Node newNode = new Node(data); + // 创建要插入节点之前的节点 + Node previous = null; + // 创建要插入节点的位置上原来的节点 + Node current = first; + // 按从小到大的顺序排序 + while (current != null && data > current.data) { + previous = current; + current = current.next; + } + if (previous == null) { + first = newNode; + } else { + previous.next = newNode; + } + newNode.next = current; + nItems++; + } + + // 查询某个特定值的节点 + public Node findNode(int data) { + // 定义一个新节点用于查询 + Node current = first; + while (current != null && current.data != data) { + if (current.next == null) { + System.out.println("该节点不存在"); + return null; + } + current = current.next; + } + return current; + } + + // 删除某个特定值的节点,并返回该节点 + public Node deleteNode(int data) { + // 定义被删除节点之前的节点 + Node previous = null; + // 定义被删除的节点 + Node current = first; + while (current != null && current.data != data) { + if (current.next == null) { + System.out.println("该节点不存在"); + return null; + } + previous = current; + current = current.next; + } + if (previous == null) { + first = first.next; + } else { + previous.next = current.next; + } + nItems--; + return current; + } + + // 遍历链表 + public void traverseList() { + // 定义一个节点用于遍历 + Node current = first; + // 判断链表是否为空 + if (current == null) { + System.out.println("链表为空!"); + return; + } + while (current != null) { + System.out.println(current.data); + current = current.next; + } + } + + // 链表的长度 + public int size() { + return nItems; + } + + // 判断链表是否为空 + public boolean isEmpty() { + return first == null; + } + +} + +// 定义节点 +class Node { + // 声明为public,方便存取 + // 指向下一个节点 + public Node next; + // 数据域 + public int data; + + public Node(int data) { + this.data = data; + } +} diff --git "a/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Node.class" "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Node.class" new file mode 100644 index 0000000000000000000000000000000000000000..5250f6a935e4b02251a9389e4e7c0291b20bc0ba Binary files /dev/null and "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Node.class" differ diff --git "a/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Programs.class" "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Programs.class" new file mode 100644 index 0000000000000000000000000000000000000000..e482acdd572178e1908f5d661f6ba6a50382a813 Binary files /dev/null and "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Programs.class" differ diff --git "a/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Programs.java" "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Programs.java" new file mode 100644 index 0000000000000000000000000000000000000000..509c99604b9067a0640945be6f06da85cac4a166 --- /dev/null +++ "b/08\345\202\205\345\233\275\351\224\213\344\275\234\344\270\232/Programs.java" @@ -0,0 +1,16 @@ +public class Programs { + public static void main(String args[]){ + LinkedList link=new LinkedList(); + link.add(1); + link.add(2); + link.add(3); + link.add(4); + link.add(5); + link.add(6); + link.deleteFirst(); + link.deleteNode(2); + link.findNode(3); + System.out.println(link.size()); + link.traverseList(); + } +} \ No newline at end of file