简体   繁体   中英

Generic Doubly Linked List

For my personal practice I am trying to make a basic, generic doubly linked list and I want to know if the methods addtoHead() and addtoTail() I made are correct and efficient and if not what would be better? and how could I remove from the list for the methods removingDataAt(), removingFromTail(), removingFromHead()?

Node class:

public class PracticeNode<T> {

private T data;
private PracticeNode<T> next;
private PracticeNode<T> prev;

PracticeNode() {
    next = null;
    prev = null;
    data = null;
}

PratciceNode(T data) {

this(data, null, null);

}

PracticeNode(T data, PracticeNode<T> next, PracticeNode<T> prev) {
    this.data = data;
    this.next = next;
    this.prev = prev;
}

public void setNextNode(PracticeNode<T> next) {
    this.next = next;
}

public void setPrevNode(PracticeNode<T> prev) {
    this.prev = prev;
}

public void setData(T data) {
    this.data = data;
}

public PracticeNode<T> getNextNode() {
    return next;
}
public PracticeNode<T> getPrevNode() {
    return prev;
}

public T getData() {
    return data;
}

}

Linked list class:

public class PracticeLinkedList<T> {

private PracticeNode<T> head;
private PracticeNode<T> tail;

public void addtoHead(T data ) {
    PracticeNode<T> newnode=new PracticeNode<T>();
    if(head==null){
        head=newnode;
        tail=newnode;
        newnode.setNextNode(null);
        newnode.setPrevNode(null);
    }else{
         newnode.setNextNode(head);
         head.setPrevNode(newnode);
         head=newnode;
    }

}

public void addToTail(T data) {
    PracticeNode<T> newnode=new PracticeNode<T>();
     if(tail==null){
            head=newnode;
            tail=newnode;
            newnode.setNextNode(null);
            newnode.setPrevNode(null);
        }else{
            newnode.setPrevNode(tail);
            tail.setNextNode(newnode);
            tail=newnode;
        }


}

public T removingDataAt (int){
    //....
}

public T removingFromTail (){
    //....
}

public T removingFromHead (){
    //....
}
}

addToTail looks good to me.

With removingDataAt() , removingFromTail() , and removingFromHead() , you want to do what you have done with addToTail and addToHead. Because this seems like it is something from an assignment, I will not give the completed code, but just tell you how to do it.
I see that you have only the navigating instances of head and tail, I would recommend that you also implement a 'current' which will allow you to navigate through the List to do things such as removingDataAt(location) . I'm not sure about the most efficient method of removing things, but with Java, it does garbage collection automatically so you could simply remove from the head by using head = head.getNextNode() . Removing from tail is a similar story.
For removingDataAt() you will need a method to find the data data first, unless the use already knows what is in each location of the list. Perhaps something like find(object) which will return an error message on fail and move the 'current' instance to the found object otherwise. You'd implement it by using something like this:
for(current = head; current!=null; current = current.getNextNode())

Remember to check if there are any other items in the linked list.

Here is a solid implementation of doubly linked list: http://algs4.cs.princeton.edu/13stacks/DoublyLinkedList.java.html

Add method looks like this:

   // add element to list 
    public void add(Item item) {
        Node x = current.prev;
        Node y = new Node();
        Node z = current;
        y.item = item;
        x.next = y;
        y.next = z;
        z.prev = y;
        y.prev = x;
        N++;
        index++;
        lastAccessed = null;
    }

What to note here:

a> <b> <c> <e> <f> <g

if you want to add d , then find e and add d

    public void add(Item item) {
        Node x = current.prev; //new before element : c
        Node y = new Node();   //new node to store element
        Node z = current;      //new after element : e
        y.item = item;         //store value to the node : d.value=value
        x.next = y;            //link before element next : c>
        y.next = z;            //link element next  : d>
        z.prev = y;            //link after element previous : <e
        y.prev = x;            //link after element previous : <d
        N++;                   //increase number of elements on list counter
        index++;               //increase current position number 
        lastAccessed = null;
    }

now you should have :

a> <b> <c> <d> <e> <f> <g

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM