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

带有首尾的可反转链表(LinkedList)的java实现

2017-12-03 20:32 501 查看

链表类:

package linkedList;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import linkedList.exception.IllegalIndexException;

/**
* @author wyhong
*
*/
public class MyLinkedList {

/**
* the size of the linked list
*/
private int size;
/**
* a pointer which records the current node while manipulating the linked list
*/
private Node pointer;
/**
* the first node of the linked list
*/
private Node head;
/**
* the last node of the linked list
*/
private Node tail;

public int size() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public class Node{

public Node(int _value) {
super();
this.next = null;
this.value = _value;
}
public Node() {
// TODO Auto-generated constructor stub
}
/**
* the next node of this node
*/
private Node next;
/**
* the value of the node
*/
private int value;

public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}

/**
* add a new node to the linked list
* @param newNode the node to be added
*/
public void add(Node newNode) {
if (this.size() == 0) {
head = newNode;
tail = newNode;
}else {
tail.setNext(newNode);
tail = newNode;
}
this.setSize(this.size() + 1);
}

public List<Node> removeNodes(int value) {
return _remove(value, new ArrayList<Node>());
}

/**
* remove nodes by a specified
* @param value nodes whose values equal to the parameter are to remove
* @param targets a container stores the removed nodes
* @return removed nodes
*/
public List<Node> _remove(int value, List<Node> targets) {
if (this.size() == 1) {
if(head.getValue() == value) {
targets.add(head);
head = tail = null;
this.setSize(0);
}else {
return null;
}
}else {
if (head.getValue() != value) {
pointer = head;
while (pointer.getNext() != null && pointer.getNext().getValue() != value) {
pointer = pointer.getNext();
}
if (pointer.getNext() == null) {
if (targets.size() == 0) {
System.out.println("the target node doesn't exist!");
}
}else {
targets.add(pointer.getNext());
if (tail == pointer.getNext()) {
tail = pointer;
pointer.setNext(null);
return targets;
}
pointer.setNext(pointer.getNext().getNext());
_remove(value, targets);
}
}else {
targets.add(head);
head = head.getNext();
_remove(value, targets);
}
}
return targets;
}

/**
* remove a node by a specified index
* @param index remove the node according to the specified index, note that the index begin with 1
* @return the node to be removed if it exists
*/
public Node removeNode(int index) {
if (index < 1 || index > this.size()) {
try {
throw new IllegalIndexException("the index's out of boundary");
} catch (IllegalIndexException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
return null;
}
}
Node tempNode = null;
if (index == 1) {
tempNode = head;
if (this.size() == 1){
tail = null;
}
head = head.getNext();
this.setSize(this.size() - 1);
}else if (index == this.size()) {
pointer = head;
while (pointer.getNext() != tail) {
pointer = pointer.getNext();
}
pointer.setNext(null);
tempNode = tail;
tail = pointer;
}else {
int pointerIndex = 1;
pointer = head;
while (pointerIndex < index - 1) {
pointer = pointer.getNext();
pointerIndex++;
}
tempNode = pointer.getNext();
pointer.setNext(pointer.getNext().getNext());
}
return tempNode;
}

/**
* get a node by a specified index
* @param index get node by this index
* @return target node
*/
public Node getNode(int index) {
if (index < 1 || index > this.size()) {
try {
throw new IllegalIndexException("Exception: the index's out of boundary!");
} catch (IllegalIndexException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
return null;
}
}
if (index == 1) {
return head;
}else if (index == this.size()) {
return tail;
}else {
int pointerIndex = 1;
pointer = head;
while (pointerIndex < index) {
pointer = pointer.getNext();
pointerIndex++;
}
return pointer
4000
;
}
}

/**
* get nodes by a value
* @param value get nodes by this value
* @return target nodes that hold the value
*/
public List<Node> getNodes(int value) {
return _getNodes(value, new ArrayList<Node>(), null);
}

public List<Node> _getNodes(int value, List<Node> targets, Node _pointer) {
if (_pointer == null) {
_pointer = head;
}
if (this.size() == 1) {
if(head.getValue() == value) {
targets.add(head);
}
}else {
if (_pointer.getValue() != value) {
while (_pointer.getNext() != null && _pointer.getValue() != value) {
_pointer = _pointer.getNext();
}
if (_pointer.getNext() == null && _pointer.getValue() != value) {
if (targets.size() == 0) {
System.out.println("Target node doesn't exist!");
}
}else {
targets.add(_pointer);
if (_pointer.getNext() != null ) {
_getNodes(value, targets, _pointer.getNext());
}
}
}else {
targets.add(head);
_getNodes(value, targets, head.getNext());
}
}
return targets;
}

/**
* reverse a linkedlist and return a new one
* @return a new LinkedList that is already reversed
*/
public MyLinkedList reverse() {
if (this.isEmpty()) {
System.out.println("the empty linked list leads to a failure of reversion");
return this;
}
Stack<Node> stack = new Stack<Node>();
pointer = head;
while (pointer != null) {
stack.push(pointer);
pointer = pointer.getNext();
}
MyLinkedList temp = new MyLinkedList();
while (stack.size() > 0) {
Node node = stack.pop();
//Original mappings have to be reset
node.setNext(null);
temp.add(node);
}
return temp;
}

/**
* insert a node into the linkedlist
* @param index insert into this index which starts from 1
* @param node node to be inserted
* @return
*/
public boolean insert(int index, Node node) {
if (index < 1 || index > this.size()) {
try {
throw new IllegalIndexException("Exception: the index's out of boundary!");
} catch (IllegalIndexException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
return false;
}
}
if (index == 1) {
node.setNext(head.getNext());
head = node;
}else if (index == this.size()) {
tail.setNext(node);
tail = node;
}else {
int pointerIndex = 1;
pointer = head;
while (pointerIndex < index - 1) {
pointer = pointer.getNext();
pointerIndex++;
}
node.setNext(pointer.getNext());
pointer.setNext(node);
}
return true;
}

/**
* print a linkedlist formattedly
*/
public void print() {
if (this.isEmpty()) {
System.out.println("The linked list is empty!");
}else if (this.size() == 1) {
System.out.println(head.getValue());
}else {
pointer = head;
while (pointer != null) {
if (pointer != tail) System.out.print(pointer.getValue()+"-->");
else System.out.println(pointer.getValue());
pointer = pointer.getNext();
}
}
}

/**
* get the first node of a linkedlist
* @return the first node of a linkedlist
*/
public Node getFirst() {
return getNode(1);
}

/**
* get the last node of a linkedlist
* @return the last node of a linkedlist
*/
public Node getLast() {
return getNode(this.size());
}

/**
* remove the first node of a linkedlist
* @return the first node of a linkedlist, which is already removed
*/
public Node removeFirst() {
return removeNode(0);
}

/**
* remove the last node of a linkedlist
* @return the last node of a linkedlist, which is already removed
*/
public Node removeLast() {
return removeNode(this.size());
}

/**
* clear the linkedlist
*/
public void clear() {
head = tail = null;
this.setSize(0);
}

/**
* To check if a linkedlist have any nodes
* @return
*/
private boolean isEmpty() {
// TODO Auto-generated method stub
if (this.size() == 0) {
return true;
}
return false;
}
}
测试类:
package linkedList.test;

