其中的修改链表的操作即包含查找操作,所以没有单独写出来。
class TNode{
int val;
TNode next;
public TNode(int val)
{
this.val=val;
}
}
class Linknode{
TNode head;
int length=0;
public void print()
{
TNode p=head;
while (p!=null)
{
System.out.println(p.val);
p=p.next;
}
}
public void addnode(int data)
{
TNode node=new TNode(data);
if(head==null)
{
head=node;
length++;
}
else
{
TNode q=head;
while (q.next!=null)
{
q=q.next;
}
q.next=node;
length++;
}
}
public Boolean deletenode(int index)
{
if(index<1||index>length)
return false;
else {
int i=1;
TNode p=head;
while (i!=index-1)
{
p=p.next;
i++;
}
p.next=p.next.next;
length--;
return true;
}
}
public Boolean updatenode(int index,int date)
{
if(index>length)
return false;
TNode p=head;
int i=1;
while (i!=index)
{
p=p.next;
i++;
}
p.val=date;
return true;
}
}
public class Main {
public static void main(String args[])
{
Linknode l=new Linknode();
l.addnode(1);
l.addnode(2);
l.addnode(3);
l.addnode(4);
l.addnode(5);
l.print();
l.deletenode(2);
l.print();
l.updatenode(4,9);
l.print();
System.out.println();
}
}
---------------------
作者:lemonlemons
来源:CSDN
原文:https://blog.csdn.net/lemonlemons/article/details/82684469
版权声明:本文为博主原创文章,转载请附上博文链接!