Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Created June 23, 2011 07:27
Show Gist options
  • Save wingyplus/1042092 to your computer and use it in GitHub Desktop.
Save wingyplus/1042092 to your computer and use it in GitHub Desktop.
Learning Linklist implement with java language
package Main;
public class main {
public static void main (String[] args) {
Node list = new Node ();
list.add(100);
System.out.println (list.getN());
System.out.println (list.getNext());
list.add(200);
System.out.println (list.getNext().getN());
System.out.println (list.getNext().getNext());
}
}
package Main;
public class Node {
private int n;
private Node next;
public void add (int num) {
Node nextNode = this;
while (nextNode.next != null) {
nextNode = nextNode.next;
}
nextNode.n = num;
nextNode.next = new Node();
}
public void insert (int index, Node node) {
Node nextNode = this;
int count = 0;
while (count != index) {
nextNode = nextNode.next;
count++;
}
node.next = nextNode.next;
nextNode.next = node;
}
public void delete (int index) {
Node pNode = this;
Node nNode = pNode;
int count = 0;
while (count != index) {
nNode = pNode;
pNode = pNode.next;
count++;
}
nNode.next = pNode.next;
}
public void print () {
Node node = this;
while (node.next != null) {
System.out.println (node.n);
node = node.next;
}
}
public int getN () {
return n;
}
public Node getNext () {
return next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment