public class Node {
public String num;
public Node next=null;
public Node(String num){
this.num=num;
}
public Node(){
num=null;
next=this;
}
}
public class MyLinkList {
private Node headNode;
private Node tempNode;
public MyLinkList(){
headNode=new Node();
}
public boolean addNode(String num){
Node node = new Node(num);
tempNode = headNode;
while (tempNode.next != headNode) {
tempNode = tempNode.next;
}
tempNode.next = node;
node.next=headNode;
System.out.println("结点增加成功");
return true;
}
public boolean deleteNode(){
tempNode = headNode;
while (tempNode.next.next!= headNode) {
tempNode=tempNode.next;
}
tempNode.next=headNode;
System.out.println("结点删除成功");
return true;
}
public void seeNode(){
tempNode = headNode;
while (tempNode.next!=headNode){
System.out.println(tempNode.num);
tempNode=tempNode.next;
}
System.out.println(tempNode.num);
}
}
测试
public static void main(String[] args) {
MyLinkList list=new MyLinkList();
list.addNode("a");
list.addNode("b");
list.addNode("c");
list.deleteNode();
list.seeNode();
}
结果:
结点增加成功
结点增加成功
结点增加成功
结点删除成功
null
a
b