8
\$\begingroup\$

Here is implementation for singly linkedlist. Any suggestions are most welcome

package linkedlist;

import java.util.Scanner;

public class linkedlist {
    private int size=0;
    private Node newNode,oldNode,firstNode,displayNode;

    public void createNode(String value) {
        if(size==0) {
            newNode=new Node(value,null);
            firstNode=newNode;
            size++;
        }
        else {
            oldNode=newNode;
            newNode=new Node(value,null);
            oldNode.setNextNode(newNode);
            size++;

        }

    }

    public void DisplayLinklist() {
        displayNode=firstNode;
        while(true) {
            System.out.println(displayNode.getItem());
            displayNode=displayNode.next;
            if(displayNode.next==null) {
                System.out.println(displayNode.getItem());
                break;
            }

        }
    }


    private class Node{
        private String item;
        private Node next;

        Node(String item,Node next){
            this.item=item;
            this.next=next;
        }

        public String getItem() {
            return this.item;
        }

        public void setNextNode(Node address) {
            this.next=address;
        }
    }

    public static void main(String[] args) {
        linkedlist mylinklist=new linkedlist();
        Scanner in=new Scanner(System.in);
        System.out.println("Enter the string value you want to save or press 'n'");
        String input=in.next();
        while(!input.equalsIgnoreCase("n")) {
            mylinklist.createNode(input);
            System.out.println("Enter the string value you want to save or press 'n'");
            input=in.next();
        }

        mylinklist.DisplayLinklist();

    }

}

This is very basic version and first time I am doing Linkedlist. I am not using any iteration, but just wanted to confirm am I on the right path?

\$\endgroup\$
  • \$\begingroup\$ You should have called it very basic instead of .. :p Are you planning a follow-up with full implementation? \$\endgroup\$ – dfhwze Jul 25 '19 at 8:37
  • \$\begingroup\$ yes @dfhwze.. i am learning datastructures so was trying it \$\endgroup\$ – gss Jul 26 '19 at 9:09
  • 1
    \$\begingroup\$ Here is an interesting link about what Linus Torvalds considers "good taste" code. He uses a linked-list as example to show, how to improve your code, so it is more clean. You may also be interested in the slashdot article/comments linked in the blog article. grisha.org/blog/2013/04/02/linus-on-understanding-pointers \$\endgroup\$ – allo Jul 26 '19 at 10:38
13
\$\begingroup\$
  • size++ should be extracted from both branches and called only once
  • there is no need to cache instance variables where local method variables should be used instead: newNode,oldNode,displayNode
  • prefer while(currentNode != null) over while(true) and currentNode should start with firstNode and be chained inside the while-loop as currentNode = currentNode.next
  • you should also provide remove and iterator functionality
  • DisplayLinklist should not be inside this class and it should be camel cased
  • variable firstNode is usually called head
  • class linkedlist should be called LinkedList
  • get rid of unwanted blank lines
\$\endgroup\$
8
\$\begingroup\$

I can add that instead of DisplayLinklist() you should override toString() that returns a String representation of the linked list. then the caller can decide where and how to display this String.

\$\endgroup\$
7
\$\begingroup\$

You can have following changes:

  1. createNode size++ done outside of if..else..
  2. In Node constructor if param next is always null then no need of this param.
  3. class and method name should be in camelcase as per java convention.
\$\endgroup\$
6
\$\begingroup\$

This isn't the really basic linked list. For the most basic way of doing linked lists, you would get rid of class linkedlist entirely, and have class Node be the entire implementation. You need both getters and setters for both item and next, and those four plus the constructor are really the only primitive operations there are. Every other list operation is an algorithm built on those 5 primitives.

It sounds like what you're trying to do is an exercise in stripping the idea of linked lists down to its essence. To do that, you need to wrap your head around the fact that there's no difference between having "a list" and having "the node at the head of a list"; each node is a list; and a list is nothing but the nodes and items that make it up.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.