diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..73f69e0958611ac6e00bde95641f6699030ad235 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..d4b854b8d4fa7be1ca526617ed1590623b835f8c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..a9a967ebf636c25d481adbb480e9fb8fb3f25eb1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DoubleLinkedList.java b/DoubleLinkedList.java new file mode 100644 index 0000000000000000000000000000000000000000..6dafcc0e40c3f1acbf9ea93e8a783cb60981e5b7 --- /dev/null +++ b/DoubleLinkedList.java @@ -0,0 +1,85 @@ +public class DoubleLinkedList { + private DoubleNode head; + + public DoubleLinkedList() { + head=null; + } + + //双向链表的插入 + public void addToFront(E item) { + DoubleNode newNode = new DoubleNode(item); + if (this.head == null) this.head = newNode; + else { + this.head.pre = newNode; + //把this.head.next节点往下移动 + newNode.next = this.head; + } + //把插入的节点作为新的节点 + this.head = newNode; + + } + + // 双向链表尾巴插入 + public void addToRear(E item) { + DoubleNode newNode = new DoubleNode(item); + if (this.head == null) { + this.head = newNode; + } else { + + DoubleNode temp = this.head; + while(temp.next != null){ + temp = temp.next; + } + temp.next = newNode; + newNode.pre=temp; + } + + } + // 删除头节点的位置 + public void deleteItem(int index){ + if (index<0 || index>size()-1){ + System.out.println("删除节点不在链表内"); + } + if (index == 0){ + head = head.next; + return; + } + int position = 0; + DoubleNode cur = head; + DoubleNode pre =null; + while (cur!=null){ + if (position==index){ + pre.next = cur.next; + cur.next.pre=pre; + //把要删除的结点置空 + cur.next = null; + return; + } + pre = cur; + cur = cur.next; + position++; + } + } + //判断链表插入的位置 + public int size(){ + int size = 0; + + DoubleNode temp = this.head; + while(temp.next != null){ + temp = temp.next; + size++; + } + return size; + } + + + + public void print() { + DoubleNode temp = this.head; + while(temp.next != null){ + System.out.print(temp.item + " -> "); + temp = temp.next; + } + System.out.println(temp.item); + } +} diff --git a/DoubleNode.java b/DoubleNode.java new file mode 100644 index 0000000000000000000000000000000000000000..b3bb504db2a17dd9e5292de41868f2bdff5e43c7 --- /dev/null +++ b/DoubleNode.java @@ -0,0 +1,9 @@ +public class DoubleNode { + E item; + DoubleNode next = null; //用来指向下一个结点 + DoubleNode pre=null;//用来指向前一个结点 + + DoubleNode(E item) { + this.item = item; + } +} diff --git a/LinkedList.java b/LinkedList.java index 7836598c18ca0b770825087dc2deff812a699ec6..3792a4dd6ebe84b28ade5ccf820d717768e7e604 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -2,9 +2,9 @@ public class LinkedList { private Node head; public LinkedList() { - head = null; + head = null; } - + //在头节点插入元素 public void addToFront(E item) { Node node = new Node(item); @@ -17,21 +17,135 @@ public class LinkedList { } } - + //在尾节点插入元素 public void addToRear(E item) { Node newNode = new Node(item); if (this.head == null) { this.head = newNode; } else { - + Node temp = this.head; while(temp.next != null){ - temp = temp.next; + temp = temp.next; } temp.next = newNode; } } + //判断链表插入的位置 + public int size(){ + int size = 0; + + Node temp = this.head; + while(temp.next != null){ + temp = temp.next; + size++; + } + return size; + } + // 在任意位置插入节点 + public void addItem(E item,int index){ + if (index<0 || index>size()){ + System.out.println("输入的节点有误"); + return; + } + if (index==0){ + //插入到链表头部 + this.addToFront(item); + + }else if(index==size()){ + this.addToRear(item); + }else { + //插入到中间的位置 + Node newnode = new Node(item); + int position = 0; + Node cur = this.head; + Node pre = null; + while (cur!=null){ + if (position==index){ + newnode.next = cur; + pre.next = newnode; + return; + } + pre = cur; + cur=cur.next; + position++; + } + + } + } + + // 删除头节点的位置 + public void deleteItem(int index){ + if (index<0 || index>size()-1){ + System.out.println("删除节点不在链表内"); + } + if (index == 0){ + head = head.next; + return; + } + int position = 0; + Node cur = head; + Node pre =null; + while (cur!=null){ + if (position==index){ + pre.next = cur.next; + //把要删除的结点置空 + cur.next = null; + return; + } + pre = cur; + cur = cur.next; + position++; + } + } +// public boolean compare(Node temp,Node newnode){ +// int index=1; +// if (temp.item.equals(newnode.item)){ +// +// +// } +// +// } +// + + public void findItem(E item){ + int index=1; + Node newnode = new Node(item); + Node temp = this.head; + while (temp.next!=null){ + if (temp.item.equals(newnode.item)){ + System.out.println("元素的位置:"+index); + } + temp = temp.next; + index++; + } + if (temp.item.equals(newnode.item)){ + System.out.println("元素的位置:"+index); + } + + } + //单链表的反转 + public void reverse(){ +// 标记当前节点 + Node cur = head; + //标记当前节点的前一个节点 + Node pre = null; + Node temp; + while (cur!=null){ + //保存当前节点的下一个格式 + temp = cur.next; + //cur.next指向pre,指针顺序置换 + cur.next = pre; + //pre、cur继续后移动 + pre = cur; + cur = temp; + } + //最后一个结点变成新的节点 + head = pre; + } + + public void print() { Node temp = this.head; while(temp.next != null){ diff --git a/Main.java b/Main.java index 057428817fa315cf419ab3ea8cc74615dbf94f39..60d7107c427f8c9c8b5818b323199648953a8124 100644 --- a/Main.java +++ b/Main.java @@ -1,10 +1,38 @@ class Main { public static void main(String[] args) { LinkedList ll = new LinkedList(); +// 在头节点插入 ll.addToFront(1); ll.addToFront(2); + +// 在尾节点插入 ll.addToRear(3); +// 单链表查找 + ll.findItem(3); +// 在中间节点插入 +// ll.addItem(4,2); + +// 删除头节点 +// ll.deleteItem(1); + +// 单链表反转 +// ll.reverse(); ll.print(); + + +//// 双向链表 +// DoubleLinkedList dl = new DoubleLinkedList(); +//// 在头节点添加 +// dl.addToFront(1); +// dl.addToFront(3); +// dl.addToFront(2); +////在尾巴结点添加 +// dl.addToRear(5); +// dl.addToRear(6); +// dl.addToRear(7); +//// 删除结点 +//// dl.deleteItem(1); +// dl.print(); } } \ No newline at end of file diff --git a/Node.java b/Node.java index ed7a3524e3c608d2a479f484e240452ad34c5a51..fe64454a80aa32af96561d01573a106861693888 100644 --- a/Node.java +++ b/Node.java @@ -1,6 +1,6 @@ public class Node { E item; - Node next = null; + Node next = null; Node(E item) { this.item = item; diff --git a/lec02-linkedlist.iml b/lec02-linkedlist.iml new file mode 100644 index 0000000000000000000000000000000000000000..b107a2dd81165eaaf682ad3da030668b937fbb6c --- /dev/null +++ b/lec02-linkedlist.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/lec02-linkedlist/.gitignore b/out/production/lec02-linkedlist/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d160a7b219f7fd0ea93ad4761fc8bf451476c7f0 --- /dev/null +++ b/out/production/lec02-linkedlist/.gitignore @@ -0,0 +1,5 @@ +gen-branch.js +package.json +package-lock.json +node_modules +id.txt diff --git a/out/production/lec02-linkedlist/.idea/.gitignore b/out/production/lec02-linkedlist/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..73f69e0958611ac6e00bde95641f6699030ad235 --- /dev/null +++ b/out/production/lec02-linkedlist/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/out/production/lec02-linkedlist/.idea/misc.xml b/out/production/lec02-linkedlist/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..d4b854b8d4fa7be1ca526617ed1590623b835f8c --- /dev/null +++ b/out/production/lec02-linkedlist/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/lec02-linkedlist/.idea/modules.xml b/out/production/lec02-linkedlist/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..a9a967ebf636c25d481adbb480e9fb8fb3f25eb1 --- /dev/null +++ b/out/production/lec02-linkedlist/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/lec02-linkedlist/.idea/vcs.xml b/out/production/lec02-linkedlist/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/out/production/lec02-linkedlist/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lec02-linkedlist/DoubleLinkedList.class b/out/production/lec02-linkedlist/DoubleLinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..9480f0cac8877b7e14a3cf874fbfd9098e797595 Binary files /dev/null and b/out/production/lec02-linkedlist/DoubleLinkedList.class differ diff --git a/out/production/lec02-linkedlist/DoubleNode.class b/out/production/lec02-linkedlist/DoubleNode.class new file mode 100644 index 0000000000000000000000000000000000000000..e53ecc26f988ac3278003f6511173696542baf9e Binary files /dev/null and b/out/production/lec02-linkedlist/DoubleNode.class differ diff --git a/out/production/lec02-linkedlist/LinkedList.class b/out/production/lec02-linkedlist/LinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..ed066c5d5c67489fca0659a7c3162ce8e1e67b94 Binary files /dev/null and b/out/production/lec02-linkedlist/LinkedList.class differ diff --git a/out/production/lec02-linkedlist/Main.class b/out/production/lec02-linkedlist/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..98676b61d610ca3bfdedd309d76aec878ff6d7fb Binary files /dev/null and b/out/production/lec02-linkedlist/Main.class differ diff --git a/out/production/lec02-linkedlist/Node.class b/out/production/lec02-linkedlist/Node.class new file mode 100644 index 0000000000000000000000000000000000000000..8bca6e1e69966e6bd08aca9f634246092aa2e5a8 Binary files /dev/null and b/out/production/lec02-linkedlist/Node.class differ diff --git a/out/production/lec02-linkedlist/README.md b/out/production/lec02-linkedlist/README.md new file mode 100644 index 0000000000000000000000000000000000000000..dcb6b868f63b2e79f78cd27bcd722adaa8d46ca4 --- /dev/null +++ b/out/production/lec02-linkedlist/README.md @@ -0,0 +1,37 @@ +## 链表实验 + +### 作业要求 + +1. 完成基础部分是基本任务 +2. 完成进阶部分锻炼自己,做多少是多少 + +### 截止日期 + +下周三上课前(11.04) + +### 基础 + +1. 用你擅长的语言实现链表 +2. 在 1 的基础上实现带尾指针的链表:将 1 的代码复制一份,新建一个项目来完成。 +3. 在 2 的基础上实现双向链表:将 1 的代码复制一份,新建一个项目来完成。 + +### 进阶 + +1. 单链表反转 +2. 两个有序的链表合并 +3. 删除双向链表倒数第 n 个结点 +4. 求链表的中间结点 +5. LRU 缓存淘汰算法的链表实现(optional) + +## 工作流程 + +1. Fork 本仓库 +2. Clone 代码到本地 +3. 切换到自己学号对应的分支 +4. 完成作业 +5. 提交代码 +6. 新建 Pull Request + +## Questions + +1. 如何提交?[参考作业提交流程.pdf](./作业提交流程.pdf) 文件 diff --git a/out/production/lec02-linkedlist/lec02-linkedlist.iml b/out/production/lec02-linkedlist/lec02-linkedlist.iml new file mode 100644 index 0000000000000000000000000000000000000000..b107a2dd81165eaaf682ad3da030668b937fbb6c --- /dev/null +++ b/out/production/lec02-linkedlist/lec02-linkedlist.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git "a/out/production/lec02-linkedlist/\344\275\234\344\270\232\346\217\220\344\272\244\346\265\201\347\250\213.pdf" "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\346\217\220\344\272\244\346\265\201\347\250\213.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..c9664d640326aca09bc745b129b70a0d3cb3f62e Binary files /dev/null and "b/out/production/lec02-linkedlist/\344\275\234\344\270\232\346\217\220\344\272\244\346\265\201\347\250\213.pdf" differ diff --git "a/out/production/lec02-linkedlist/\350\257\276\344\273\266/lecture02-linked-list.pdf" "b/out/production/lec02-linkedlist/\350\257\276\344\273\266/lecture02-linked-list.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..50b893fb4c97f8c6ee2349886f4c1c8dab62bf68 Binary files /dev/null and "b/out/production/lec02-linkedlist/\350\257\276\344\273\266/lecture02-linked-list.pdf" differ