Results 1 to 6 of 6
  1. #1
    Thomaat1 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default how to delete/search double linked list

    Hello everyone!

    I'm trying to create a double linked list with functions like delete last, delete first, add first, add last.

    However the search and delete any does not work. I've tried a few different methods which are posted below in the code (as comments).

    Any help would be greatly appreciated! :)


    PHP Code:
    public class DoublyLinkedList {
      private static Node first; 
    
      private static Node last; 
      
    
      public DoublyLinkedList() {
        first = null; 
        last = null;
      }
    
      public boolean isEmpty(){
        return first == null;
      }
    
      public void addToFront(String dd){
        Node newNode = new Node(dd); 
    
        if (isEmpty()) 
          last = newNode; 
        else
          first.previous = newNode; 
        newNode.next = first; 
        first = newNode; 
        
      }
    
      public void addToEnd(String dd){
        Node newNode = new Node(dd); 
        if (isEmpty()) 
          first = newNode; 
        else {
          last.next = newNode; 
          newNode.previous = last; 
        }
        last = newNode; 
       
      }
    
      public Node deleteFirst(){ 
        Node temp = first;
        if (first.next == null) 
          last = null; 
        else
          first.next.previous = null;
        first = first.next; 
        
        return temp;
        
      }
    
      public Node deleteLast(){ 
        Node temp = last;
        if (first.next == null)
          first = null; 
        else
          last.previous.next = null;
        last = last.previous; 
        
        return temp;
      
      }
      
    
     /* public static Node search (Node first, String item){
            for(int i= 0; i<10;i++){
                if (first.next.dData=="26"){
                    System.out.println("blah");
                   return first;}
                   first=first.next;
                }
                return null;
            }
      /*public static Node delete(Node first, String data) {
           if (first.dData == data)
                return first.next;
                
           Node previous = null;
           Node current = first;
           System.out.println("1");
           
           while (current != null) {
                if (current.dData == "26") {
                        previous.next = current.next;
                         System.out.println("1");
                        return first;
                    }
                else {
                        previous = current;
                        current = current.next;
                    }
                }
                 
                return first;
            }*/
      
      /*public Node deleteKey(String key){
        Node current = first; 
        while (current.dData != key)
        {
          current = current.next;
          if (current == null)
            return null; // cannot find it
        }
        if (current == first) // found it; first item?
          first = current.next; 
        else
          current.previous.next = current.next;
    
        if (current == last) // last item?
          last = current.previous; 
        else
          // not last
          current.next.previous = current.previous;
        return current; // return value
      }*/
    
      public void print() {
        System.out.print("List (first to last): ");
        Node current = first; // start at beginning
        while (current != null) // until end of list,
        {
          current.displayNode();
          current = current.next; // move to next Node
        }
        System.out.println("");
      }
    
      public void printFromLast() 
      {
        System.out.print("List : ");
        Node current = last;
        while (current != null){
          current.displayNode();
          current = current.previous;
        }
        System.out.println("");
      }
    
      public static int count(Node x)
      {   // count number of elements in list
            // when we start counting at Node x
            if (x==null) return 0;
            return 1 + count(x.next);
      }
     
      
      
      public static void main(String[] args) {
        DoublyLinkedList theList = new DoublyLinkedList();
    
        int b = 22;
        int c = 44;
        for(int i = 0; i < 6; i++)
        {
        b = i+22;
        c = i+44;
        String bString=Integer.toString(b);
        String cString=Integer.toString(c);
        theList.addToFront(bString);
        theList.addToEnd(cString);
    }
        theList.print();
        theList.printFromLast();
    
        System.out.println("The length of the list before deleting = " + count(first));
        
        theList.deleteFirst();
        theList.deleteLast();
      //  theList.deleteKey("25");
    
       //delete(first, "26");
        
        Node found = search(first, "23");
        
       // System.out.println(found.dData);
        
        theList.print();
    
        theList.print();
       
       System.out.println("The length of the list = " + count(first));
       
      }
    
    }
    
    class Node {
      public String dData; // data item
    
      public Node next; // next Node in list
    
      public Node previous; // previous Node in list
    
      public Node(String d)
      {
        dData = d;
      }
    
      public void displayNode(){
        System.out.print(dData + " ");
      }
    
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    9

    Default

    Sorry But where do you store a data from this classes?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    11

    Default

    Quote Originally Posted by Petr View Post
    Sorry But where do you store a data from this classes?

    inside the class Node is a field named dData where you can store a string.

    @Toomat1: use first.next.dData.equals("26") inside the search method and not ==
    Last edited by j2me64; 04-16-2011 at 02:36 PM.

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    9

    Default

    No I mean other. I mean follows. Topic starter say that he want to create some classes, which implement a linked list.
    The Linked list this list, where previous node links on next node. This structure does not allow get some node for index.
    for example
    3 -> 5 -> 0 -> 1
    The same way you need create class Node, which contain value and links on previous and next node and class, which store whole elements.
    So I don't understand where store whole elements.
    Can anyone explain where store whole elements in post above code?
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    Thomaat1 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks alot for the replies! with the comment of jw2me64 and just slowly checking every line I figured out how to solve delete + search methods.

    Petr, you mean I can't for example search for a location nr '1' for example or? I cannot for example delete nr '50' in the list of 1 to 100 right? I probably need to add a second variable to the Node class which keeps track of its position in the list. I think that's not too hard, but in this case not required.

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    9

    Default

    Not I mean other, how you know Java has some collections, which implement interface List. I mean about ArrayList and LinkedList.
    Let's imagine follows situation, you need add a node in center a list.
    If you use ArrayList you need create new ArrayList for it. but if you use LinkedList you just point that needs node will links on other node it all.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. Replies: 8
    Last Post: 11-04-2011, 08:30 PM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  4. Java double linked list
    By Clown in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 04:04 PM
  5. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •