Hii every One

i create Doubly Linked List with all methods i need , but is still one question which i really did not understand it very well it said : write a method to create linked list from double linked list using the methods available in myLinkedList(which have the methods that i created), and java.util.LinkedList ?

can any body explain to me this question ?

Recommended Answers

All 14 Replies

It would be good to see the code that you have, but as a guess, it sounds like you are supposed to iterate through your doubly-linked list and add each element to a java.util.LinkedList. Could it be that simple, or am I assuming too much?

Look this is my main code , i do not have to post another class code c'uase they work probley with me , but when i created the single LL , that i only imported from lib.
i got an error .. how i can create Single LL with Double LL methods.

public static void main(String[] args) {
        DLL dll = new DLL();
        LinkedList L1 = new LinkedList();


        dll.insertFirst("A");
        dll.insertFirst("b");
        dll.insertFirst("c");
        dll.insertLast(1);
        dll.insertLast(3);
        dll.insertLast(5);

        dll.deleteFirst();
        dll.deleteLast();

        dll.printFirst();
        dll.printLast();
        
        
        L1.addFirst("I");
        L1.addFirst("s");
        L1.addFirst("h");
        L1.printFirst();

    }

By the way i created single list by my self but i can not use methods that in the lib.

so i have write method this first require.
by using methods in DLL and java.util.LinkedList.

i do not have a lot of info about Linked list , but i know that ..
i can create simple linked list without using " .getPrev Node" ..
so the first think i think about it is write this method in the main to create Single Linked List ..

public static LinkedList singLL(LinkedList L1)
{

int a = L1.size();
for(int K =0;K<=a;K++)
{
L1.addFirst("A");
}
System.out.println(L1.getFirst());
return L1;


}

but my problem now is /// how i can use DLL methods for SLL that i ALREADY created !!


{ can any one here help me please } !!

@dononelson has already given a good answer to this "iterate through your doubly-linked list and add each element to a java.util.LinkedList".
Do you have methods in your DLL to (a) get the first element and (b) get the next element? (If not, your implementation is incomplete.) You can use those to retrieve all the elements in your DLL one at a time in a loop and add them one at a time in the same loop to the LinkedList.

but excuse me how i can ititerate through my doubly-linked ?

System.out.println("Iterating with the help of for loop");
                for (int i=0; i< dll.size(); i++)
                {
                                L1.add("1");
                                L1.add("2");
                                L1.add("3");
                                L1.add("4");
                                L1.add("5");
                }

IS THAT WHAT DO YOU MEAN !

No no no, you're not reading the posts.
What methods do you have in your DLL to RETRIEVE the elements that are already in the DLL?

These are all methods in myDoubleLinkeList :

public class DLL
{
        private Dnode first;
        private Dnode last;

     public DLL()
     {
        first = null;
        last = null;
     }

  public boolean isEmpty()

  {
    return first == null;
  }

    public void insertFirst(Object d)

    {
    Dnode newLink = new Dnode(d);
    if (isEmpty())
      last = newLink;
    else
      first.setBack(newLink);
      newLink.setNext(first);
      first = newLink;
  }

    public void insertLast(Object d)
    {
    Dnode newLink = new Dnode(d);
    if (isEmpty())
      first = newLink;
    else {
      last.setNext(newLink);
      newLink.setBack(last);
    }
    last = newLink;
  }


     public void printFirst()
     {
        System.out.print("print first"+" ");
        Dnode current = first;
            while (current != null)
                {
                    System.out.print(current.getData());
                    current = current.getNext();
                }
                System.out.println("");
     }

  public void printLast()
  {
        System.out.print("print last"+" ");
        Dnode current = last;
        while (current != null){
        System.out.print(current.getData());
        current = current.getBack();
    }
    System.out.println("");
  }

    public Dnode deleteFirst(){
    Dnode curr = first;
    if (first.getNext() == null)
      last = null;
    else
      first.getNext().setBack(null);
      first = first.getNext();
    return curr;
  }

  public Dnode deleteLast(){
    Dnode curr = last;
    if (first.getNext()==null)
    {
      first = null;
    }
    else
      last.getBack().setNext(null);
    last = last.getBack();
    return curr;
  }

    public int size()
    {
        int count=0;
        Dnode curr = first;
        if (first.getNext()==null)
        {
             count++;
             curr = curr.getNext();
        }
    System.out.println(count);
    return count;
  }

}

If you look in printFirst you will see methods that (a) get the first element and (b) can be used in a loop to get the next element.
You can use those methods to get all the elements and add them to a LinkedList - the logic will be just like printFirst (which is mis-named because it actually prints all the elements) except that instead of printing each element you will be adding them to a LinkedList.

now it's work correctly , thank you very much =)

Excellent! Time to mark this thread as solved?

yup , you can mark it .. ,

.. sorry, it doesn't work that way. Only the originator (you) or someone with admin status (not me) can do that...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.