package struktur_data;
public class singlelinkedlist {
Node head;
class Node{
int data;
Node next;
Node(int d){data = d;next=null;}
}
public void push(int new_data){
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void insertafter(Node prev_node,int new_data){
if (prev_node == null){
return;
}
Node new_node = new Node(new_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
public void append(int new_data){
Node new_node = new Node(new_data);
if(head == null){
head = new Node(new_data);
return;
}
new_node.next = null;
Node last = head;
while(last.next != null)
last = last.next;
last.next = new_node;
return;
}
public void printlist(){
Node tnode = head;
while (tnode != null){
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String[] args) {
singlelinkedlist llist = new singlelinkedlist();
llist.append(2);
llist.push(4);
llist.push(7);
llist.append(5);
llist.insertafter(llist.head, 8);
System.out.println("\nCreated Linked list : ");
llist.printlist();
System.out.println("\n");
}
}
Berikut Outputnya :
Videonya ada dibawah :
Semoga Bermanfaat.......
Tidak ada komentar:
Posting Komentar