java创建单链表_【Java数据结构】2.1单链表的实现

package com.ds.link;

/**

*

* 数据结构之Java单链表

*

*

* 单链表提供了在列表头的高效插入和删除操作,不过在单链表的末尾的插入操作效率很低.

*

*

* 单链表指针域保存着下一节点的引用,尾结点的指针域等于null

*

*

* @author zhb 2013

*/

public class SingleLink {

/**

* 结点类

*/

private class Node {

private T data;// 数据域

private Node next; // 指针域保存着下一节点的引用

public Node(T data, Node next) {

this.data = data;

this.next = next;

}

public Node(T data) {

this(data, null);

}

}

// 下面是SingleLinkedList类的数据成员和方法

private Node header, tail;

private int size;

public SingleLink() {

this.header = null;

this.tail = null;

this.size = 0;

}

/**

* 判断链表是否为空

*/

public boolean isEmpty() {

return header == null;

}

/**

* 创建头指针,该方法只用一次!

*/

public void addToHeader(T item) {

header = new Node(item);

size += 1;

if (tail == null)

tail = header;

}

/**

* 添加尾指针,该方法使用多次

*/

public void addToTail(T item) {

if (!isEmpty()) {// 若链表非空那么将尾指针的next初使化为一个新的元素

tail.next = new Node(item); // 然后将尾指针指向现在它自己的下一个元素

tail = tail.next;

size += 1;

} else {// 如果为空则创建一个新的!并将头尾同时指向它

header = tail = new Node(item);

size += 1;

}

}

/**

* 打印列表

*/

public void print() {

if (isEmpty()) {

System.out.println("null");

} else {

Node current = header;

while (current != null) {

System.out.println("sigleLink size:" + this.size + "\t data:" + current.data);

current = current.next;

}

}

}

/**

* 在表头插入结点,效率非常高

*/

public void addFirst(T item) {

Node newNode = new Node(item);

newNode.next = header;

header = newNode;

size += 1;

}

/**

* 在表尾插入结点,效率很低

*/

public void addLast(T item) {

Node newNode = new Node(item);

while (header.next != null)

header = header.next;

header.next = newNode;

newNode.next = null;

size += 1;

}

/**

* 在表头删除结点,效率非常高

*/

public void deleteFirst() {

if (!isEmpty()) {

header = header.next;

size -= 1;

} else

System.out.println("The list have been emptied!");

}

/**

* 在表尾删除结点,效率很低

*/

public void deleteLast() {

Node current = header;

Node pre = null;

while (current != null) {

pre = current;

current = current.next;

if (current.next == null) {

pre.next = null;

size -= 1;

}

}

}

/**

* 在链表中任意位置插入,即在n后面插入

*/

public void insert(int n, T item) {

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

System.out.println("insert error!");

} else {

Node current = header;

Node newNode = new Node(item);

for (int index = 0; index < n -1; index++) {

current = current.next;

}

newNode.next = current.next;

current.next = newNode;

this.size +=1;

}

}

/**

* 测试代码

*/

public static void main(String[] args) {

SingleLink sl = new SingleLink();

sl.addToHeader("A");

sl.addToTail("B");

sl.addFirst("D");

sl.insert(2, "C");

sl.insert(1, "E");

sl.deleteFirst();

sl.print();

}

}

有问题可以随时交流!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值