Skip to content

Commit 02962dc

Browse files
author
chenweijie
committed
数据结构-链表
1 parent d81a125 commit 02962dc

File tree

3 files changed

+141
-41
lines changed

3 files changed

+141
-41
lines changed

src/main/java/com/chen/dataStructure/linklistnode/SingleLinkedList.java

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* 单链表的具体实现
5+
*
56
* @author : chen weijie
67
* @Date: 2019-02-21 11:35 PM
78
*/
@@ -14,7 +15,6 @@ public class SingleLinkedList {
1415
private Node head;
1516

1617

17-
1818
private class Node {
1919

2020
//每个节点的数据
@@ -31,38 +31,33 @@ public Node(Object data) {
3131

3232
/**
3333
* 单链表的表头添加元素
34+
*
3435
* @param data
3536
* @return
3637
*/
3738
public Object addHead(Object data) {
3839

3940
Node newHead = new Node(data);
40-
4141
if (size == 0) {
4242
head = newHead;
4343
} else {
44-
//新头节点的下个节点是旧的head节点
4544
newHead.next = head;
46-
//新加入的节点为SingleLinkedList的头节点
4745
head = newHead;
4846
}
49-
5047
size++;
51-
return data;
48+
49+
return head;
5250
}
5351

5452

5553
/**
5654
* 删除链表头节点
55+
*
5756
* @return
5857
*/
5958
public Object deleteHead() {
60-
6159
Object data = head.data;
62-
63-
//新头节点为旧头节点的下个节点
6460
head = head.next;
65-
6661
size--;
6762
return data;
6863
}
@@ -75,37 +70,55 @@ public Object deleteHead() {
7570
*/
7671
public Node find(Object data) {
7772

78-
Node curr = head;
79-
//只是查找,不改变链表的个数,所以要新建tempSize变量
80-
int tempSize = size;
73+
Node current = head;
8174

75+
int tempSize = size;
8276
while (tempSize > 0) {
83-
if (data.equals(curr.data)) {
84-
return curr;
77+
if (data.equals(current.data)) {
78+
return current;
79+
} else {
80+
current = current.next;
81+
tempSize--;
8582
}
86-
curr = curr.next;
87-
tempSize--;
83+
8884
}
8985
return null;
86+
9087
}
9188

9289
/**
9390
* 删除指定元素,删除成功返回true
91+
*
9492
* @param data
9593
* @return
9694
*/
97-
public boolean delete(Object data){
98-
99-
100-
101-
102-
103-
104-
105-
95+
public boolean delete(Object data) {
10696

97+
if (size == 0) {
98+
return true;
99+
}
100+
Node current = head;
101+
Node previous = head;
102+
103+
while (current.data != data) {
104+
if (current.next == null) {
105+
return false;
106+
} else {
107+
previous = current;
108+
current = current.next;
109+
}
110+
}
107111

108-
return false;
112+
//如果删除的节点是第一个节点
113+
if (current == head) {
114+
head = current.next;
115+
size--;
116+
} else {
117+
//删除的节点不是第一个节点
118+
previous.next = current.next;
119+
size--;
120+
}
121+
return true;
109122
}
110123

111124

@@ -119,6 +132,59 @@ public boolean isEmpty() {
119132
}
120133

121134

135+
//显示节点信息
136+
public void display() {
137+
if (size > 0) {
138+
Node node = head;
139+
int tempSize = size;
140+
//当前链表只有一个节点
141+
if (tempSize == 1) {
142+
System.out.println("[" + node.data + "]");
143+
return;
144+
}
145+
while (tempSize > 0) {
146+
if (node.equals(head)) {
147+
System.out.print("[" + node.data + "->");
148+
} else if (node.next == null) {
149+
System.out.print(node.data + "]");
150+
} else {
151+
System.out.print(node.data + "->");
152+
}
153+
node = node.next;
154+
tempSize--;
155+
}
156+
System.out.println();
157+
} else {
158+
//如果链表一个节点都没有,直接打印[]
159+
System.out.println("[]");
160+
}
161+
162+
}
163+
164+
165+
public static void main(String[] args) {
166+
167+
SingleLinkedList singleLinkedList = new SingleLinkedList();
168+
singleLinkedList.addHead("A");
169+
singleLinkedList.addHead("B");
170+
singleLinkedList.addHead("C");
171+
singleLinkedList.addHead("D");
172+
173+
singleLinkedList.display();
174+
175+
singleLinkedList.delete("B");
176+
177+
singleLinkedList.display();
178+
179+
System.out.println("find:" + singleLinkedList.find("D"));
180+
181+
182+
}
183+
184+
185+
186+
187+
122188

123189

124190

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.dataStructure.linklistnode;
2+
3+
/**
4+
* 用单向链表实现栈
5+
* 栈的pop()方法和push()方法,对应于链表的在头部删除元素deleteHead()以及在头部增加元素addHead()。
6+
*
7+
* @author : chen weijie
8+
* @Date: 2019-03-08 12:57 AM
9+
*/
10+
public class StackSingleLink {
11+
12+
13+
private SingleLinkedList linkedList;
14+
15+
16+
public StackSingleLink(SingleLinkedList linkedList) {
17+
this.linkedList = linkedList;
18+
}
19+
20+
public void push(Object data) {
21+
linkedList.addHead(data);
22+
}
23+
24+
25+
public Object pop() {
26+
return linkedList.deleteHead();
27+
}
28+
29+
30+
public boolean isEmpty() {
31+
return linkedList.isEmpty();
32+
}
33+
34+
35+
}

src/main/java/com/chen/dataStructure/linknode/LinkNodeTest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public static void main(String[] args) {
2323
node2.setNext(node3);
2424

2525
//打印反转前的列表
26-
Node h =head;
27-
while (null!=h){
28-
System.out.println(h.getData()+" ");
29-
h =h.getNext();
26+
Node h = head;
27+
while (null != h) {
28+
System.out.println(h.getData() + " ");
29+
h = h.getNext();
3030
}
3131

3232
// 调用反转方法
@@ -42,31 +42,30 @@ public static void main(String[] args) {
4242
}
4343

4444

45-
46-
4745
/**
4846
* 遍历,将当前节点的下一个节点缓存后更改当前节点指针
4947
*/
50-
public static Node reverse(Node head){
48+
public static Node reverse(Node head) {
5149

52-
if (head ==null)
50+
if (head == null) {
5351
return head;
52+
}
5453

5554
//上一节点
56-
Node pre =head;
55+
Node pre = head;
5756
//当前节点
58-
Node cur =head.getNext();
57+
Node cur = head.getNext();
5958
//临时节点 用于保存当前节点的指针域(即下一节点)
6059
Node tmp;
6160

6261
//当前节点为null 说明位于尾节点
63-
while (cur!=null){
64-
tmp =cur.getNext();
62+
while (cur != null) {
63+
tmp = cur.getNext();
6564
//反转指针域的指向
6665
cur.setNext(pre);
6766
//指针向下移动
68-
pre =cur;
69-
cur =tmp;
67+
pre = cur;
68+
cur = tmp;
7069
}
7170
// 最后将原链表的头节点的指针域置为null,还回新链表的头结点,即原链表的尾结点
7271
head.setNext(null);

0 commit comments

Comments
 (0)