/*c current 表示现有节点
 * ci currentindex  表示现有节点的位置值
 * i=index   想要插入或删除节点的位置值
 * 
 * */public class A{
myNode first;
void Init(){
first=null;
}
void Tshow(){
myNode c=first;
while(c!=null){
c.show();
c=c.next;
}
}
//**********************************
void add(int value){
myNode newN=new myNode();
newN.data=value;
add(newN);//调用指针部分
}
void add(myNode newN){
if(first==null){
first=newN;
return;
}
/*
myNode c=first;//使当前节点为first
while(c.next!=null){//如果c的下面一个节点不是空的 就一个个的往后挪
c=c.next;
}
c.next=newN;//会执行此句话 表示c已经是最后一个节点 那么将新的节点赋给c.next
*/
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
像刚才那样做就是对的,用c.next!=null  
        为什么用c!=null就不对呢?
        而我下面也是这么做的呀?为什么在添加这里行不通?
myNode p;
myNode c=first;
while(c!=null){
p=c;
c=c.next;
if(c==null){
c=newN;
return;
}
}


}
//**********************************
void del(int i){
if(i==0){
if(first==null)
return;
first=first.next;
}
int ci=0;
myNode p;
myNode c=first;
while(c!=null){
p=c;
c=c.next;//一个个往后挪
ci++;
if(ci==i){
p.next=c.next;//使c的前面一个与c后面的一个相连
return;
}
}
}
//**********************************
void ins(int i,int value){
myNode newN=new myNode();
newN.data=value;
ins(i,newN);
}

void ins(int i,myNode newN){
int ci=0;
myNode p;
myNode c=first;
while(c!=null){
ci++;
p=c;
c=c.next;
if(ci==i){
p.next=newN;
newN.next=c;

}
}
}

//**********************************
int length(){
int length=0;
myNode c=first;
while(c!=null){
length++;
c=c.next;
}
return length;
}

//**********************************
void locate(int value){
int ci=0;
myNode c=first;
//int r;
while(c!=null){
if(value==c.re()){
System.out.println("locate: value is "+ci);
}
c=c.next;
ci++;
}
}
//**********************************
public static void main(String args[]){
A ob=new A();
//ob.Init();
ob.add(0);
ob.add(1);
ob.add(2);
ob.Tshow();
ob.del(0);
//System.out.println("**");
ob.Tshow();
ob.ins(1, 55);
ob.Tshow();
System.out.println("length is "+ob.length());
ob.locate(55);
ob.ins(1,54);
ob.Tshow();
System.out.println("length is "+ob.length());
}
}
class myNode{
int data;
myNode next;
void show(){
System.out.println(data);
}
int re(){
return data;
}
}

解决方案 »

  1.   

    按你的做法,c最成了null了,你还要个什么?
      

  2.   

    把void add(myNode newN)中的
    c=newN;
    改成p.next=newN注意JAVA所有的方法调用都是传值调用而非传引用调用。
      

  3.   

    看着好累,帮楼主排个版/*c current 表示现有节点 
    * ci currentindex  表示现有节点的位置值 
    * i=index  想要插入或删除节点的位置值 

    * */ public class A
    {
       myNode first;
       void Init()
       {
          first=null;
       }
       void Tshow()
       {
          myNode c=first;
          while(c!=null)
          {
             c.show();
             c=c.next;
          }
       }
       //********************************** 
    void add(int value)
       {
          myNode newN=new myNode();
          newN.data=value;
          add(newN);
       }
       //调用指针部分 
     
    void add(myNode newN)
       {
          if(first==null)
          {
             first=newN;
             return;
          }
          /* 
    myNode c=first;
          //使当前节点为first 
    while(c.next!=null)
          {
             //如果c的下面一个节点不是空的 就一个个的往后挪 
    c=c.next;
          }
          c.next=newN;
      

  4.   


    /*
     * 一个单向链表例子
     * 开发者:爪哇鹅
     * 开发时间:09.08.05
     */package javaapplication1;class Link{                                     //链表类
        //定义一个内部类
        class Node{                                 //定义一个内部类
            private String name;                    //节点名
            private Node next;                      //下一个节点        public Node(String name){
                this.name=name;
            }
            //添加一个节点
            public void addNode(Node newNode){
                if(this.next==null){                //如果一个节点都没有,那么就把新的节点作为第一个节点
                    this.next=newNode;
                }else{
                    this.next.addNode(newNode);     //否则利用递归方法向下添加节点
                }
            }
            //查找节点
            public boolean searchNode(String name){
                if(this.name.equals(name)){
                    return true;
                }else{
                    if(this.next!=null){
                        return this.next.searchNode(name);
                    }else{
                        return false;
                    }
                }
            }
            //删除节点
            public void deleteNode(Node preNode,String name){
                if(this.name.equals(name)){
                    preNode.next=this.next;
                }else{
                    this.next.deleteNode(this,name);
                }
            }
            //打印节点
            public void printNode(){
                System.out.print(this.name+"---->");
                if(this.next!=null){
                    this.next.printNode();               //如果不止一个节点,采用递归方法继续向下打印
                }
            }
        }
        //**************************************************************************
        private Node root;
        //外部类添加节点方法,调用内部类相关方法
        public void add(String name){
            Node newNode=new Node(name);
            if(this.root!=null){
                this.root.addNode(newNode);
            }else{
                this.root=newNode;
            }
        }
        //外部类查找节点方法
        public boolean search(String name){
            if(this.root!=null){
                return this.root.searchNode(name);
            }else{
                return false;
            }
        }
        //外部类删除节点方法
        public void delete(String name){
            if(this.search(name)){
                if(this.root.name.equals(name)){
                    if(this.root.next!=null){
                        this.root=this.root.next;
                    }else{
                        this.root=null;
                    }
                }else{
                    this.root.next.deleteNode(root,name);
                }
            }else{
                System.out.println("您要删除的节点根本不存在!!!");
            }
        }
        //外部类打印节点方法
        public void print(){
            if(this.root!=null){
                this.root.printNode();
            }
        }
    }
    public class NodeDemo {
        //主方法中测试以上各个功能
        public static void main(String[] args) {
            Link link=new Link();
            link.add("根节点");
            link.add("第一点");
            link.add("第二点");
            link.add("第三点");
            link.add("第四点");       // link.print();
           //boolean bool=link.search("第四点");
           //System.out.println(bool);        link.delete("根节点");
            link.print();
        }}