特色个人网站/百度一下官方网址
参考视频: 【黑马程序员】2020最新数据结构与算法教程(求职面试必备)
参考leetcode学习资料: 图解算法数据结构
注意 目录结构呦!!!按本文目录
在src文件夹下
创建项目和文件,直接粘代码即可运行
文章目录
- 算法和数据结构简述+排序的笔记
- Mywrite
- linear
- SequenceList(顺序表)
- LinkList(单链表)
- TwoWayLinkList(双向链表)
算法和数据结构简述+排序的笔记
【Java数据结构笔记一】-- 数据结构与算法概述–【时间复杂度+空间复杂度】
【Java数据结构笔记三】-- 线性表
Mywrite
linear
SequenceList(顺序表)
package Mywrite.linear;import java.util.Iterator;// 线性表——顺序表——基本实现
public class SequenceList<T> implements Iterable<T> {// 测试public static void main(String[] args) {// 创建顺序表对象SequenceList<String> SL = new SequenceList<>(10);// 测试插入SL.insert("会的传教士这部分");SL.insert("要命");SL.insert("zhaznmusi");SL.insert(1,"我的对我的");for(String s: SL){System.out.println(s);}// 测试获取String getResult = SL.get(1);System.out.println("获取1处的索引值为:"+getResult);// 测试删除String removeResult = SL.remove(0);System.out.println("删除的元素是" + removeResult);// 清空测试,以及返回长SL.clear();System.out.println("清空后的线性表中得到元素个数为"+SL.length());}// 存储元素的数组private T[] eles;// 记录当前顺序表中的元素个数private int N;//构造方法public SequenceList(int capacity) {// 初始化数据this.eles = (T[]) new Object[capacity];// 初始化长度this.N = 0;}// 将一个线性表置为空表public void clear() {this.N = 0;}// 判断当前线性表是否为空表public boolean isEmpty() {return N == 0;}// 获取线性表的长度public int length() {return N;}//获取指定位置的元素public T get(int i) {return eles[i];}// 向线性表中添加元素tpublic void insert(T t) {eles[N++] = t;}//在i元素处插入元素tpublic void insert(int i, T t) {if (N == eles.length) {resize(2*eles.length);}// 先把i索引的元素及其后面的元素依次后移一位for (int index = N; index > i; index--) {eles[index] = eles[index - 1];}//将t元素放到i索引处即可eles[i] = t;// 元素个数 +1N++;}// 删除指定位置i处的元素,并返回该元素public T remove(int i) {// 记录索引i处的值T current = eles[i];for (int index = i; index < N - 1; index++) {eles[index] = eles[index + 1];}// 元素个数 -1N--;if(N<eles.length/4) {resize(eles.length/2);}return current;}//查找t元素第一次出现的位置public int indexOf(T t) {for (int index = 0; index < N; index++) {if (eles[index].equals(t)) {return index;}}return -1;}//根据参数newSize , 重置eles的大小public void resize(int newSize) {// 定义一个临时数组,指向原数组T[] temp = eles;// 创建新数组eles = (T[]) new Object[newSize];// 把原数组的数据拷贝到新数组即可for (int i = 0; i < N; i++) {eles[i] = temp[i];}}//提供便利@Overridepublic Iterator<T> iterator() {// 内部类不能遍历return new SIterator();}private class SIterator implements Iterator {private int cursor;public SIterator() {this.cursor = 0;}@Overridepublic boolean hasNext() {return cursor < N;}//获取当前元素的下一个方法@Overridepublic Object next() {return eles[cursor++];}}
}
LinkList(单链表)
package Mywrite.linear;import java.util.Iterator;public class LinkList<T> implements Iterable<T>{public static void main(String[] args) {// 创建顺序表对象LinkList<String> SL = new LinkList<>();// 测试插入SL.insert("会的传教士这部分");SL.insert("要命");SL.insert("zhaznmusi");SL.insert(1,"我的对我的");for(String s: SL){System.out.println(s);}// 测试获取String getResult = SL.get(1);System.out.println("获取1处的索引值为:"+getResult);// 测试删除String removeResult = SL.remove(0);System.out.println("删除的元素是" + removeResult);// 清空测试,以及返回长SL.clear();System.out.println("清空后的线性表中得到元素个数为"+SL.length());}//记录头结点private Node head;//记录链表的长度private int N;//结点类private class Node {// 存储数据T item;// 下一个节点Node next;public Node(T item,Node next){this.item = item;this.next = next;}}public LinkList(){//初始化头结点,默认值指向空的this.head = new Node(null,null);// 初始化this.N = 0;}// 清空链表public void clear(){head.next = null;this.N = 0;}// 获取链表的长度public int length(){return N;}// 判断链表是否为空public boolean isEmpty(){return N == 0;}// 获取指定位置i的元素public T get(int i){// 通过循环,从头结点开始往后找,依次找i次,就可以找出对应的元素Node n = head.next;for(int index = 0; index<i; index++){n = n.next;}return n.item;}// 向链表中添加元素tpublic void insert(T t){//找到链表中的最后一个结点Node n = head;while(n.next != null){n = n.next;}//创建新的节点Node newNode = new Node(t, null);n.next = newNode;// 元素的个数夹一N++;}// 向指定元素i处添加元素public void insert(int i, T t){//找到i处的前一个节点Node pre = head;for(int index=0; index<=i-1; index++){pre = pre.next;}Node curr = pre.next;Node newNode = new Node(t,curr);pre.next = newNode;N++;}// 删除指定位置i出的元素,并返回被删除的元素public T remove(int i){Node pre = head;for(int index = 0; index<=i-1; index++){pre = pre.next;}Node curr = pre.next;pre.next = curr.next;N--;return curr.item;}// 查找元素t在链表中第一次出现的位置public int search(T t){Node n = head;for(int i=0; n.next!=null; i++){n = n.next;if(n.item.equals(t)){return i;}}return -1;}@Overridepublic Iterator<T> iterator() {return new LIterator();}private class LIterator implements Iterator{private Node n;public LIterator(){this.n = head;}@Overridepublic boolean hasNext() {return n.next!=null;}@Overridepublic Object next() {n = n.next;return n.item;}}
}
TwoWayLinkList(双向链表)
package Mywrite.linear;//import org.w3c.dom.Node;import java.util.Iterator;public class TwoWayLinkList<T> implements Iterable<T>{public static void main(String[] args) {// 创建双向链表对象TwoWayLinkList<String> sl= new TwoWayLinkList<>();// 测试插入sl.insert("姚明");sl.insert("科比");sl.insert("麦迪");sl.insert(1,"詹姆斯");for(String s: sl){System.out.println(s);}System.out.println("-----------------");// 测试获取String getResult = sl.get(1);System.out.println("获取索引1处的结果为:"+getResult);// 测试删除String removeResult = sl.get(0);System.out.println("删除的元素:" + removeResult);System.out.println("---------------------");System.out.println("第一个元素是"+sl.getFirst());System.out.println("最后一个元素是:" + sl.getLast());// 测试清空sl.clear();System.out.println("清空后的线性表中的元素个数为:"+sl.length());}//首结点private Node head;// 最后一个结点private Node last;//链表的长度private int N;// 结点类private class Node {public Node(T item, Node pre, Node next) {this.item = item;this.pre = pre;this.next = next;}//存储数据public T item;// 指向上一个结点public Node pre;// 指向下一个结点public Node next;}public TwoWayLinkList() {//初始化头节点和尾结点this.head = new Node(null, null, null);this.last = null;// 初始化元素结点this.N = 0;}// 清空链表public void clear() {this.head.next = null;this.head.item = null;this.last = null;this.N = 0;}// 获取链表长度public int length() {return N;}// 判断链表是否为空public boolean isEmpty() {return N == 0;}// 获取第一个元素public T getFirst() {if (isEmpty()) {return null;}return head.next.item;}// 获取最后一个元素public T getLast() {if (isEmpty()) {return null;}return last.item;}// 插入元素tpublic void insert(T t) {// 如果链表为空if (isEmpty()) {// 创建新的节点Node newNode = new Node(t, head, null);// 让新结点成为尾结点last = newNode;// 让头结点指向尾结点head.next = last;} else {// 如果链表不为空Node oldLast = last;// 创建新的结点Node newNode = new Node(t, oldLast, null);// 让当前的尾节点 指向新结点oldLast.next = newNode;// 让新结点成为尾结点last = newNode;}N++;}//向指定位置i处插入元素tpublic void insert(int i, T t) {Node pre = head;for (int index = 0; index < i; index++ ){pre = pre.next;}Node curr = pre.next;Node newNode = new Node(t, pre, curr);pre.next = newNode;curr.pre = newNode;N++;}// 获取指定位i处的元素public T get(int i){Node n = head.next;for(int index = 0; index < i; index++){n = n.next;}return n.item;}// 找到元素t在链表中第一次出现的位置public int indexOf(T t){Node n = head;for(int index = 0; index<N; index++){n = n.next;if(n.next.equals(t)){return index;}}return -1;}// 删除位置i处的元素,并返回该元素public T remove(int i){Node pre = head;for(int index = 0; index < i; index++){pre = pre.next;}Node curr = pre.next;Node nextNode = curr.next;pre.next = nextNode;nextNode.pre = pre;// 元素个数减一N--;return curr.item;}@Overridepublic Iterator<T> iterator() {return new TIterator();}private class TIterator implements Iterator{private Node n;public TIterator(){this.n = head;}@Overridepublic boolean hasNext() {return n.next != null;}@Overridepublic Object next() {n = n.next;return n.item;}}
}
要点赞+关注+收藏呦👉
笔记在博主本人的博客上也有奥!全套!!!
- 翼遥bingo【持续完善中,biu~~】