package linklist_insert;
import java.util.*;
public class SingleLinkedList
{
public
static void main(String[]args)
{
System.out.println("Linkedlist");
LinkedList
List = new LinkedList();
int a=3, b=5, c=9, d=11; //data sebelum di tambahkan
int
size;
List.add(a);
List.add(b);
List.add(c);
List.add(d);
System.out.print("\n
Sebelum di insert"+List);
System.out.print("\n
insert 7");
int
e=7;//data
yang akan di tambahkan
List.add(2,
e);
System.out.println("\n
setelah di urutkan"+List);
}
}
class Node
{
Object data;
Node next;
Node prev;
}
public class DoubleCircularList
{
Node head;
public DoubleCircularList()
{
head = null;
}
public boolean IsEmpty()
{
return (head==null);
}
public Object GetFirst()
{
return head.data;
}
public boolean IsNotEmpty()
{
return (head!=null);
}
public void InsertLast(Object o)
{
Node new_n = new Node();
new_n.data = o;
if (IsEmpty())// List Masih kosong
{
head=new_n;
new_n.next=head;
new_n.prev=head;
}
else
{
// List tak kosong
new_n.next=head;
new_n.prev=head.prev;
head.prev.next=new_n;
head.prev=new_n;
}
}
public void InsertFirst(Object o)
{
InsertLast(o);
head=head.prev;
}
public void print(String str)
{
System.out.println(str);
Node n = head;
if (IsNotEmpty())
{
Object data;
{
System.out.println(n.data);
n = n.next;
}
while (n != head);
}
else
{
System.out.println("List Kosong");
}
}
public static void main( String [ ] args )
{
DoubleCircularList dcl=new DoubleCircularList();
dcl.InsertFirst(10);
dcl.InsertFirst(20);
dcl.print("Double Cicular List");
}
}
No comments:
Post a Comment