链表相比数组操作更灵活,在空间效率方面比数组更有优势,虽然java中没有指针,但可以通过自定义类建立链表模型,进而实现链表。
分享下自己用java实现链表的过程:
java代码:
Node 类:
package com.list.stack;
/**
* Setup Node class
* @author gannyee
*
*/
public class Node {
//Declared element
private Object element;
//Declared next node
private Node next;
//Constructor of Class Node
public Node(){
this(null,null);
}
public Node(Object newElement,Node newnNext){
element = newElement;
next = newnNext;
}
//Get element function
public Object getElement() {
return element;
}
//Set element function
public void setElement(Object newElement) {
element = newElement;
}
//Get next node function
public Node getNext() {
return next;
}
//Set next node function
public void setNext(Node newNext) {
next = newNext;
}
}
NodeList 类:
package com.list.test;
import java.util.Arrays;
import com.list.stack.Node;
import com.stack.ExceptionStackEmpty;
/**
*
* @author gannyee
*
*/
public class NodeList {
// Declared head node
private static Node head;
// Declared rear node
private static Node rear;
// Get size
private static int size;
// Constructor
public NodeList() {
head = rear = null;
size = 0;
}
// Add node after a requested node
public void AddElementAfterNode(Object desElement, Object newElement) {
Node traNode = head;
Node copyNode;
Node newNode = new Node(newElement, null);
// Find element‘s position
if (FindElement(desElement)) {
while (traNode != null) {
if (traNode.getElement() == desElement)
break;
traNode = traNode.getNext();
}
newNode.setNext(traNode.getNext());
traNode.setNext(newNode);
size++;
System.out.println(newElement
+ " Successfully added in the after of " + desElement);
} else {
System.out.println("Element: " + desElement
+ " isn‘t in node list, can‘t be added");
}
}
// Add node by first node
public void AddElementByFirst(Object element) {
Node newNode = new Node(element, head);
if (size == 0 && rear == null)
rear = newNode; // Let the rear node not be a null node
head = newNode;
System.out.println(element
+ " Successfully added in the head of node list!");
size++;
}
// Add node by last node
public void AddElementByLast(Object element) {
Node newNode = new Node(element, null);
if (isEmpty()) {
head = newNode;
rear = newNode;
} else {
rear.setNext(newNode);
rear = newNode;
}
System.out.println(element
+ " Successfully added in the rear of node list!");
size++;
}
// Delete node by first node
public void DeleteElementByFirst() throws ExceptionNodeListEmpty {
Object copyElement;
if (isEmpty())
throw new ExceptionNodeListEmpty("Node list is empry!");
copyElement = head.getElement();
head = head.getNext();
System.out.println(copyElement
+ " Successfully deleted from the head of node list!");
size--;
}
// Delete node by last node
public void DeleteElementByLast() throws ExceptionNodeListEmpty {
Object copyElement;
if (isEmpty())
throw new ExceptionNodeListEmpty("Node list is empry!");
Node travelNode = head;
while (travelNode != null) {
if (travelNode.getNext() == rear) {
break;
}
travelNode = travelNode.getNext();
}
copyElement = rear.getElement();
rear = travelNode;
rear.setNext(null);
System.out.println(copyElement
+ " Successfully deleted from the rear of node list!");
size--;
}
// Delete node which in center of node list
public void DeleteNodeInCenter(Object element) {
Node traNode = head;
Node swapNode;
// Find element‘s position
if (FindElement(element)) {
while (traNode != null) {
if (traNode.getNext().getElement() == element)
break;
traNode = traNode.getNext();
}
traNode.setNext(traNode.getNext().getNext());
System.out.println("Delete node in the center of node list!");
} else {
System.out.println("Element: " + element
+ " isn‘t in node list, can‘t be deleted");
}
}
// Find node
private boolean FindElement(Object element) {
int flag = 0; // Element not found
Node travelNode = new Node(element, null);
travelNode = head;
while (travelNode != null) {
if (travelNode.getElement() == element) {
return true;
}
travelNode = travelNode.getNext();
}
return false;
}
// Modify element of node in node list
public void ModifyElement(Object oldElement, Object newElememt) {
Node traNode = head;
// Find element‘s position
if (FindElement(oldElement)) {
while (traNode != null) {
if (traNode.getElement() == oldElement)
break;
traNode = traNode.getNext();
}
oldElement = traNode.getElement();
traNode.setElement(newElememt);
System.out.println("Successfully modify! before: " + oldElement
+ " After: " + traNode.getElement());
} else {
System.out.println("Element: " + oldElement
+ " isn‘t in node list, can‘t be modified");
}
}
// Whether node list is empty
public boolean isEmpty() {
return size == 0;
}
// Is Element in node list
public void isInNodeList(Object element) {
if (FindElement(element))
System.out.println("Element: " + element + " is in node list");
else
System.out.println("Element: " + element + " isn‘t in node list");
}
// Get the size of stack
public int getSize() {
if (isEmpty())
return 0;
else
return size;
}
// Get the all elements of stack
public void getAllElements() throws ExceptionStackEmpty {
Node travelTop;
String allElement = "";
travelTop = head;
for (int i = 0; travelTop != null; i++) {
String str = travelTop.getElement().toString();
allElement = allElement + str + " ";
travelTop = travelTop.getNext();
}
System.out.println("Get all elemnt: " + allElement);
}
}
NodeTest 类:
package com.list.test;
import com.stack.ExceptionStackEmpty;
/**
*
* @author gannyee
*
*/
public class NodeTest {
public static void main(String[] args) throws ExceptionStackEmpty, ExceptionNodeListEmpty {
//Test AddAddElementByFirst() function and AddElementByLast() function
NodeList nodeList = new NodeList();
nodeList.AddElementByFirst("1");
nodeList.AddElementByFirst("2");
nodeList.AddElementByFirst("3");
nodeList.AddElementByFirst("4");
nodeList.AddElementByFirst("5");
nodeList.AddElementByFirst("6");
nodeList.AddElementByLast("8");
nodeList.AddElementByLast(9);
nodeList.AddElementByLast("10");
nodeList.AddElementByLast(11);
System.out.println("--------------------------------");
//Test ModifyElement() function
nodeList.ModifyElement("6","32");
nodeList.ModifyElement("12","34");
System.out.println("--------------------------------");
//Test isInNodeList() function
nodeList.isInNodeList("7");
nodeList.isInNodeList("9");
nodeList.isInNodeList(11);
nodeList.isInNodeList(2);
System.out.println("--------------------------------");
//Test getAllElements() function and getSize() function
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
System.out.println("--------------------------------");
//Test DeleteNodeInCenter() function
nodeList.DeleteNodeInCenter(9);
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
System.out.println("--------------------------------");
//Test DeleteElementByLast() function and DeleteElementByFirst() function
nodeList.DeleteElementByLast();
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
System.out.println("--------------------------------");
nodeList.DeleteElementByLast();
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
System.out.println("--------------------------------");
nodeList.DeleteElementByLast();
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
System.out.println("--------------------------------");
nodeList.DeleteElementByFirst();
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
System.out.println("--------------------------------");
//Test AddElementAfterNode() function
nodeList.AddElementAfterNode("4", "65");
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
System.out.println("--------------------------------");
nodeList.AddElementAfterNode("22", "65");
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
nodeList.AddElementByLast("12");
nodeList.AddElementByLast("13");
nodeList.AddElementByLast("14");
nodeList.AddElementByLast("15");
nodeList.AddElementByLast("16");
nodeList.AddElementByLast("17");
nodeList.AddElementByLast("18");
nodeList.getAllElements();
System.out.println("size: " + nodeList.getSize());
}
}
测试结果:
1 Successfully added in the head of node list!
2 Successfully added in the head of node list!
3 Successfully added in the head of node list!
4 Successfully added in the head of node list!
5 Successfully added in the head of node list!
6 Successfully added in the head of node list!
8 Successfully added in the rear of node list!
9 Successfully added in the rear of node list!
10 Successfully added in the rear of node list!
11 Successfully added in the rear of node list!
--------------------------------
Successfully modify! before: 6 After: 32
Element: 12 isn‘t in node list, can‘t be modified
--------------------------------
Element: 7 isn‘t in node list
Element: 9 isn‘t in node list
Element: 11 is in node list
Element: 2 isn‘t in node list
--------------------------------
Get all elemnt: 32 5 4 3 2 1 8 9 10 11
size: 10
--------------------------------
Delete node in the center of node list!
Get all elemnt: 32 5 4 3 2 1 8 10 11
size: 10
--------------------------------
11 Successfully deleted from the rear of node list!
Get all elemnt: 32 5 4 3 2 1 8 10
size: 9
--------------------------------
10 Successfully deleted from the rear of node list!
Get all elemnt: 32 5 4 3 2 1 8
size: 8
--------------------------------
8 Successfully deleted from the rear of node list!
Get all elemnt: 32 5 4 3 2 1
size: 7
--------------------------------
32 Successfully deleted from the head of node list!
Get all elemnt: 5 4 3 2 1
size: 6
--------------------------------
65 Successfully added in the after of 4
Get all elemnt: 5 4 65 3 2 1
size: 7
--------------------------------
Element: 22 isn‘t in node list, can‘t be added
Get all elemnt: 5 4 65 3 2 1
size: 7
12 Successfully added in the rear of node list!
13 Successfully added in the rear of node list!
14 Successfully added in the rear of node list!
15 Successfully added in the rear of node list!
16 Successfully added in the rear of node list!
17 Successfully added in the rear of node list!
18 Successfully added in the rear of node list!
Get all elemnt: 5 4 65 3 2 1 12 13 14 15 16 17 18
size: 14
体会:联系数组可以得到:链表在增加节点和删除节点的时候,通过getnext()方法可以获取我们想要的节点,与数组相比,直接通过下标访问效率更高。但是对于数组来说,增加和删除时元素都要将目标删除元素后面的每个元素往前移动一位。假设又能个元素,则数组的时间复杂度为O(n),而链表则是0(1)。 如果增删操作很频繁的话,链表相比比数组效率更高。 所以在增删比较频繁的时候我们应该考虑是否使用链表这种数据结构。
转载请注明出处,谢谢!
http://blog.csdn.net/github_27609763/article/details/46476851
原文地址:http://blog.csdn.net/github_27609763/article/details/46476851