双向链表(华强买瓜)

情景


有一个人前来找茬买瓜

不是,我们今天是瓜摊老板

用双向链表来存储整个瓜摊

创建节点

首先,我们需要创建节点,这次摆瓜摊,节点就是每一个西瓜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Watermelon {
public int location;
public Ripe ripe;
public Watermelon pre;
public Watermelon next;

public Watermelon(int location, String isRipe) {
this.location = location;
this.ripe = Ripe.valueOf(isRipe);
}

@Override
public String toString() {
return "Watermelon{" +
"location=" + location +
", ripe=" + ripe +
'}';
}
}

重写toString方法方便后续输出西瓜信息
location是西瓜在瓜摊上占的位置,ripe是枚举类,只能有三种对象

1
2
3
4
5
enum Ripe{
熟瓜,
生瓜蛋子,
头结点无瓜;
}

读者不熟悉枚举类的话可以直接用布尔型变量来表示,true表示熟瓜,false表示生瓜,头结点存一个空值null就行

现在我们正式开始写瓜摊(链表)

1
2
3
4
5
6
7
8
class Stall{
/*增:摆西瓜
* 插入:按编号插入
* 删:卖西瓜
* 改:偷换一个西瓜
* 查:查看成熟程度
* */
}

我们的通过实际情况来练习双向链表增删改查

定义好头结点

1
private Watermelon head = new Watermelon(0,"头结点无瓜");

在结尾位置添加一个西瓜,也是通过遍历来实现,遍历到最后一个位置,再添加新的瓜,新的瓜的pre指向最后一个瓜,最后一个瓜的next指向新的瓜

1
2
3
4
5
6
7
8
9
10
public void add (Watermelon newWatermelon){
Watermelon temp = head;
while(true){
if (temp.next == null)
break;
temp = temp.next;
}
newWatermelon.pre = temp;
temp.next = newWatermelon;
}

在中间空位置插入瓜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void addByOrder(Watermelon newWatermelon){
Watermelon temp = head;
boolean ifExist = false;
while (true){
if (temp.location == newWatermelon.location)//这个位置已经有瓜占了
break;
if (temp.location > newWatermelon.location){//找到了合适位置的后一个
ifExist = true;
break;
}
temp = temp.next;
}
if (ifExist){
newWatermelon.pre = temp.pre;
temp.pre.next = newWatermelon;
temp.pre = newWatermelon;
newWatermelon.next = temp;
}
else {
System.out.println("这个位置已经有瓜啦");
}
}

一眼看出是个不懂瓜的人或者懂瓜的人,随机应变改变显眼位置的瓜

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
public void exchange (Watermelon newWatermelon){
if (head.next == null){
System.out.println("今天的瓜已经卖完了");
return;
}
Watermelon temp = head;
boolean ifExist = false;//标记一下,该位置有没有瓜,有瓜再换,没瓜也不用换
while (true){
if (temp == null)
break;
if (temp.location == newWatermelon.location){
ifExist = true;//该位置存在瓜
break;
}
temp = temp.next;
}
if (ifExist){
temp.pre.next = newWatermelon;
temp.next.pre = newWatermelon;
newWatermelon.pre = temp.pre;
newWatermelon.next = temp.next;
}
else {
System.out.println("该位置本来就没有瓜,不用换");
}
}

对面的人凶神恶煞还拿着刀,可以拿走不熟的生瓜蛋子,防止被捅

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void takeAway(int location){
if (head.next == null){
System.out.println("今天的瓜已经卖完了");
return;
}
Watermelon temp = head;
boolean ifExist = false;//标记一下,假如该位置没有瓜,本来就不用拿走
while(true){
if (temp.next == null)//没找到位置
break;
if (temp.location == location){
ifExist = true;//该位置存在瓜
break;
}
temp = temp.next;
}
if (ifExist){
temp.pre.next = temp.next;
temp.next.pre = temp.pre;
}
else{
System.out.println("该位置本来就没有瓜,不用拿走");
}
}

查看瓜摊情况

1
2
3
4
5
6
7
8
9
10
11
12
13
public void show(){
if (head == null){
System.out.println("今天的瓜已经卖完了");
return;
}
Watermelon temp = head.next;
while(true){
if (temp == null)
break;
System.out.println(temp);
temp = temp.next;
}
}

写个主类

好,现在瓜摊的几个功能写完了,只需要随便写个主类,加几个功能就可以啦~

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
public class DoubleLinkedList {
public static void main(String[] args) {
System.out.println("摆一个地摊");
Stall stall = new Stall();
System.out.println("放几个西瓜,趁人不注意,塞俩生瓜蛋子");
stall.add(new Watermelon(1,"熟瓜"));
stall.add(new Watermelon(2,"熟瓜"));
stall.add(new Watermelon(3,"熟瓜"));
stall.add(new Watermelon(4,"熟瓜"));
stall.add(new Watermelon(5,"熟瓜"));
stall.add(new Watermelon(6,"生瓜蛋子"));
stall.add(new Watermelon(7,"生瓜蛋子"));
stall.add(new Watermelon(8,"熟瓜"));
stall.add(new Watermelon(9,"熟瓜"));
stall.add(new Watermelon(10,"熟瓜"));
Scanner function = new Scanner(System.in);
Scanner scanner = new Scanner(System.in);
boolean stop = true;
System.out.println("0:收摊\n" +
"1:末尾摆一个瓜\n" +
"2:中间位置插入一个瓜\n" +
"3:来了个新手,换上一个生瓜蛋子\n" +
"4:来了个找茬儿的,拿走一个生瓜蛋子\n" +
"5:查看瓜摊情况");
while(stop){
switch(function.nextInt()){
case 0:
stop = false;
System.out.println("收摊走人~");
break;
case 1:
System.out.println("输入瓜的位置和成熟程度");
stall.add(new Watermelon(scanner.nextInt(),scanner.next()));
break;
case 2:
System.out.println("输入瓜的位置和成熟程度");
stall.addByOrder(new Watermelon(scanner.nextInt(),scanner.next()));
break;
case 3:
System.out.println("输入瓜的位置和成熟程度");
stall.exchange(new Watermelon(scanner.nextInt(),scanner.next()));
break;
case 4:
System.out.println("输入瓜的位置和成熟程度");
stall.takeAway(scanner.nextInt());
break;
case 5:
stall.show();
break;
}
}
}
}

读者看完可以自行编写实现功能或者在公众号获取完整源码,尝试做一天瓜摊老板吧~

微信公众号“一只达瓦里氏”后台回复0816获取完整源码


双向链表(华强买瓜)
http://example.com/双向链表(华强买瓜)/
作者
李小基
许可协议