Last active
August 29, 2015 14:00
-
-
Save goFrendiAsgard/fb8e9b35cd9bac7dff89 to your computer and use it in GitHub Desktop.
Superb-Linked-List (in java). I don't like to code in java. However sometime I have to. This is an example of how to make a superb-linked-list in java. The linked-list has both queue & stack mechanism, plus it support to push data on the correct order.
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 prof_04_01; | |
class Node{ | |
int data; | |
Node next; | |
Node prev; | |
public Node(int new_data){ | |
data = new_data; | |
} | |
} | |
class LinkList{ | |
Node head, tail; | |
// constructor | |
public LinkList(){ | |
head = null; | |
tail = null; | |
} | |
// push | |
void push(Node new_node){ | |
if(head == null && tail == null){ | |
head = new_node; | |
tail = new_node; | |
}else{ | |
new_node.prev = tail; | |
tail.next = new_node; | |
tail = new_node; | |
} | |
} | |
// pop queue | |
Node queue_pop(){ | |
Node node = head; | |
if(head != null){ | |
head = head.next; | |
if(head != null){ | |
head.prev = null; | |
} | |
} | |
if(head == null){ | |
tail = null; | |
} | |
return node; | |
} | |
// pop stack | |
Node stack_pop(){ | |
Node node = tail; | |
if(tail != null){ | |
tail = tail.prev; | |
if(tail != null){ | |
tail.next = null; | |
} | |
} | |
if(tail == null){ | |
head = null; | |
} | |
return node; | |
} | |
// tampilkan | |
void print(){ | |
Node node = head; | |
System.out.println("Isi Link List : "); | |
while(node != null){ | |
System.out.println(" " + node.data); | |
node = node.next; | |
} | |
} | |
// push_sorted | |
void push_sorted(Node new_node){ | |
if(head == null && tail == null){ | |
head = new_node; | |
tail = new_node; | |
}else{ | |
Node node = head; | |
// penyisipan di tengah atau di awal | |
boolean inserted = false; | |
while(node != null){ | |
if(node.data > new_node.data){ | |
Node previous_node = node.prev; | |
new_node.next = node; | |
new_node.prev = previous_node; | |
node.prev = new_node; | |
if(previous_node != null){ | |
previous_node.next = new_node; | |
}else{ | |
head = new_node; | |
} | |
inserted = true; | |
break; | |
} | |
node = node.next; | |
} | |
// penyisipan di akhir, sama seperti push biasa | |
if(!inserted){ | |
push(new_node); | |
} | |
} | |
} | |
void delete(Node node){ | |
Node previous_node = node.prev; | |
Node next_node = node.next; | |
if(previous_node != null){ | |
previous_node.next = next_node; | |
} | |
if(next_node != null){ | |
next_node.prev = previous_node; | |
} | |
} | |
} | |
public class Prof_04_01 { | |
public static void main(String[] args) { | |
LinkList linklist = new LinkList(); | |
Node nanti_hilang = new Node(6); | |
linklist.push_sorted(new Node(5)); | |
linklist.push_sorted(new Node(7)); | |
linklist.push_sorted(nanti_hilang); | |
linklist.push_sorted(new Node(4)); | |
linklist.push_sorted(new Node(8)); | |
linklist.push_sorted(new Node(9)); | |
linklist.print(); | |
linklist.delete(nanti_hilang); | |
linklist.print(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment