tonglin0325的个人主页

Java数据结构——双向链表

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class DoublyLinkedList{				//双向链表
private Link_long first;
private Link_long last;

public DoublyLinkedList(){ //构造函数
this.first = null;
this.last = null;
}

public boolean isEmpty(){
return first==null;
}

public void insertFirst(long dd){ //从链表的头开始插入
Link_long newLink = new Link_long(dd);
if(isEmpty()){
last = newLink; //不用改变first
}else{
first.previous = newLink; //插入新的元素
}
newLink.next = first; //插入新的元素
first = newLink; //修改first的位置
}

public void insertLast(long dd){ //从链表的尾开始插入
Link_long newLink = new Link_long(dd);
if(isEmpty()){
first = newLink; //不用改变last
}else{
last.next = newLink; //在last后面添加新元素,并修改last的位置
newLink.previous = last;
}
last = newLink; //注意:只有一个元素的时候,插入要把last也赋为newLink
}

public Link_long deleteFirst(){ //从链表的头删除一个元素
Link_long temp = first; //暂存first
if(first.next == null){ //如果只有一个元素,把last也赋为null
last = null;
}else{
first.next.previous = null;
}
first = first.next; //把next设为first
return temp; //返回原来的first
}

public Link_long deleteLast(){ //从链表的头删除一个元素
Link_long temp = last; //暂存last
if(first.next == null){ //如果只有一个元素,把first也赋为null
first = null;
}else{
last.previous.next = null;
}
last = last.previous; //把previous设为last
return temp; //返回原来的last
}

public boolean insertAfter(long key,long dd){ //在特定元素后面插入
Link_long current = first;
while(current.dData != key){
current = current.next;
if(current == null){
return false;
}
}
Link_long newLink = new Link_long(dd);

if(current == last){
newLink.next = null;
last = newLink;
}else{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}

public Link_long deleteKey(long key){ //删除指定元素
Link_long current = first;
while(current.dData != key){
current = current.next;
if(current == null){
return null;
}
}
if(current == first){ //如果第一个元素匹配,则删除,并把first向后移动
first = current.next;
}else{
current.previous.next = current.next; //改变current上一个元素的next的指向
}
if(current == last){
last = current.previous; //如果最后一个元素匹配,则删除,并把previous向前移动
}else{
current.next.previous = current.previous; //改变current后一个元素的previous的指向
}
return current;
}

public void displayForward(){
System.out.println("List(first-->last):");
Link_long current = first; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.next;
}
}

public void displayBackward(){
System.out.println("List(last-->first):");
Link_long current = last; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.previous;
}
}

}

public class DoublyLinked_demo {

public static void main(String[] args) {
// TODO 自动生成的方法存根
DoublyLinkedList theList = new DoublyLinkedList();

theList.insertFirst(40);
theList.insertFirst(30);
theList.insertFirst(20);
theList.insertFirst(10);
theList.displayForward();
theList.displayBackward();

theList.insertAfter(20, 25);
theList.displayForward();
theList.displayBackward();

theList.deleteFirst();
theList.displayForward();

theList.deleteKey(20);
theList.displayForward();
}

}

 

OpenTSDB API基本操作

1.OpenTSDB CLI,参考

1
2
https://www.docs4dev.com/docs/zh/opentsdb/2.3/reference/user_guide-cli-index.html

 

 

2.OpenTSDB HTTP API,参考

1
2
https://www.docs4dev.com/docs/zh/opentsdb/2.3/reference/api_http-index.html

 

使用java原生API,DOM4J,JDOM和SAX解析XML文件

解析 XML 有两种方式: SAX 和 DOM 。它们各有利弊。

        DOM 是把 XML 文档全部装载到内存中,然后当成一树进行处理。其好处是当成树处理起来比较方便,但弊端是如果 XML 文件比较大时,会对内存消耗比较大;

        SAX 是逐行扫描 XML 文档,逐行解析,而且可以在处理 XML 文档过程中的任意时刻中止处理过程,比如找到我们的目标节点,剩下的 XML 文档内容就可以不读了,直接结束。其弊端是操作起来相对不方便,而且对 XML 文档进行处理,如果修改、新增、删除等操作比较不方便。

        SAX 是事件驱动型 XML 解析的一个标准接口。它的工作原理是读到文档的开始与结束、标签元素的开始与结束、内容实体等地方时,触发相应的函数,我们就可以在相应的函数中进行我们所要进行的处理。

 

1.使用Java API解析DOM解析

 

全文 >>

Java数据结构——哈夫曼树(Huffman Tree)

1.哈夫曼树

