Click here to Skip to main content
15,921,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class Link{
    public  String val;
    public  Link nextlink;
    public Link(String val) {
        this.val = val;
    }
    public void linkNext(Link l){
        this.nextlink=l;
    }
    public void printlink()
    {
        System.out.print(" "+val);
   }
}
class linkList{
    Link first;
    public static int  count=0;
    public linkList(){
        first=null;
    }
    boolean isEmpty(){
        return first==null;
    }

    public void insertfirst(String str){

        Link next=new Link(str);
       count++;
       next.nextlink=first;
        first=next;
    }
    public void insertN(int pos,String str){
        if(pos<count){

         Link temp=first;
       count++;
       for(int i=0;i<pos-1;i++)
           temp=temp.nextlink;
       Link next=new Link(str);
     
       next=temp.nextlink;
         temp=next;
        }
    }
    public void printList(){
        Link currentLink=first;
      System.out.println("data is:");
        while(currentLink!=null){
            
            currentLink.printlink();
            currentLink=currentLink.nextlink;

    }
        System.out.println("  ");


}
}


public class LinkedList2 {
    public static void main(String[] args) {
        linkList li=new linkList();
        li.insertfirst("neeraj");
        li.insertfirst("rohit");
        li.insertfirst("lalit");
        li.insertfirst("meenu");
        li.printList();
        li.insertfirst("newly");
        li.insertN(9, "newlaay");
        li.printList();
        System.out.println(li.count);
    }

}
Posted

1 solution

1. Why public static int count=0;?
That means there is a single count for every instance, remove the static!

2.
Change the access for the member variables to private.

3.
Change method linknext to setNextLink, this is a convention in Java that works well.

4.
insertFirst must use the method setNextLink, as the member varianble cannot be accessed:

Java
public void insertFirst(String str) {
    Link next = new Link(str);
    count++;
    next.setNextLink(first);
    first=next;
}


5.
insertN will not work as planned. Allow the Link class to do the work for you :

Java
class Link
    // stuff

    Link getLink (int position) {
        if (position <= 0 || this.nextlink == null) {
            return this.nextLink;
        }
        else
        {
            return this.nextlink.getLink(position - 1);
        }
    }
}


Then to add after a position:

Java
public void insertAfter(int pos,String str){
    Link previous = this.first.getLink(pos);
    if (previous != null) {
        Link next = previous.getNextLink();
        Link insert = new Link(str);
        previous.setNextLink(insert);
        insert.setNextLink(next);
    }
}


Simple and less code to go wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900