Skip to main content
added 18 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am new to studying data structures although I have been programming in javaJava/androidAndroid since October. I feel itsit's time to advance, so I am starting data structures / algorithms this month. Since linked listlists are a big topic in interviews and I knew nothing about them I(I spent the past 3 days learning about it), hopefully my code is mostly correct, but please let me know if I am missing anything or not catching something, or if there's any way to improve it.

I am new to studying data structures although I have been programming in java/android since October. I feel its time to advance so I am starting data structures / algorithms this month. Since linked list are a big topic in interviews and I knew nothing about them I spent the past 3 days learning about it, hopefully my code is mostly correct, but please let me know if I am missing anything or not catching something or any way to improve it

I am new to studying data structures although I have been programming in Java/Android since October. I feel it's time to advance, so I am starting data structures / algorithms this month. Since linked lists are a big topic in interviews and I knew nothing about them (I spent the past 3 days learning about it), hopefully my code is mostly correct, but please let me know if I am missing anything or not catching something, or if there's any way to improve it.

Better title.
Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 193

first singly Singly linked list implementation

Source Link
gallly
  • 205
  • 2
  • 5

first singly linked list implementation

I am new to studying data structures although I have been programming in java/android since October. I feel its time to advance so I am starting data structures / algorithms this month. Since linked list are a big topic in interviews and I knew nothing about them I spent the past 3 days learning about it, hopefully my code is mostly correct, but please let me know if I am missing anything or not catching something or any way to improve it

public class MyLinkedList<T> {

    private LinkNode<T> head;
    private int size;

    public MyLinkedList() {
        this.head = null;
        this.size = 0;
    }

    public void display() {
        if (head == null) {
            System.out.println("empty list");
        } else {
            LinkNode<T> current = head;
            while (current != null) {
                System.out.println(current.toString());
                current = current.getNext();
            }
        }

    }

    public void addEnd(T data) {
        if (head == null) {
            head = new LinkNode<T>(data, head);
        } else {
            LinkNode<T> current = head;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            LinkNode<T> newNode = new LinkNode<T>(data, null);
            current.setNext(newNode);
            size++;
        }
    }

    public void addFront(T data) {
        head = new LinkNode<T>(data, head);
        size++;
    }

    public void removeEnd() {
        if (head == null) {
            System.out.println("empty list");
        } else {
            LinkNode<T> current = head;
            LinkNode<T> previous = head;
            while (current.getNext() != null) {
                previous = current;
                current = current.getNext();
            }
            previous.setNext(null);
            size--;
        }
    }

    public void removeFront() {
        if (head == null) {
            System.out.println("empty list");
        } else {
            head = head.getNext();
        }
        size--;
    }

    public void removeNode(T data) {
        if (head == null) {
            System.out.println("empty list");

        } else {
            LinkNode<T> current = head;
            LinkNode<T> previous = head;
            while (current.getData() != data) {
                previous = current;
                current = current.getNext();
            }
            if (current == head) {
                head = head.getNext();
            }
            previous.setNext(current.getNext());
        }
        size--;
    }

    public LinkNode<T> getNode(T data) {
        LinkNode<T> current = head;
        if (head == null) {
            System.out.println("empty list");
            return null;
        } else {
            while (current.getData() != data) {
                if (current.getNext() == null) {
                    System.out.println("not found");
                    return null;
                }
            }
        }
        return current;
    }

    private static class LinkNode<T> {
        T data;
        LinkNode<T> next;

        private LinkNode(T data, LinkNode<T> next) {
            this.data = data;
            this.next = next;
        }

        public T getData() {
            return data;
        }

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

        public LinkNode<T> getNext() {
            return next;
        }

        public void setNext(LinkNode<T> node) {
            this.next = node;
        }

        public String toString() {
            return ("\nData: " + data);

        }

    }

    public static void main(String[] args) {
        MyLinkedList<String> list = new MyLinkedList<String>();
        list.addEnd("a");
        list.addEnd("aaa");
        list.addEnd("aaaaaa");
        list.removeNode("aaa");
        list.display();
    }

}

My main concern is how differently every one writes these implementations.