import linkedList.MyLinkedList;

public class MyLinkedListTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
add(10);
// removeNode(5);
// removeNodes(4);
// getNode(3);
// getNodes(5);
// reverse();
// insert();
// clear();
}

private static void clear() {
// TODO Auto-generated method stub
MyLinkedList linkedList = add(10);
linkedList.clear();
linkedList.print();
}

private static void insert() {
// TODO Auto-generated method stub
MyLinkedList linkedList = add(10);
linkedList.insert(5, linkedList.new Node(233));
linkedList.print();
}

private static void reverse() {
// TODO Auto-generated method stub
add(10).reverse().print();
}

private static void getNodes(int value) {
// TODO Auto-generated method stub
MyLinkedList linkedList = new MyLinkedList();
linkedList.add(linkedList.new Node(1));
linkedList.add(linkedList.new Node(2));
linkedList.add(linkedList.new Node(1));
linkedList.add(linkedList.new Node(3));
linkedList.add(linkedList.new Node(4));
linkedList.add(linkedList.new Node(2));
linkedList.add(linkedList.new Node(4));
linkedList.print();
System.out.println(linkedList.getNodes(value).size());
linkedList.print();
}

private static void getNode(int index) {
// TODO Auto-generated method stub
MyLinkedList list = add(10);
System.out.println(list.getNode(index).getValue());
list.print();
}

private static void removeNodes(int value) {
// TODO Auto-generated method stub
MyLinkedList linkedList = new MyLinkedList();
linkedList.add(linkedList.new Node(1));
linkedList.add(linkedList.new Node(2));
linkedList.add(linkedList.new Node(1));
linkedList.add(linkedList.new Node(3));
linkedList.add(linkedList.new Node(4));
linkedList.add(linkedList.new Node(2));
linkedList.add(linkedList.new Node(4));
linkedList.print();
System.out.println(linkedList.removeNodes(value).size());
linkedList.print();
}

private static void removeNode(int index) {
// TODO Auto-generated method stub
MyLinkedList list = add(10);
list.removeNode(index);
list.print();
}

private static MyLinkedList add(int num) {
// TODO Auto-generated method stub
MyLinkedList linkedList = new MyLinkedList();
for (int i = 0; i < num; i++) {
linkedList.add(linkedList.new Node(i));
}
linkedList.print();
return linkedList;
}

}

结果:

添加:

插入233到位置5:

删除索引为5的元素:

删除值为4的元素(多个):

查找索引为3的元素:

查找值为2的元素(多个):

反转:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: