java 链结构_java实现链表结构

1. 定义节点node

public class Node {

private Node pre;

private Node next;

private T data;

public void setPre(Node node){

this.pre = node;

}

public Node getPre(){

return pre;

}

public void setNext(Node node){

this.next = node;

}

public Node getNext(){

return next;

}

public void setData(T data){

this.data = data;

}

public T getData(){

return this.data;

}

public Node(Node pre, Node next, T data){

this.pre = pre;

this.next = next;

this.data = data;

}

public Node(){

super();

}

}

2.  创建链表方式1,与c++类似

public class ListDirectNode> {

private byte[] lock = new byte[1];

/**

* 链表大小

*/

private int size;

/**

* 链表头节点

*/

private Node header;

/**

* 链表尾部节点

*/

private Node tail;

public void setHeader(Node node){

this.header = node;

}

public Node getHeader(){

return this.header;

}

public void setTail(Node node){

this.tail = node;

}

public Node getTail(){

return this.tail;

}

public int size(){

return this.size;

}

public ListDirectNode(){

size = 0;

header = new Node(null, null, null);

tail = new Node(header, null, null);

header.setNext(tail);

}

/**

* 往链表尾部追加数据

* @param data

*/

public void add(T data){

synchronized (lock) {

Node node = new Node(null, null, data);

tail.getPre().setNext(node);

node.setPre(tail);

node.setNext(tail);

tail.setPre(node);

size++;

}

}

/**

* @param sortType 排序方式

* true:升序

* false:降序

*/

public void sort(boolean sortType){

for(Node node1 = header.getNext(); node1.getNext() != tail; node1 = node1.getNext()){

for(Node node2 = node1.getNext(); node2 != tail; node2 = node2.getNext()){

T d1 = node1.getData();

T d2 = node2.getData();

if(sortType){

if(compare(d1, d2) > 0){

node1.setData(d2);

node2.setData(d1);

}

}else{

if(compare(d1, d2) < 0){

node1.setData(d2);

node2.setData(d1);

}

}

}

}

}

private int compare(T d1, T d2){

return d1.compareTo(d2);

}

public boolean isEmpty(){

return size==0;

}

public T get(int index){

if(index > size || index < 0){

throw new IndexOutOfBoundsException();

}

Node currentNode = new Node(header, null, null);

for(int i=1; i<=index; i++){

currentNode = currentNode.getNext();

}

return currentNode.getData();

}

public void print(){

int index = 0;

Node currentNode = header.getNext();

while(currentNode.getNext() != null){

index++;

System.out.println("当前位置:"+index+",当前值"+currentNode.getData().toString());

currentNode = currentNode.getNext();

}

}

}

3. 创建链表方式2,迭代器

public class MyList> implements Iterable{

/**

* 链表大小

*/

private int size;

/**

* 链表头节点

*/

private Node header;

/**

* 链表下一节点

*/

private Node tail;

public void setHeader(Node node){

this.header = node;

}

public Node getHeader(){

return this.header;

}

public void setTail(Node node){

this.tail = node;

}

public Node getTail(){

return this.tail;

}

public int size(){

return this.size;

}

public MyList(){

size = 0;

header = new Node(null, null, null);

tail = new Node(header, null, null);

header.setNext(tail);

}

public void add(T data){

Node node = new Node(null, null, data);

tail.getPre().setNext(node);

node.setPre(tail);

node.setNext(tail);

tail.setPre(node);

size++;

}

/**

* @param sortType 排序方式

* true:升序

* false:降序

*/

public void sort(boolean sortType){

for(Node node1 = header.getNext(); node1.getNext() != tail; node1 = node1.getNext()){

for(Node node2 = node1.getNext(); node2 != tail; node2 = node2.getNext()){

T d1 = node1.getData();

T d2 = node2.getData();

if(sortType){

if(compare(d1, d2) > 0){

node1.setData(d2);

node2.setData(d1);

}

}else{

if(compare(d1, d2) < 0){

node1.setData(d2);

node2.setData(d1);

}

}

}

}

}

private int compare(T d1, T d2){

return d1.compareTo(d2);

}

public void print(){

int index = 0;

Iterator iterator = this.iterator();

while(iterator.hasNext()){

index++;

System.out.println("当前位置:"+index+",当前值"+iterator.next().toString());

}

}

@Override

public Iterator iterator() {

return new MyListIterator();

}

private class MyListIterator implements Iterator{

@SuppressWarnings("unchecked")

Node current = (Node) header.getNext();

@Override

public boolean hasNext() {

return current != tail;

}

@Override

public T next() {

if(!hasNext()){

throw new IndexOutOfBoundsException();

}

T data = current.getData();

current = current.getNext();

return data;

}

@Override

public void remove() {

if(!hasNext()){

throw new NoSuchElementException();

}

current.getPre().setNext(current.getNext());

current.getNext().setPre(current.getPre());

current = current.getNext();

size--;

}

}

}

4. 测试代码

public class TestMain {

public static void main(String[] args){

ListDirectNode list = new ListDirectNode();

list.add("a");

list.add("d");

list.add("b");

list.add("e");

list.sort(true);

list.print();

MyList list2 = new MyList();

list2.add("a1");

list2.add("d1");

list2.add("b1");

list2.add("e1");

list2.sort(true);

list2.print();

ListDirectNode list3 = new ListDirectNode();

list3.add(1);

list3.add(3);

list3.add(2);

list3.add(10);

list3.sort(false);

list3.print();

MyList list4 = new MyList();

list4.add(10);

list4.add(3);

list4.add(6);

list4.add(0);

list4.sort(false);

list4.print();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值