How to create customized singly linked list


Linked list work on the concept of FIFO – First in First out. i.e the element inserted first will be the one retrieved first.

Create a class which will act as data structure and act as a single Node.

 public class MyList {

 

            public Object ob;

            public MyList next;

           

            public MyList(Object ob){

                        this.ob=ob;

            }

}

Now create a class to implement the basic functionality:

public class MyLinkedList {

 

            private MyList first=null;

            private MyList last=null;

           

            public MyLinkedList (){

                       

            }

            public void add(Object o){

                        if(first == null){

                                    first = last= new MyList(o);

                        }else{

                                    last.next = new MyList(o);

                                    last= last.next;

                        }

            }

            private void getData(){

                        MyList temp =first;

                        do{

                                    System.out.println(temp.ob + ” “);

                                    temp=temp.next;

                        }while(temp != null);

                       

            }

            private void delete(Object o){

                        MyList temp =first;

                        MyList prev =null;

                       

                        do{

                                    if(temp.ob.equals(o)){

                                                prev.next=temp.next;

                                                break;

                                    }

                                    prev = temp;

                                    temp=temp.next;

                        }while(temp != null);

                       

            }

 

public static void main(String args[]){

                         MyLinkedList lst = new MyLinkedList();

                         lst.add(6);

                         lst.add(62);

                         lst.add(16);

                         lst.add(65);

                         lst.add(26);

                         lst.add(25);

                         System.out.println(“After Insert”);

                         lst.getData();

                         lst.delete(16);

                         System.out.println(“After Delete”);

                         lst.getData();

                         

            }

}

The output will be :

After Insert

6 62 16 65 26 25

 

After Delete

6 62 65 26 25

 

 

3 thoughts on “How to create customized singly linked list

  1. I must tell you that it’s hard to find your articles in google, i found this one on 22
    spot, you should build some quality backlinks in order to
    rank your webiste, i know how to help you,
    just type in google – k2 seo tips

  2. I read a lot of interesting content here. Probably you spend a
    lot of time writing, i know how to save you a lot of time, there is
    an online tool that creates unique, SEO friendly posts in minutes, just type in google – laranitas free content source

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s