i just learn about single linkedlist and i am trying to create a program.

public class linklis {


    public static void main(String[] args) {
       LinkedList ab= new LinkedList();
       ab.add("1");
       ab.add("2");
       ab.add("4");
       ab.add("7");
       System.out.println("the list" + ab);
       System.out.println("remove 2nd element: " + ab.Delete(2));

       System.out.println(" - print linkedlist size: " + ab.size());
       System.out.println("the list" + ab);




    }
}



class LinkedList {
    private Node head;
    private int cursor;

    public LinkedList(){
    head=-1;
    cursor=-1;

    }
    public void add(int newMember){
        Node newData=new Node(newMember);
        newData.setLink(head);
        head=newData;


    }

    public boolean empty(){
       return head=null;

    }
    public int size(){
        return size;
    }

    public void setData(integer newdata){
        data=newdata;


    }

    public void setLink (node newlink){
       link=newlink;

    }

public int getData(){
    return data;
}

public int getLink(){
    return link;
}

public boolean Delete(int key){
    Node found=search(key);
    if (found==-1)
    {
        System.out.println("nothing to delete");

    return false;
    }

    else
    {
        if  (found==head)
        {
            head=found.getLink();
          System.out.println("keydeleted");
        }
          return true;

        }
    }
    public String toString(){


        String outString = "";
        if (!empty ())
        {
            resetCursor ();
            outString += (cursor + " ") ;
            while (hasNext ())
                outString += (getNext () + "  ");

        }
        return outString;
    }

    }

Linkedlist program doesn't work

can you be a bit more specific abou the "doesn't work" part?
does it compile? do you get compile time exceptions?
does it run? do you get runtime exceptions?
does it run but produces unexpected results? what do you expect, and what do you get?

Edited 2 Years Ago by stultuske

`

 class LinkList1 
 {
    private Link head;

    public LinkList1(){
        head=null;

    }
    public void insertfirst(int data){
        Link newLink= new Link(data, head);
        head=newLink;
    }

public Link search (int key){
    if (head=null);
    System.out.println("empty list");
    return null;
    Link current=head;
    while (current.getData!=key)
    {
        if
                (current.next==null)

            return null;

    else
            current=current.next;
                    }
    return current;
            }

public Link delete (int key){
    Link current=head;
    Link previous=head;
    while (current.getData!=key)
    {
        if (current.next==null)
            return null;
        else
            if (current==head)
                head=head.next;
        else
                previous.next=current.next;



    }
     return current;   




}

public void display (){
    System.out.println("first");
Link current=head;
while (current!=null)

{
    current.displaydata();
    current=current.next;


}



}
public void addtorear(int x){
    Link newNode= new Link(x);
    if (head=null);
    head=newNode;
    Link current=head;
    current.next=newNode;
}
int get (int index){
    Link current=head;
    Link count=0;

    while (count<index)

    {
        if (current!=null)
        {
            current=current.next;
            count++;
        }
            else 
            return 0;
    }
        if (current!=null)
        return current.getData;


        else


    return 0;

}
public int removefirst(){
 Link temp=head;
 if(head!=null)
 {
     head=head.next;
     return temp.getData;
 }
 else 
     return 0;
 }


 public int removelast()
 {
 Link temp=head;
 if (temp==null)
     return 0;
 Link previous=head; //learn
 while(temp.next != null){
            previous = temp;         
            temp = temp.next;      
        }                                

        previous.next = null;            
        return temp.getData;            
    }                     


 public int indexof(int x){
     if (head=null)
         return 0;
     Link current=head;
     int count=0;
     while(current.getData!=x)
     {
         if (current.next=null)
             return 0;
         else
         {
             current=current.next;
          count++;   
         }

     }
         return count;

     }


 public static void main(String [] args) {
     LinkList1 L=new LinkList1();

     L.insertfirst(5);
        L.insertfirst(15);
        L.insertfirst(25);


        L.display();
                Link a= L.search(5);
                if(a!=null)
                {
                    a.display();
                    System.out.println("");

                }
                else
                    System.out.println("5 no found");


 Link b=L.search(20);
 if(b!=null)
 {
     b.display();
     System.out.println("");
 }
 else
     System.out.println("20 not found");

 L.delete(15);
 L.display();
 L.insertfirst(50);
 L.display();
 L.delete(5);

        L.display();


        L.delete(50);

        L.display();

        L.addtorear(30);
        L.addtorear(22);

        L.display();


        for(int i = 0; i < 10; i++)
            System.out.println(L.get(i));


        System.out.println(L.removefirst());

        System.out.println(L.removefirst());


        System.out.println(L.removefirst());


        L.display();
    }
 }

`

