其他分享
首页 > 其他分享> > 基本数据结构的简单实现

基本数据结构的简单实现

作者:互联网

基本数据结构的简单实现

顺序表

 1 /**
 2  * 顺序表
 3  */
 4 @SuppressWarnings("all")
 5 public class SequenceList<T> {
 6     private final Object[] data;
 7     
 8     public SequenceList(int length) {
 9         this.data = new Object[length];
10     }
11     
12     public int length() {
13         return data.length;
14     }
15     
16     public T get(int index) {
17         return (T) data[index];
18     }
19     
20     public void set(int index, T data) {
21         this.data[index] = data;
22     }
23     
24     public void delete(int index) {
25         this.data[index] = null;
26     }
27     
28     public int find(T item) {
29         for (int i = 0; i < data.length; i++) {
30             if (data[i].equals(item)) {
31                 return i;
32             }
33         }
34         return -1;
35     }
36     
37     public void print() {
38         System.out.print("[");
39         System.out.print(data[0]);
40         for (int i = 1; i < data.length; i++) {
41             if (data[i] == null) {
42                 System.out.print(", _");
43             } else {
44                 System.out.print(", " + data[i]);
45             }
46         }
47         System.out.print("]\n");
48     }
49 }

单向链表

 1 /**
 2  * 单向链表
 3  */
 4 @SuppressWarnings("all")
 5 public class SinglyLinkedList<T> {
 6     private Node<T> head;
 7     private int length;
 8     
 9     public SinglyLinkedList() {
10     }
11     
12     public int length() {
13         return length;
14     }
15     
16     public T get(int index) {
17         Node<T> node = head;
18         for (int i = 0; i < index; i++) {
19             node = node.next;
20         }
21         return node.data;
22     }
23     
24     public void set(int index, T data) {
25         Node<T> node = head;
26         for (int i = 0; i < index; i++) {
27             node = node.next;
28         }
29         node.data = data;
30     }
31     
32     public void insert(int index, T data) {
33         if (index == 0) {
34             if (length == 0) {
35                 this.head = new Node<>(null, data);
36             } else {
37                 this.head = new Node<>(this.head, data);
38             }
39         } else {
40             Node<T> p = head; // 插入节点的前驱
41             for (int i = 1; i < index; i++) {
42                 p = p.next;
43             }
44             if (index == length) {
45                 p.next = new Node<>(null, data);
46             } else {
47                 p.next = new Node<>(p.next, data);
48             }
49         }
50         this.length++;
51     }
52     
53     public void delete(int index) {
54         if (index == 0) {
55             head = head.next;
56         } else {
57             Node<T> p = head; // 删除节点的前驱
58             for (int i = 1; i < index; i++) {
59                 p = p.next;
60             }
61             p.next = p.next.next;
62         }
63         this.length--;
64     }
65     
66     public int find(T data) {
67         Node<T> node = head;
68         int index = 0;
69         while (node != null) {
70             if (node.data.equals(data)) {
71                 return index;
72             }
73             index++;
74             node = node.next;
75         }
76         return -1;
77     }
78     
79     public void print() {
80         Node<T> p = head;
81         while (p != null) {
82             System.out.print(p.data + " => ");
83             p = p.next;
84         }
85         System.out.print("null\n");
86     }
87     
88     private static class Node<T> {
89         Node<T> next;
90         T data;
91         
92         Node(Node<T> next, T data) {
93             this.next = next;
94             this.data = data;
95         }
96     }
97 }

双向链表

  1 /**
  2  * 双向链表
  3  */
  4 @SuppressWarnings("all")
  5 public class DoubleLinkedLis<T> {
  6     private Node<T> head;
  7     private int length;
  8     
  9     public DoubleLinkedLis() {
 10     }
 11     
 12     public int length() {
 13         return length;
 14     }
 15     
 16     public T get(int index) {
 17         Node<T> node = head;
 18         for (int i = 0; i < index; i++) {
 19             node = node.next;
 20         }
 21         return node.data;
 22     }
 23     
 24     public void set(int index, T data) {
 25         Node<T> node = head;
 26         for (int i = 0; i < index; i++) {
 27             node = node.next;
 28         }
 29         node.data = data;
 30     }
 31     
 32     public void insert(int index, T data) {
 33         if (index == 0) {
 34             if (length == 0) {
 35                 this.head = new Node(null, null, data);
 36             } else {
 37                 this.head = new Node(null, this.head, data);
 38             }
 39         } else {
 40             Node<T> p = head; // 插入节点的前驱
 41             for (int i = 1; i < index; i++) {
 42                 p = p.next;
 43             }
 44             if (index == length) {
 45                 p.next = new Node<>(p, null, data);
 46             } else {
 47                 Node<T> node = new Node<>(p, p.next, data);
 48                 node.next.prev = node;
 49                 node.prev.next = node;
 50             }
 51         }
 52         this.length++;
 53     }
 54     
 55     public void delete(int index) {
 56         if (index == 0) {
 57             head = head.next;
 58             head.prev = null;
 59         } else {
 60             Node<T> p = head; // 删除节点的前驱
 61             for (int i = 1; i < index; i++) {
 62                 p = p.next;
 63             }
 64             p.next = p.next.next;
 65             if (p.next != null) {
 66                 p.next.prev = p;
 67             }
 68         }
 69         this.length--;
 70     }
 71     
 72     public int find(T data) {
 73         Node<T> node = head;
 74         int index = 0;
 75         while (node != null) {
 76             if (node.data.equals(data)) {
 77                 return index;
 78             }
 79             index++;
 80             node = node.next;
 81         }
 82         return -1;
 83     }
 84     
 85     public T prev(T data) {
 86         Node<T> node = head;
 87         while (node != null) {
 88             if (node.data.equals(data)) {
 89                 if (node.prev == null) {
 90                     return null;
 91                 } else {
 92                     return node.prev.data;
 93                 }
 94             }
 95             node = node.next;
 96         }
 97         return null;
 98     }
 99     
100     public T next(T data) {
101         Node<T> node = head;
102         while (node != null) {
103             if (node.data.equals(data)) {
104                 if (node.next == null) {
105                     return null;
106                 } else {
107                     return node.next.data;
108                 }
109             }
110             node = node.next;
111         }
112         return null;
113     }
114     
115     public void print() {
116         Node<T> p = head;
117         System.out.print("null <=> ");
118         while (p != null) {
119             System.out.print(p.data + " <=> ");
120             p = p.next;
121         }
122         System.out.print("null\n");
123     }
124     
125     private static class Node<T> {
126         Node<T> prev;
127         Node<T> next;
128         T data;
129         
130         Node(Node<T> prev, Node<T> next, T data) {
131             this.prev = prev;
132             this.next = next;
133             this.data = data;
134         }
135     }
136 }

 

标签:基本,node,index,int,next,简单,数据结构,data,Node
来源: https://www.cnblogs.com/kkelin/p/16686200.html