java中的单链表_Java中单链表的实现

注释比较详细,直接上代码!

package XiaoMi;

import java.util.HashSet;

import java.util.Set;

public class SingleLinkList {

//节点

class Element { //自定义数据结构

public Object value = null; //元素属性

public Element nextNode = null; //元素指针

}

//节点内容

class Value {

public String code;

public String name;

public Value() {

}

public Value(String code, String name) {

this.code = code;

this.name = name;

}

@Override

public String toString() {

return code + "-" + name;

}

}

//链表头节点

private Element header = null; //引用

public static void main(String[] args) {

SingleLinkList list = new SingleLinkList();

Value value1 = list.new Value("1", "java");

Value value2 = list.new Value("2", "c++");

Value value3 = list.new Value("3", "c#");

Value value4 = list.new Value("4", "vb");

list.add(value1);

list.add(value2);

list.add(value3);

list.add(value4);

System.out.println("remove vb ?" + list.remove(value4));

System.out.println("have c++ ?" + list.contains(value2));

System.out.println("have vb ?" + list.contains(value4));

System.out.println("list is emptry ? " + list.isEmpty());

System.out.println(list);

list.clear();

System.out.println("list is emptry ? " + list.isEmpty());

}

//添加一个节点

public void add(Object node) {

if (header == null) { //若为空,创建链表头

header = new Element();

header.value = null;

header.nextNode = null;

}

Element element = new Element(); //把链表头给加上

element.value = node;

element.nextNode = header.nextNode;

header.nextNode = element;

}

//清空链表

public void clear() {

header = null;

}

//是否包含某个元素

public boolean contains(Object o) {

if (header == null)

return false;

Element eqEl = header.nextNode;

while (eqEl != null) {

if (eqEl.value == o) {

return true;

}

eqEl = eqEl.nextNode;

}

return false;

}

//重写toString方法,拼接字符串

public String toString() {

int size = this.size();

String print = "";

if (size == 0)

return print;

for (int i = 0; i < size; i++) {

print = "," + this.get(i) + print;

}

print = "[" + print.substring(1) + "]";

return print;

}

//取第i个元素

public Object get(int index) {

if (header == null)

return null;

int size = this.size();

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

return null;

}

Element temp = header.nextNode;

int i = 0;

while (temp != null) {

if (index == i) {

return temp.value;

}

i++;

temp = temp.nextNode;

}

return null;

}

//返回第i个节点元素

private Element getElement(int index) {

if (header == null)

return null;

int size = this.size();

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

return null;

}

Element temp = header.nextNode;

int i = 0;

while (temp != null) {

if (index == i) {

return temp;

}

i++;

temp = temp.nextNode;

}

return null;

}

//链表是否为空

public boolean isEmpty() {

if (header == null)

return true;

else

return false;

}

//删除一个节点

public boolean remove(Object o) {

if (header == null)

return false;

Element eqPreEl = header;

Element eqEl = header.nextNode;

while (eqEl != null) {

if (eqEl.value == o) {

eqPreEl.nextNode = eqEl.nextNode; //指向它的下一个节点

return true;

}

eqPreEl = eqEl; //指针向后移动

eqEl = eqEl.nextNode;

}

return false;

}

//链表的长度

public int size() {

if (header == null)

return 0;

Element temp = header.nextNode;

int i = 0;

while (temp != null) {

i++;

temp = temp.nextNode;

}

return i;

}

/**

* 检查环状单链表

*

* @return

*/

public boolean checkLoop() {

if (header == null)

return false;

int size = this.size();

if (size == 0)

return false;

Set set = new HashSet();

for (int i = 0; i < size; i++) {

Element el = getElement(i);

if (!set.contains(el)) {

set.add(el);

}

if (set.contains(el.nextNode)) { //如果和set中已有节点重复,则认为有环

return true;

}

}

return false;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值