That's a lot of code, but you still haven't told us what your question/problem is, and that code is still incomplete (no Link class). So what are you asking us to do?

 class LinkList1
 {
   private Link head;

    public LinkList1(){
        head=null;


    }
    public void insertfirst(int data){
        Link newLink= new Link(data, head);
        head=newLink;
    }

public Link search (int key){
    if (head==null);
    System.out.println("empty list");
    return null;
    Link current=head;
    while (current.getData!=key)
    {
        if
                (current.next==null)

            return null;

    else
            current=current.next;
                    }
    return current;
            }

public Link delete (int key){
    Link current=head;
    Link previous=head;
    while (current.getData!=key)
    {
        if (current.next==null)
            return null;
        else
            if (current==head)
                head=head.next;
        else
                previous.next=current.next;



    }
     return current;




}

public void display (){
    System.out.println("first");
Link current=head;
while (current!=null)

{
    current.displaydata();
    current=current.next;


}

  System.out.println("");

}
public void addtorear(int x){
    Link newNode= new Link(x);
    if (head==null);
    head=newNode;
    Link current=head;
    current.next=newNode;
}
int get (int index){
    Link current=head;
    Link count=0;

    while (count<index)

    {
        if (current!=null)
        {
            current=current.next;
            count++;
        }
            else
            return 0;
    }
        if (current!=null)
        return current.getData;


        else


    return 0;

}

public int removefirst(){
 Link temp=head;
 if(head!=null)
 {
     head=head.next;
     return temp.getData;
 }
 else
     return 0;
 }


 public int removelast()
 {
 Link temp=head;
 if (temp==null)
     return 0;
 Link previous=head; //learn
 while(temp.next != null){
            previous = temp;
            temp = temp.next;
        }

        previous.next = null;
        return temp.getData;
    }


 public int indexof(int x){
     if (head=null)
         return 0;
     Link current=head;
     int count=0;
     while(current.getData!=x)
     {
         if (current.next=null)
             return 0;
         else
         {
             current=current.next;
          count++;
         }

     }
         return count;

     }


 public static void main(String [] args) {
     LinkList1 L=new LinkList1();

     L.insertfirst(5);
        L.insertfirst(15);
        L.insertfirst(25);


        L.display();
                Link a= L.search(5);
                if(a!=null)
                {
                    a.displaydata();
                    System.out.println("");

                }
                else
                    System.out.println("5 no found");


 Link b=L.search(20);
 if(b!=null)
 {
     b.displaydata();
     System.out.println("");
 }
 else
     System.out.println("20 not found");

 L.delete(15);
 L.display();
 L.insertfirst(50);
 L.display();
 L.delete(5);

        L.display();


        L.delete(50);

        L.display();

        L.addtorear(30);
        L.addtorear(22);

        L.display();


        for(int i = 0; i < 10; i++)
            System.out.println(L.get(i));


        System.out.println(L.removefirst());

        System.out.println(L.removefirst());


        System.out.println(L.removefirst());


        L.display();
    }

 }

public class Link {
    public int getData;
    public Link next;

    public Link (int d)
    {getData=d;}

    public void displaydata()          // display this link
      { System.out.print(getData + " "); }

   }  
> > 
> > 
> >     i am getting following errors
> >     C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:12: error: constructor Link in class Link cannot be applied to given types;
> >         Link newLink= new Link(data, head);
> >                           ^
> >       required: int
> >       found: int,Link
> >       reason: actual and formal argument lists differ in length
> >     C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:81: error: incompatible types: int cannot be converted to Link
> >         Link count=0;
> >                    ^
> >     C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:83: error: bad operand types for binary operator '<'
> >         while (count<index)
> >                     ^
> >       first type:  Link
> >       second type: int
> >     C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:89: error: bad operand type Link for unary operator '++'
> >                 count++;
> >                      ^
> >     C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:134: error: incompatible types: Link cannot be converted to boolean
> >          if (head=null)
> >                  ^
> >     C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:140: error: incompatible types: Link cannot be converted to boolean
> >              if (current.next=null)
> >                              ^
> >     6 errors
> >     
> > 
> > 
> 
> 

In this line Link newLink= new Link(data, head); you create new link with 2 parameters, but your constructor takes only 1 of type int public Link (int d)

Thanks now i am getting following error
C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:20: error: unreachable statement
    Link current=head;
         ^
1 error

ali11 : your entire code is wrong and full of (in Java's point of vue) illegal statements.

you obviously don't know how to work with classes versus primitives

hence this problem

  required: int
         found: int,Link
         reason: actual and formal argument lists differ in length
     C:\Users\sher\Documents\Bluetooth Folder\LinkList1.java:81: error: incompatible types: int cannot be converted to Link
          Link count=0;

and you try to write a LinkedList application?
start with the basics. learn the groundrules of Java, start of with smaller applications and build up to stuff like this. don't just decide to start Java and go immediately for more advanced material, that's when questions like this, which you would have been able to solve in about five seconds time (if needed at all, since you wouldn't have made those errors in the first place) if you had learned the basics of programming in Java.

This article has been dead for over six months. Start a new discussion instead.