用java实现单向链表
来源:互联网 发布:星星知多少 编辑:程序博客网 时间:2024/06/04 21:47
主要就是简单的指针移动,之前有人让我帮改了一个链表的程序,但我觉得实现有问题 改完 自己又写了一个,代码在下面
public class MyLinkedList {
intsize;
Nodehead;
Nodetail;
publicMyLinkedList() {
size =0;
head =null;
tail =null;
}
public voidaddElement(A inNode) {
if (head ==null) {
head = tail= new Node(inNode);
} else{
tail.next =new Node(inNode);
tail =tail.next;
}
size++;
}
public voidupdateElement(int index, A content) {
if (index< 0 || index >= size) {
return;
}
NodecurNode = head;
while (index> 0) {
curNode =curNode.next;
index--;
}
curNode.content = content;
}
public AgetElement(int index) {
if (index< 0 || index >= size) {
returnnull;
}
NodecurNode = head;
while (index> 0) {
curNode =curNode.next;
index--;
}
returncurNode.content;
}
public voiddeleteElement(int index) {
if (index< 0 || index >= size) {
return;
public class MyLinkedList {