1

I am trying to create a doubly linked list in java and just need a little guidance as too where I am going wrong.I also wanted to be able to set a capacity for the list as well.

This is what I have so far:

public class LRU(int capacity){
    int data;
    Node previous;
    Node next;

    public void Node(int data){
        this.data = data;


     }
    Node head,tail = null;

    public void addNode(int data){
        Node newNode;
        newNode = new Node(data);

    if(head == null){
        head = tail = newNode;

        head.previous = null;

        tail.next = null;
    }else{
        tail.next = newNode;
        newNode.previous = tail;
        tail = newNode;
        tail.next= null;
    }
}
2

2 Answers 2

0

In fact your doubly linked list should have the following variables:

  1. int size - it's size of your list, it allows you to return size with O(1) complexity
  2. Node head - it's link to head of your list (first Node)
  3. Node tail - it's link to tail of your list (last Node)

Previous and Next Nodes should be declared in class 'Node'. It means that every node will have link to previous and next node.

Here is small example, but it's better to use generics to make your list more flexible (then it could support different types, not only int):

public class DoublyLinkedList {
    private int size;
    private Node head;
    private Node tail;

    public void addFirst(int value) {
        size++;

        // already have head (list not empty)
        if (head != null) {
            // creating new "head" element, old "head" now has previous value
            head.previous = new Node(null, value, head);
            head = head.previous; // update head
            return;
        }

        // empty list, so create first node
        head = new Node(null, value, null);
        tail = head; // because we have only one element, and it also becomes tail
    }

    public void add(int value) {
        size++;

        // already have tail (list not empty)
        if (tail != null) {
            tail.next = new Node(tail, value, null); // creating new "tail" node with given value, old "tail" now has next value
            tail = tail.next; // update tail
            return;
        }

        // empty list, so create first node
        if (head == null) {
            head = new Node(null, value, null);
            tail = head; // because we have only one element, and it also becomes tail
        }
    }

    public int getSize() {
        return size;
    }

    private static class Node {
        private Node previous;
        private Node next;
        private int data;

        public Node(Node previous, int data, Node next) {
            this.previous = previous;
            this.data = data;
            this.next = next;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

class List {

// nested class
public class Node {
    Node next;
    Node previous;
    float data;

    public Node(Node next, float data) {
        this.next = next;
        this.data = data;
    }

    public Node(float elem) {
        this(null, elem);
    }
}

private Node first;
private Node currentPosition;

int size;

public void forward(int numPositions) {
    if (numPositions > 0 && first != null) {
        int positionsForward = numPositions;
        if (currentPosition == null) {
            currentPosition = first;
            positionsForward--;
        }
        while (currentPosition.next != null && positionsForward > 0) {
            currentPosition = currentPosition.next;
            positionsForward--;
        }
    }
}

public void insert(float data) {
    Node node = new Node(data);

    if (currentPosition == null) {
        // inserts in first node
        node.next = first;
        if (first != null) {
            first.previous = node;
        }
        first = node;
    } else {
        // the node isn't the first.
        node.next = currentPosition.next;
        node.previous = currentPosition;
        if (currentPosition.next != null) {
            currentPosition.next.previous = node;
        }
        currentPosition.next = node;
    }

    // updates current position
    currentPosition = first;
    size++;
}

public Object extract() {
    Object data = null;

    if (currentPosition != null) {
        data = currentPosition.data;

        // 'delete' the node
        if (first == currentPosition) {
            first = currentPosition.next;
        } else {
            currentPosition.previous.next = currentPosition.next;
        }

        if (currentPosition.next != null) {
            currentPosition.next.previous = currentPosition.previous;
        }

        // next position
        currentPosition = currentPosition.next;
    }

    size--;
    return data;
}

public Object pop(){

    return currentPosition.data;

}

public void divide(){ //questao 1

    List half2 = new List();

    for(int i = 0; i < Math.floorDiv(size, 2); i++){

        half2.insert((Float) this.extract());

    }

    //debugar

    for(int i = 0; i < half2.size; i++){

        System.out.println(half2.extract());

    }

}

public float soma(){ //questao 2

    float sum = 0f;

    currentPosition = first;

    for (int i = 0; i < size; i++){

        sum += (Float) this.pop();

        this.forward(1);

    }

    currentPosition = first;

    return sum;

}

public void extrairMenores(float check){ //questao 3

    currentPosition = first;

    for(int i = 0; i < size; i++){

        if(currentPosition.data < check){

            System.out.println("if aq");

            this.extract();

        } else{

            System.out.println("else aq");

            this.forward(1);

        }

    }

    currentPosition = first;

}

public boolean igualdadeCheck(List outra_lista){ //questao 4

    currentPosition = first;

    outra_lista.currentPosition = first;

    for(int i = 0; i < size; i++){

        if(currentPosition.data - outra_lista.currentPosition.data != 0){

            return false;

        }

        forward(1);

        outra_lista.forward(1);

    }

    currentPosition = first;

    return true;

}

}

Besides the "extra" methods, the insert and extract methods may help you. I use an method to move between the items. I think that using "first" and "currentPosition" attributes makes structuring the methods easier. At each method including node referencing or beeing referenced you need to check if you re not having the possibility of a null referencing something else (it will "break" your program); but if a node refence a null, there is no problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.