您的位置:首页 > 编程语言 > Java开发

Data structure-3 单链表LinkedList--Java语言实现

2017-07-20 20:53 579 查看

1. 链表特点

One disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set. Arrays are also expensive to maintain new insertions and deletions. In this chapter we consider another data structure called Linked Lists that addresses some of the limitations of arrays.

A linked list is a linear data structure where each element is a separate object.

链表是相对于数组这种数据结构存在的,弥补了数组的部分缺点,同时链表的缺点是数组的主要特点。

特点:链表是动态的数据结构,可以动态扩展,对于插入删除的开销较少。缺点是–对于数据的查找无法直接访问;且所占用内存空间相对于数组较多。



2. 单链表 Java实现

//LinkedList.java
package com.fqyuan.linkedlist;

/*
* Version with dummy node or not.
* This is one without a dummy node.
*/
public class LinkedList<T> {
private Node head;

public LinkedList() {
head = new Node();
head.value = null;
head.link = null;
}

public boolean isEmpty() {
return head.value == null;
}

// Insert before the head node.
public boolean insertAtStart(T item) {
if (!isEmpty()) {
Node node = new Node();
node.value = item;
node.link = head;
head = node;
return true;
} else {
head.value = item;
head.link = null;
return true;
}
}

// Insert after the end node.
public boolean insertAtEnd(T item) {
// First find the position to insert after
if (!isEmpty()) {
Node node = head;
while (node.link != null)
node = node.link;
// Then 'node' is the last element of the list
Node newNode = new Node();
newNode.value = item;
newNode.link = null;

node.link = newNode;

return true;
} else {
head.value = item;
head.link = null;
return true;
}
}

// Insert after a given position.
public boolean insertAtPos(int pos, T item) {
if (pos > size())
return false;
/*
* First find the position to insert. The position to insert will be 1
* to size.
*/
Node pNode = head;
for (int i = 0; i < pos - 1; i++) {
pNode = pNode.link;
}
Node qNode = pNode.link;

Node newNode = new Node();
newNode.value = item;
newNode.link = qNode;

pNode.link = newNode;
return true;
}

// Delete an item at a given position
public boolean deleteItem(int pos) {
if (pos > size())
return false;
if (isEmpty())
return false;
// First find the position to delete
if (pos == 1) {
Node pNode = head.link;
head.link = null;
head = pNode;
return true;
}
if (pos == size()) {
Node preNode = head;
for (int i = 0; i < pos - 2; i++) {
preNode = preNode.link;
}
preNode.link = null;
return true;
}

Node preNode = head;
for (int i = 0; i < pos - 2; i++) {
preNode = preNode.link;
}
Node node = preNode.link;
Node nextNode = node.link;
preNode.link = nextNode;
node.link = null;

return true;
}

// Select sort the list
public void selectSort() {
for (Node p = head; p.link != null; p = p.link) {
for (Node q = p.link; q != null; q = q.link) {
if ((int) p.value > (int) q.value) {
T temp = p.value;
p.value = q.value;
q.value = temp;
}
}
}
}

// Bubble sort the list
public void bubbleSort() {
for (Node p = head; p.link != null; p = p.link) {
for (Node q = head; q.link != null; q = q.link) {
if ((int) q.value > (int) q.link.value) {
T temp = q.value;
q.value = q.link.value;
q.link.value = temp;
}
}
}
}

public void printList() {
Node n = head;
while (n != null) {
System.out.print(n.value + " ");
n = n.link;
}
System.out.println();
}

public int size() {
if (isEmpty())
return 0;
int sz = 0;
Node node = head;
while (node != null) {
node = node.link;
sz++;
}
return sz;
}

class Node {
private T value;
private Node link;
}
}

//TestLinkList.java
package com.fqyuan.linkedlist;

import java.util.Random;

public class TestLinkList {

public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
int val = random.nextInt(100);
System.out.print(val + " ");
list.insertAtStart(val);
}
System.out.println();
list.printList();

System.out.println("Delete at position 1: ");
list.deleteItem(1);
list.printList();

System.out.println("Insert at start with value 1: ");
list.insertAtStart(1);
list.printList();

System.out.println("Delete at position 3: ");
list.deleteItem(3);
list.printList();

System.out.println("The size of the list is: " + list.size());

System.out.println("Insert at positon 3 with value 33:");
list.insertAtPos(3, 333);
list.printList();

System.out.println("Delete at positon: 1");
list.deleteItem(1);
list.printList();

System.out.println("Delete at position size(): ");
list.deleteItem(list.size());
list.printList();

System.out.println("Insertion at end: ");
list.insertAtEnd(100);
list.printList();

System.out.println("Sort the array: ");
list.bubbleSort();
// list.selectSort();
list.printList();
}

}

//Running result:
69 65 65 14 97 91 22 13 38 43
43 38 13 22 91 97 14 65 65 69
Delete at position 1:
38 13 22 91 97 14 65 65 69
Insert at start with value 1:
1 38 13 22 91 97 14 65 65 69
Delete at position 3:
1 38 22 91 97 14 65 65 69
The size of the list is: 9
Insert at positon 3 with value 33:
1 38 22 333 91 97 14 65 65 69
Delete at positon: 1
38 22 333 91 97 14 65 65 69
Delete at position size():
38 22 333 91 97 14 65 65
Insertion at end:
38 22 333 91 97 14 65 65 100
Sort the array:
14 22 38 65 65 91 97 100 333
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息