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?
-
\$\begingroup\$ You should have called it very basic instead of .. :p Are you planning a follow-up with full implementation? \$\endgroup\$dfhwze– dfhwze2019年07月25日 08:37:34 +00:00Commented Jul 25, 2019 at 8:37
-
\$\begingroup\$ yes @dfhwze.. i am learning datastructures so was trying it \$\endgroup\$gss– gss2019年07月26日 09:09:54 +00:00Commented Jul 26, 2019 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– allo2019年07月26日 10:38:28 +00:00Commented Jul 26, 2019 at 10:38
4 Answers 4
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)
overwhile(true)
andcurrentNode
should start withfirstNode
and be chained inside the while-loop ascurrentNode = 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 calledhead
- class
linkedlist
should be calledLinkedList
- get rid of unwanted blank lines
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
.
You can have following changes:
- createNode size++ done outside of if..else..
- In Node constructor if param next is always null then no need of this param.
- class and method name should be in camelcase as per java convention.
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.
Explore related questions
See similar questions with these tags.