I am new to data structures. I am very curious to learn data structures, but i didnt find any healthy tutorial for that so I am posting it here thinking someone would help me. I know theory of linked list but m totally blank while implementation. If Someone can make me understand how it works that would be really helpful for me.Like, how to traverse through Linked List ,insert and delete. Please provide me a running code so that its easy for me to understand. I KNOW there are lot of peoples who will think to mark this question as a duplicate and downvote this. Rather than finding mistakes if you guys provide me a good solution that would be really Helpful. Thanks.
Asked
Active
Viewed 1,115 times
-3
-
It's a big ask my friend. Good luck. – Robert Moskal Jul 09 '16 at 15:58
-
1Take a look at `java.util.LinkedList`. There is probably some (if not a lot of) overhead. But this should give you some point to start from. – dpr Jul 09 '16 at 16:11
2 Answers
0
Well to start, if you go on youtube and search up Derek Banas you will find a great video just on LinkedLists, and how to implement them. He talks kind of fast, but the content is very well taught in my opinion. Just following along in his videos and after the video I think you will have a better understanding.

beastlyCoder
- 2,349
- 4
- 25
- 52
0
Simple implementation of a linked list showing append, insert, delete and iterate. There are inefficiencies, they're for you to figure out :) Go do some research to see how to make it better.
public class LinkedList {
public static class Node {
private Object data;
private Node next = null;
public Node(Object data) {
this.data = data;
}
public void setNext(Node n) {
next = n;
}
public Node getNext() {
return next;
}
public Object getData() {
return data;
}
}
public static void iterate(Node n) {
while (n != null) {
System.out.println(n.getData());
n = n.getNext();
}
}
public static void insert(Node newNode, Node after) {
newNode.setNext(after.getNext());
after.setNext(newNode);
}
public static void delete(Node toDelete, Node root) {
Node n = root;
while (n.getNext() != toDelete) {
n = n.getNext();
}
n.setNext(toDelete.getNext());
}
public static void main(String[] args) {
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
// append
a.setNext(b);
b.setNext(c);
// iterate
System.out.println("Initial list");
iterate(a);
// insert d after b
Node d = new Node("d");
insert(d, b);
// iterate again
System.out.println("After insert");
iterate(a);
// delete d
delete(d, a);
// iterate again
System.out.println("After delete");
iterate(a);
}
}

adamreeve
- 376
- 1
- 4