数据结构 链表
- 本来正常的 都要 理论一大堆 链表是一种物理存储单元上非连续、非顺序的存储结构… 巴拉巴拉 一大堆
- 但是嘛 我偏不 直接上代码
- 项目 用的 普通的 MAVEN Java 项目
单向链表
- 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
package com.codervibe.ch01;
public class OneWayLinkList<T> {
public Node head;
public int length;
private class Node {
T item;
Node next;
public Node() {
}
public Node(T item, Node next) {
this.item = item;
this.next = next;
}
}
public OneWayLinkList() {
this.head = new Node();
this.length = 0;
}
public void clear() {
this.head = null;
this.length = 0;
}
public int length() {
return length;
}
public boolean isEmpty() {
return length == 0;
}
public T get(int i) {
Node node = head;
for (int j = 0; j <= i; j++) {
node = node.next;
}
return node.item;
}
public void insert(T t) {
Node last = head;
while (last.next != null) {
last = last.next;
}
Node now = new Node(t, null);
last.next = now;
this.length++;
}
public void insert(int i, T t) {
Node pre = head;
for (int j = 0; j < i; j++) {
pre = pre.next;
}
Node cur = pre.next;
Node now = new Node(t, cur);
pre.next = now;
this.length++;
}
public T remove(int i) {
Node pre = head;
for (int j = 0; j < i; j++) {
head = pre.next;
}
Node cur = pre.next;
Node next = cur.next;
pre.next =next;
this.length--;
return cur.item;
}
public int indexOf(T t){
Node now =head;
for (int i = 0; now.next != null; i++) {
now =now.next;
if (now.item == t) {
return i;
}
}
return - 1;
}
}
@Test
public void OneWayLinkListTest() {
OneWayLinkList<String> oneWayLinkList =new OneWayLinkList<>();
oneWayLinkList.insert("孙悟空");
oneWayLinkList.insert("猪八戒");
oneWayLinkList.insert("貂蝉");
oneWayLinkList.insert("吕布");
oneWayLinkList.insert("老夫子");
oneWayLinkList.insert("盘古");
oneWayLinkList.insert("后羿");
oneWayLinkList.insert("扁鹊");
for (int i = 0; i < oneWayLinkList.length(); i++) {
System.out.println(oneWayLinkList.get(i));
}
System.out.println("当前的链表长度为:" + oneWayLinkList.length());
System.out.println("-------------------------------------");
String getLinkList = oneWayLinkList.get(5);
System.out.println("获取索引的位置的结果为:"+getLinkList);
String removeItem= oneWayLinkList.remove(4);
System.out.println("删除的元素为 " + removeItem+"\t"+"删除后的长度为"+ oneWayLinkList.length());
System.out.println("扁鹊 第一次出现的索引为 " + oneWayLinkList.indexOf("扁鹊"));
oneWayLinkList.clear();
System.out.println("清空后链表中元素的个数为"+ oneWayLinkList.length());
}
双向链表
package com.codervibe.ch01;
public class TwoWayLinkList<T> {
private Node head;
private Node last;
private int length;
private class Node {
public T item;
public Node pre;
public Node next;
public Node(T item, Node pre, Node next) {
this.item = item;
this.pre = pre;
this.next = next;
}
}
public TwoWayLinkList() {
this.head = new Node(null, null, null);
this.last = null;
this.length = 0;
}
public void clear() {
this.head.next = null;
this.last = null;
this.length = 0;
}
public int length() {
return this.length;
}
public boolean isEmpty() {
return length == 0;
}
public T getFileItem() {
if (isEmpty()) {
return null;
}
return head.next.item;
}
public T getLastItem() {
if (isEmpty()) {
return null;
}
return last.item;
}
public void insert(T t) {
if (isEmpty()) {
Node newNode = new Node(t, head, null);
last = newNode;
head.next = last;
} else {
Node newNode = new Node(t, last, null);
last.next = newNode;
last = newNode;
}
this.length++;
}
public void insert(int i, T t) {
Node pre = head;
for (int j = 0; j < i; j++) {
pre = pre.next;
}
Node cur = pre.next;
Node newNode = new Node(t, pre, cur);
pre.next = newNode;
cur.pre = newNode;
this.length++;
}
public T getItem(int i) {
Node node = head;
for (int j = 0; j < i; j++) {
node = node.next;
}
return node.item;
}
public int indexOf(T t) {
Node now = head;
for (int i = 0; now.next != null; i++) {
now = now.next;
if (now.item == t) {
return i;
}
}
return -1;
}
public T remove(int i){
Node pre = head;
for (int j = 0; j < i; j++) {
pre =pre.next;
}
Node cur = pre.next;
Node nextNode = cur.next;
pre.next = nextNode;
nextNode.pre = pre;
this.length--;
return cur.item;
}
}
@Test
public void TwoWayLinkListTest(){
TwoWayLinkList<String> twoWayLinkList=new TwoWayLinkList<>();
twoWayLinkList.insert("孙悟空");
twoWayLinkList.insert("猪八戒");
twoWayLinkList.insert("貂蝉");
twoWayLinkList.insert("吕布");
twoWayLinkList.insert("老夫子");
twoWayLinkList.insert("盘古");
twoWayLinkList.insert("后羿");
twoWayLinkList.insert("扁鹊");
for (int i = 0; i < twoWayLinkList.length(); i++) {
System.out.println(twoWayLinkList.getItem(i));
}
System.out.println("当前列表的长度为:" + twoWayLinkList.length());
System.out.println("----------------------------------------------------------");
System.out.println("获取索引位置的元素为" + twoWayLinkList.getItem(6));
System.out.println("删除的元素为 " + twoWayLinkList.remove(4)+ "删除后的 链表长度为:"+twoWayLinkList.length());
System.out.println("吕布 出现的 第一次索引为 " + twoWayLinkList.indexOf("吕布"));
twoWayLinkList.insert(4,"李元芳");
System.out.println("插入新元素之后 链表为");
for (int i = 0; i < twoWayLinkList.length(); i++) {
System.out.println(twoWayLinkList.getItem(i));
}
System.out.println("清空链表~~~");
twoWayLinkList.clear();
System.out.println("链表清空后的 的链表长度为 :" + twoWayLinkList.length());
}
两个类的 全部代码
package com.codervibe.ch01;
public class OneWayLinkList<T> {
public Node head;
public int length;
private class Node {
T item;
Node next;
public Node() {
}
public Node(T item, Node next) {
this.item = item;
this.next = next;
}
}
public OneWayLinkList() {
this.head = new Node();
this.length = 0;
}
public void clear() {
this.head = null;
this.length = 0;
}
public int length() {
return length;
}
public boolean isEmpty() {
return length == 0;
}
public T get(int i) {
Node node = head;
for (int j = 0; j <= i; j++) {
node = node.next;
}
return node.item;
}
public void insert(T t) {
Node last = head;
while (last.next != null) {
last = last.next;
}
Node now = new Node(t, null);
last.next = now;
this.length++;
}
public void insert(int i, T t) {
Node pre = head;
for (int j = 0; j < i; j++) {
pre = pre.next;
}
Node cur = pre.next;
Node now = new Node(t, cur);
pre.next = now;
this.length++;
}
public T remove(int i) {
Node pre = head;
for (int j = 0; j < i; j++) {
head = pre.next;
}
Node cur = pre.next;
Node next = cur.next;
pre.next =next;
this.length--;
return cur.item;
}
public int indexOf(T t){
Node now =head;
for (int i = 0; now.next != null; i++) {
now =now.next;
if (now.item == t) {
return i;
}
}
return - 1;
}
}
package com.codervibe.ch01;
public class TwoWayLinkList<T> {
private Node head;
private Node last;
private int length;
private class Node {
public T item;
public Node pre;
public Node next;
public Node(T item, Node pre, Node next) {
this.item = item;
this.pre = pre;
this.next = next;
}
}
public TwoWayLinkList() {
this.head = new Node(null, null, null);
this.last = null;
this.length = 0;
}
public void clear() {
this.head.next = null;
this.last = null;
this.length = 0;
}
public int length() {
return this.length;
}
public boolean isEmpty() {
return length == 0;
}
public T getFileItem() {
if (isEmpty()) {
return null;
}
return head.next.item;
}
public T getLastItem() {
if (isEmpty()) {
return null;
}
return last.item;
}
public void insert(T t) {
if (isEmpty()) {
Node newNode = new Node(t, head, null);
last = newNode;
head.next = last;
} else {
Node newNode = new Node(t, last, null);
last.next = newNode;
last = newNode;
}
this.length++;
}
public void insert(int i, T t) {
Node pre = head;
for (int j = 0; j < i; j++) {
pre = pre.next;
}
Node cur = pre.next;
Node newNode = new Node(t, pre, cur);
pre.next = newNode;
cur.pre = newNode;
this.length++;
}
public T getItem(int i) {
Node node = head;
for (int j = 0; j < i; j++) {
node = node.next;
}
return node.item;
}
public int indexOf(T t) {
Node now = head;
for (int i = 0; now.next != null; i++) {
now = now.next;
if (now.item == t) {
return i;
}
}
return -1;
}
public T remove(int i){
Node pre = head;
for (int j = 0; j < i; j++) {
pre =pre.next;
}
Node cur = pre.next;
Node nextNode = cur.next;
pre.next = nextNode;
nextNode.pre = pre;
this.length--;
return cur.item;
}
}
package com.codervibe.ch01;
import org.junit.Test;
public class LinkListTest {
@Test
public void OneWayLinkListTest() {
OneWayLinkList<String> oneWayLinkList =new OneWayLinkList<>();
oneWayLinkList.insert("孙悟空");
oneWayLinkList.insert("猪八戒");
oneWayLinkList.insert("貂蝉");
oneWayLinkList.insert("吕布");
oneWayLinkList.insert("老夫子");
oneWayLinkList.insert("盘古");
oneWayLinkList.insert("后羿");
oneWayLinkList.insert("扁鹊");
for (int i = 0; i < oneWayLinkList.length(); i++) {
System.out.println(oneWayLinkList.get(i));
}
System.out.println("当前的链表长度为:" + oneWayLinkList.length());
System.out.println("-------------------------------------");
String getLinkList = oneWayLinkList.get(5);
System.out.println("获取索引的位置的结果为:"+getLinkList);
String removeItem= oneWayLinkList.remove(4);
System.out.println("删除的元素为 " + removeItem+"\t"+"删除后的长度为"+ oneWayLinkList.length());
System.out.println("扁鹊 第一次出现的索引为 " + oneWayLinkList.indexOf("扁鹊"));
oneWayLinkList.clear();
System.out.println("清空后链表中元素的个数为"+ oneWayLinkList.length());
}
@Test
public void TwoWayLinkListTest(){
TwoWayLinkList<String> twoWayLinkList=new TwoWayLinkList<>();
twoWayLinkList.insert("孙悟空");
twoWayLinkList.insert("猪八戒");
twoWayLinkList.insert("貂蝉");
twoWayLinkList.insert("吕布");
twoWayLinkList.insert("老夫子");
twoWayLinkList.insert("盘古");
twoWayLinkList.insert("后羿");
twoWayLinkList.insert("扁鹊");
for (int i = 0; i < twoWayLinkList.length(); i++) {
System.out.println(twoWayLinkList.getItem(i));
}
System.out.println("当前列表的长度为:" + twoWayLinkList.length());
System.out.println("----------------------------------------------------------");
System.out.println("获取索引位置的元素为" + twoWayLinkList.getItem(6));
System.out.println("删除的元素为 " + twoWayLinkList.remove(4)+ "删除后的 链表长度为:"+twoWayLinkList.length());
System.out.println("吕布 出现的 第一次索引为 " + twoWayLinkList.indexOf("吕布"));
twoWayLinkList.insert(4,"李元芳");
System.out.println("插入新元素之后 链表为");
for (int i = 0; i < twoWayLinkList.length(); i++) {
System.out.println(twoWayLinkList.getItem(i));
}
System.out.println("清空链表~~~");
twoWayLinkList.clear();
System.out.println("链表清空后的 的链表长度为 :" + twoWayLinkList.length());
}
}