Created
June 23, 2011 07:27
-
-
Save wingyplus/1042092 to your computer and use it in GitHub Desktop.
Learning Linklist implement with java language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Main; | |
public class DoubleLinkList { | |
private DoubleLinkList prev; | |
private int data; | |
private DoubleLinkList next; | |
public void add (int value) { | |
DoubleLinkList node = this; | |
DoubleLinkList prevNode = new DoubleLinkList(); | |
while (node.next != null) { | |
prevNode = node; | |
node = node.next; | |
} | |
node.prev = prevNode; | |
node.data = value; | |
node.next = new DoubleLinkList (); | |
} | |
public void print () { | |
DoubleLinkList node = this; | |
while (node.next != null) { | |
System.out.println (node.data); | |
node = node.next; | |
} | |
} | |
public int getData () { | |
return data; | |
} | |
public DoubleLinkList getNext () { | |
return next; | |
} | |
public DoubleLinkList getPrev () { | |
return prev; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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