HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Queue Implementation using a Linked List

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linkedusingimplementationlistqueue

Problem

For understanding the concepts, I've implemented the Queue data structures using a linked list. Is there anything to improve?

LinkList.java

public class LinkList {

    private static class Node {

        private final T data;
        private Node next;

        public Node(T data){
            this.data=data;
        }

        public void displayNode(){
            System.out.print(data+ " ");
        }

    }

    private Node first = null;
    private Node last = null;

    public boolean isEmpty() {
        return (first == null);
    }

    public  void addLast(T data) {
        Node n = new Node(data);
        if (isEmpty()) {
            n.next = first;
            first = n;
            last = n;
        } else {
            last.next = n;
            last = n;
            last.next = null;
        }

    }

    public void removeFirst() {

            Node temp = first;
            if (first.next == null)
                last = null;
            first = first.next;

        }

    public void displayList() {
        Node current = first;
        while (current != null) {
            current.displayNode();
            current = current.next;
        }
    }

}


LinkListQueue.java

public class LinkListQueue {

    LinkList newLinkList = new LinkList();

    public  void enqueue(T data) {
        newLinkList.addLast(data);
    }

    public void dequeue() {
        if(!newLinkList.isEmpty())
            newLinkList.removeFirst();

    }

    public void displayQueue() {
        newLinkList.displayList();
        System.out.println();
    }

    public boolean isEmpty(){
        return newLinkList.isEmpty();
    }

}


LinkListQueueDemo.java

```
public class LinkListqueueDemo {

public static void main(String[] args) {

LinkListQueue queueImpl = new LinkListQueue();

queueImpl.enqueue("A");
queueImpl.enqueue("B");
queueImpl.enqueue("C");
queueImpl.enqueue("D");
queueImpl.displayQueue();

Solution

BUG:

public void removeFirst() {

        Node temp = first;
        if (first.next == null)
            last = null;
        first = first.next;

    }


This crashes if you have an empty list. Add a check for first == null. Additionally... what's that temp variable for?

Don't define the Node as T, define the List to contain T.

public class LinkList


This because you use the T outside the Node class and you ended up having to add it to the addLast method.

if (isEmpty()) {
        n.next = first;
        first = n;
        last = n;
    } else {
        last.next = n;
        last = n;
        last.next = null;
    }


You already know that n.next is null. You also know that first is null, because that's what defines isEmpty().

So instead, try this:

if (isEmpty()) {
        first = n;
    } else {
        last.next = n;
    }
    last = n;
    last.next = null;


It does the same thing.

Design:

You have an insert (enqueue) method... but there's no way to retrieve an object that went into the Queue. removeFirst could return the object. This drastically cuts down on the uses of such an object (it's useless now).

Code Snippets

public void removeFirst() {

        Node temp = first;
        if (first.next == null)
            last = null;
        first = first.next;

    }
public class LinkList<T>
if (isEmpty()) {
        n.next = first;
        first = n;
        last = n;
    } else {
        last.next = n;
        last = n;
        last.next = null;
    }
if (isEmpty()) {
        first = n;
    } else {
        last.next = n;
    }
    last = n;
    last.next = null;

Context

StackExchange Code Review Q#62746, answer score: 16

Revisions (0)

No revisions yet.