服务器之家

服务器之家 > 正文

Java实现双链表互相交换任意两个节点的方法示例

时间:2021-02-03 11:19     来源/作者:Dzwsss

本文实例讲述了Java实现双链表互相交换任意两个节点的方法。分享给大家供大家参考,具体如下:

概述:

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。

思路:

1、确定两个节点的先后顺序
2、next、prev互相交换顺序以及将换向前方的节点与之前的节点对接。(1.prev.next = 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
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
public class SLink {
  public SNode head;
  public SLink() {
    this.head = new SNode();
  }
  public boolean interChangeById(int id1,int id2) {
    SNode s = head;
    SNode node1 = null,node2 = null;
    int node1Count,node2Count;
    node1Count = node2Count = 0;
    if(id1 == id2) {
      return true;
    }
    /**
     * 向下遍历
     */
    while (s.next != null) {
      s = s.next;
      node1Count ++ ;
      if(s.student.stuId == id1) {
        /**
         * 记录节点1
         */
        node1 = s;
        break;
      }
    }
    s = head;
    while (s.next != null) {
      s = s.next;
      node2Count ++ ;
      if(s.student.stuId == id2) {
        /**
         * 记录节点2
         */
        node2 = s;
        break;
      }
    }
    if(node1 != null && node2 != null) {
      SNode temp = new SNode();
      /**
       * node1在后
       */
      if(node1Count > node2Count) {
        temp.next = node1.next;
        temp.prev = node1.prev;
        /**
         * 记录那个节点就先处理那个节点
         * 1、交换next
         * 2、交换prev
         * 3、设置之前节点的next(对接)
         */
        node1.next = node2.next;
        node1.prev = node2.prev;
        node2.prev.next = node1;
        if(node1.next.equals(node1)) {
          /**
           * 说明两个节点相邻
           */
          node1.next = node2;
          node2.next = temp.next;
          node2.prev = node1;
        }else {
          /**
           * 说明节点不相邻
           */
          node2.next = temp.next;
          node2.prev = temp.prev;
          temp.prev.next = node2;
        }
      }else {
        /**
         * node1在前
         */
        temp.next = node2.next;
        temp.prev = node2.prev;
        node2.next = node1.next;
        node2.prev = node1.prev;
        node1.prev.next = node2;
        if(node2.next.equals(node2)) {
          node2.next = node1;
          node1.next = temp.next;
          node1.prev = node2;
        }else {
          node1.next = temp.next;
          node1.prev = temp.prev;
          temp.prev.next = node1;
        }
      }
      return true;
    }
    return false;
  }
  public void displayStudent() {
    SNode s = head;
    while (s.next != null) {
      s = s.next;
      System.out.println(s.student);
    }
  }
}

节点类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class SNode {
  public Student student;
  public SNode next;
  public SNode prev;
  public SNode(Student student, SNode prev,SNode next) {
    this.student = student;
    this.next = next;
    this.prev = prev;
  }
  public SNode() {
    this.student = null;
    this.next = null;
    this.prev = null;
  }
}

Student类:

?
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
public class Student {
  public int stuId;
  public String name;
  public int age;
  public String className;
  public Student(int stuId, String name, int age, String className) {
    this.stuId = stuId;
    this.name = name;
    this.age = age;
    this.className = className;
  }
  public int getStuId() {
    return stuId;
  }
  public void setStuId(int stuId) {
    this.stuId = stuId;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getClassName() {
    return className;
  }
  public void setClassName(String className) {
    this.className = className;
  }
  @Override
  public String toString() {
    return "Student{" +
        "stuId=" + stuId +
        ", name='" + name + '\'' +
        ", age=" + age +
        ", className='" + className + '\'' +
        '}';
  }
}

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/u010369338/article/details/71640467

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部