0

I am taking a course in Data Structures at University and I am having troubles understanding why my Singly Linked List is not following FIFO algorithm.

Here is my Node/PSVM class:

public class Node {

    protected int data;
    protected Node next;

    Node(int element){
        this.data = element;
        next = null;
    }



    public static void main(String[] args) {
        LinkedList ll = new LinkedList();

        ll.addElement(300);
        ll.addElement(600);
        ll.addElement(900);
        ll.addElement(1200);


        ll.printList();
    }
}

This is my Linked List Class:


public class LinkedList {

    // create a reference of type node to point to head
    Node head;

    // keep track of the size of ll
    int size = 0;

    void printList() {
        Node n = head;

        for (int i = 0; i < llSize(); i++) {
            System.out.print(n.data + " ");
            n = n.next;

        }
        System.out.println("");
    }


    int llSize() {
        return this.size;
    }

    boolean isEmpty() {
        return size == 0;
    }

    void addElement(int element) {

        if (isEmpty()) {
            head = new Node(element);

        } else {
            Node nNode = new Node(element);
            Node current = head;

            while(current.next != null){
                current = current.next;
            }

            current.next = nNode;

        }

        this.size++;

    }


}

Sorry in advance if this is a basic question/problem. I have asked my professor and she sent me a YouTube link which really didn't help.

Thank you for your time.

7
  • 2
    No need to apologize. But you did post the same code twice. You may want to show expected/actual inputs and outputs though Commented Feb 28, 2020 at 20:17
  • Oops, sorry. I just edited it and added my LL class. Commented Feb 28, 2020 at 20:29
  • 1
    I don't see a bug in the code. What's the output you're getting from your printList call and what are you expecting? Side note: it's a little odd to put your main inside the Node class - not a bug, just unexpected. Commented Feb 28, 2020 at 20:35
  • 1
    I also don't see any logic errors in what you've posted. One suggestion though, it'd be more efficient to keep a separate reference to the tail node, rather than traversing the entire list every time you add an element. Commented Feb 28, 2020 at 20:37
  • Thanks for all the resposes This is my output: 1200 900 600 300 Shouldn't it be outputting in the same order it was inserted? FIFO? Also, I could put my main in a different file to keep it separate. Commented Feb 28, 2020 at 20:40

1 Answer 1

1

The code has no bugs.

For the list to behave as FIFO, nodes will be added to one end and deleted from the opposite end.

Therefore, you will have to implement a delete operation. You can maintain separate reference to the head and tail node.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.