给定N个权值作为N个叶子节点,构造一棵二叉树,若该树的带权路径长度****(WPL)达到最小,称这样的二叉树为最优二叉树,也称为**哈夫曼树(Huffman Tree)**。

哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。

参考:霍夫曼树(参考该文章中的概念,其中代码不对)

2.树的带权路径长度(WPL,Weighted Path Length of Tree)

树的带权路径长度,就是树中所有的叶结点的权值乘上其到根结点的路径长度(若根结点为0层,叶结点到根结点的路径长度为叶结点的层数)

如下图中的哈夫曼树,其WPL的值 = (2+3) * 3 + 4 * 2 + 6 * 1 = 29

上图参考:数据结构——哈夫曼树(Huffman Tree)

3.哈夫曼树的应用

1.哈夫曼编码

4.哈夫曼编码的实现

1.哈夫曼编码的步骤:

  • 根据输入的字符构建哈夫曼树
      1. 统计原始数据中各字符出现的频率; 1. 所有字符按频率降序排列; 1. 建立哈夫曼树:
      1. 将哈夫曼树存入结果数据;
      2. 重新编码原始数据到结果数据

        全文 >>

  • Java数据结构——双端链表

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    class FirstLastList{
    private Link first;
    private Link last;

    public FirstLastList() { //构造函数
    this.first = null;
    this.last = null;
    }

    public boolean isEmpty(){
    return (first == null);
    }

    public void insertFirst(int id,double dd){ //从链表的头开始插入
    Link newLink = new Link(id,dd);
    if(isEmpty()){
    last = newLink; //不用改变first
    }
    newLink.next = first;
    first = newLink;
    }

    public void insertLast(int id,double dd){ //从链表的尾开始插入
    Link newLink = new Link(id,dd);
    if(isEmpty()){
    first = newLink; //不用改变last
    }else{
    last.next = newLink; //在last后面添加新元素,并修改last的位置
    }
    last = newLink; //注意:只有一个元素的时候,插入要把last也赋为newLink
    }

    public Link deleteFirst(){
    Link temp = first; //暂存first
    if(first.next == null){ //如果只有一个元素,把last也赋为null
    last = null;
    }
    first = first.next; //把next设为first
    return temp; //返回原来的first
    }

    public void displayList(){
    System.out.println("List(first-->last):");
    Link current = first; //用于不断改变位置实现遍历
    while(current != null){
    current.displayLink();
    current = current.next;
    }
    }

    }

    public class FirstLastList_demo {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    FirstLastList theList = new FirstLastList();
    theList.insertFirst(11, 11.1);
    theList.insertFirst(22, 22.2);
    theList.insertFirst(33, 33.3);
    theList.insertFirst(44, 44.4);
    theList.insertFirst(55, 55.5);
    theList.displayList();

    theList.deleteFirst();
    theList.deleteFirst();
    theList.deleteFirst();
    theList.displayList();

    }

    }

     

     

    Java数据结构——有序链表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    class SortedList{
    private Link_long first;

    public SortedList(){ //构造函数
    first = null;
    }

    public void insert(long key){
    Link_long newLink = new Link_long(key);
    Link_long previous = null; //上一次插入的值
    Link_long current = first; //每插入一次,current就重新赋为表头的值
    while(current != null && key > current.dData){ //没进入这里,pre就是null,也就只进入下面if的上一层
    previous = current;
    current = current.next; //current的位置往后移动
    }
    if(previous == null){ //最开始的情况,给first赋值为newLink,即key
    first = newLink;
    }else{ //
    previous.next = newLink;
    }
    newLink.next = current;
    }

    public long remove(){
    Link_long temp = first;
    first = first.next;
    return temp.dData;
    }

    public void displayList(){
    System.out.println("List (first-->last)");
    Link_long current = first;
    while(current != null){
    current.displayLink();
    current = current.next;
    }
    }

    }

    public class SortedList_demo {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    SortedList theSortedList = new SortedList();
    theSortedList.insert(10);
    theSortedList.insert(20);
    theSortedList.insert(30);
    theSortedList.displayList();
    theSortedList.remove();
    theSortedList.remove();
    theSortedList.displayList();
    }

    }

     

    Java数据结构——用双端链表实现队列

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    class FirstLastList_long{
    private Link_long first;
    private Link_long last;

    public FirstLastList_long() { //构造函数
    this.first = null;
    this.last = null;
    }

    public boolean isEmpty(){
    return (first == null);
    }

    public void insertFirst(long dd){ //从链表的头开始插入
    Link_long newLink = new Link_long(dd);
    if(isEmpty()){
    last = newLink; //不用改变first
    }
    newLink.next = first;
    first = newLink;
    }

    public void insertLast(long dd){ //从链表的尾开始插入
    Link_long newLink = new Link_long(dd);
    if(isEmpty()){
    first = newLink; //不用改变last
    }else{
    last.next = newLink; //在last后面添加新元素,并修改last的位置
    }
    last = newLink; //注意:只有一个元素的时候,插入要把last也赋为newLink
    }

    public long deleteFirst(){
    Link_long temp = first; //暂存first
    if(first.next == null){ //如果只有一个元素,把last也赋为null
    last = null;
    }
    first = first.next; //把next设为first
    return temp.dData; //返回原来的first
    }

    public void displayList(){
    System.out.println("List(first-->last):");
    Link_long current = first; //用于不断改变位置实现遍历
    while(current != null){
    current.displayLink();
    current = current.next;
    }
    }

    }

    class LinkQueue{
    private FirstLastList_long theList;

    public LinkQueue() { //构造函数
    theList = new FirstLastList_long(); //创建一个双端链表对象
    }

    public void push(long j){ //从链表的尾开始插入,新来的元素在尾部
    theList.insertLast(j);
    }

    public long pop(){
    return theList.deleteFirst(); //从链表的头开始弹出,先进的元素先被弹出
    }

    public boolean idEmpty(){
    return theList.isEmpty();
    }

    public void displayQueue(){
    System.out.println("Queue (front-->rear)");
    theList.displayList();
    }
    }

    public class LinkQueue_demo {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    LinkQueue theQueue = new LinkQueue();
    theQueue.push(10);
    theQueue.push(20);
    theQueue.push(30);
    theQueue.displayQueue();

    theQueue.pop();
    theQueue.pop();
    theQueue.displayQueue();
    }

    }

     

    Java数据结构——用链表实现栈

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    class Link_long{	//链节点类
    public long dData;
    public Link_long next; //链表中下一个节点的引用

    public Link_long(long dData) {
    super();
    this.dData = dData;
    }

    public void displayLink(){ //显示当前节点的值
    System.out.println("dData"+dData);
    }

    }

    class LinkList_long{
    private Link_long first; //只需要第一个节点,从第一个节点出发即可定位所有节点

    public LinkList_long() { //构造函数
    this.first = null;
    }

    public boolean isEmpty(){
    return (first == null);
    }

    public void insertFirst(long dd){ //插入元素是从链表的头开始插入
    Link_long newLink = new Link_long(dd);
    newLink.next = first;
    first = newLink;
    }

    public long deleteFirst(){ //删除temp.dData
    Link_long temp = first; //暂存first
    first = first.next; //把next设为first
    return temp.dData; //返回原来的first
    }

    public void displayList(){
    System.out.println("List(first-->last):");
    Link_long current = first; //用于不断改变位置实现遍历
    while(current != null){
    current.displayLink();
    current = current.next;
    }
    }

    public Link_long find(int key){ //查找指定的关键字
    Link_long current = first;
    while(current.dData != key){
    if(current.next == null)
    return null;
    else
    current = current.next;
    }
    return current;
    }

    public Link_long delete(int key){ //如果current的值匹配,则删除
    Link_long current = first;
    Link_long previous = first;
    //没有匹配到值
    while(current.dData != key){
    if(current.next == null)
    return null;
    else{ //pre和cur向后移动
    previous = current;
    current = current.next;
    }
    }
    //匹配到值
    if(current == first) //只有一个first,并匹配,则把first设成first.next
    first = first.next;
    else //current的值匹配,则删除,并把cur的next赋给pre的next
    previous.next = current.next;
    return current;
    }
    }

    class LinkStack{ //用链表实现栈,链表从First开始插入,新的元素将变成First,先删除后来元素

    private LinkList_long theList;

    public LinkStack() { //构造函数
    theList = new LinkList_long();
    }

    public void push(long j){ //入栈,即在链表的First插入元素
    theList.insertFirst(j);
    }

    public long pop(){ //出栈,即删除链表的First元素
    return theList.deleteFirst();
    }

    public boolean isEmpty(){ //判断栈是否为空,即判断链表是否为空
    return (theList.isEmpty());
    }

    public void displayStack(){
    System.out.println("Stack(top-->bottom):");
    theList.displayList();
    }

    }

    public class LinkStack_demo {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    LinkStack theStack = new LinkStack();
    theStack.push(10);
    theStack.push(20);
    theStack.push(30);
    theStack.displayStack();

    theStack.pop();
    theStack.pop();
    theStack.displayStack();
    }

    }

     

    Java数据结构——链表-单链表

    <1>链表

    <2>引用和基本类型

    <3>单链表

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    class Link{			//链节点类
    public int iData;
    public double dData;
    public Link next; //链表中下一个节点的引用

    public Link(int iData, double dData) {
    super();
    this.iData = iData;
    this.dData = dData;
    }

    public void displayLink(){ //显示当前节点的值
    System.out.println("iData="+iData+","+"dData"+dData);
    }

    }

    class LinkList{
    private Link first; //只需要第一个节点,从第一个节点出发即可定位所有节点

    public LinkList() { //构造函数
    this.first = null;
    }

    public boolean isEmpty(){
    return (first == null);
    }

    public void insertFirst(int id,double dd){ //插入元素是从链表的头开始插入
    Link newLink = new Link(id,dd);
    newLink.next = first;
    first = newLink;
    }

    public Link deleteFirst(){
    Link temp = first; //暂存first
    first = first.next; //把next设为first
    return temp; //返回原来的first
    }

    public void displayList(){
    System.out.println("List(first-->last):");
    Link current = first; //用于不断改变位置实现遍历
    while(current != null){
    current.displayLink();
    current = current.next;
    }
    }
    }

    public class LinkList_demo {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    LinkList theList = new LinkList();
    theList.insertFirst(11, 11.1);
    theList.insertFirst(22, 22.2);
    theList.insertFirst(33, 33.3);
    theList.insertFirst(44, 44.4);
    theList.insertFirst(55, 55.5);
    theList.displayList();
    while(!theList.isEmpty()){
    Link aLink = theList.deleteFirst();
    System.out.print("delete:");
    aLink.displayLink();
    }
    theList.displayList();
    }

    }

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    class Link{			//链节点类
    public int iData;
    public double dData;
    public Link next; //链表中下一个节点的引用

    public Link(int iData, double dData) {
    super();
    this.iData = iData;
    this.dData = dData;
    }

    public void displayLink(){ //显示当前节点的值
    System.out.println("iData="+iData+","+"dData"+dData);
    }

    }

    class LinkList{
    private Link first; //只需要第一个节点,从第一个节点出发即可定位所有节点

    public LinkList() { //构造函数
    this.first = null;
    }

    public boolean isEmpty(){
    return (first == null);
    }

    public void insertFirst(int id,double dd){ //插入元素是从链表的头开始插入
    Link newLink = new Link(id,dd);
    newLink.next = first;
    first = newLink;
    }

    public Link deleteFirst(){
    Link temp = first; //暂存first
    first = first.next; //把next设为first
    return temp; //返回原来的first
    }

    public void displayList(){
    System.out.println("List(first-->last):");
    Link current = first; //用于不断改变位置实现遍历
    while(current != null){
    current.displayLink();
    current = current.next;
    }
    }

    public Link find(int key){ //查找指定的关键字
    Link current = first;
    while(current.iData != key){
    if(current.next == null)
    return null;
    else
    current = current.next;
    }
    return current;
    }

    public Link delete(int key){ //如果current的值匹配,则删除
    Link current = first;
    Link previous = first;
    //没有匹配到值
    while(current.iData != key){
    if(current.next == null)
    return null;
    else{ //pre和cur向后移动
    previous = current;
    current = current.next;
    }
    }
    //匹配到值
    if(current == first) //只有一个first,并匹配,则把first设成first.next
    first = first.next;
    else //current的值匹配,则删除,并把cur的next赋给pre的next
    previous.next = current.next;
    return current;
    }
    }

    public class LinkList_demo {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    LinkList theList = new LinkList();
    theList.insertFirst(11, 11.1);
    theList.insertFirst(22, 22.2);
    theList.insertFirst(33, 33.3);
    theList.insertFirst(44, 44.4);
    theList.insertFirst(55, 55.5);
    theList.displayList();

    Link f = theList.find(22);
    if(f != null){
    System.out.print("找到:");
    f.displayLink();
    }
    else
    System.out.print("没有找到");

    Link d = theList.delete(32);
    if(d != null){
    System.out.print("删除:");
    d.displayLink();
    }
    else
    System.out.print("没有找到匹配的删除");

    }

    }

     

    Ubuntu16.04离线安装python3.8

    1.下载python3.8

    1
    2
    3
    cd ~/Download
    wget https://www.python.org/ftp/python/3.8.11/Python-3.8.11.tgz

    解压

    1
    2
    tar -zxvf Python-3.8.11.tgz

    2.创建目录

    1
    2
    3
    4
    5
    cd /usr/local
    sudo mkdir python
    cd ./python
    sudo mkdir python3.8

    3.编译安装

    1
    2
    3
    4
    ./configure prefix=/usr/local/python/python3.8 --enable-optimizations
    make -j 2
    make altinstall >&amp;1|tee make.log

    4.配置环境变量

    1
    2
    3
    # python
    export PATH=$PATH:/usr/local/python/python3.8/bin

    全文 >>