package rizal;
public class Main {
public static void main(String[] args)
int x =5;
System.out.println(x);
}
}
DoubleList Class
package rizal;
public class DoubleList {
int awal, akhir;
int jnode;
int max_node=100;
Node item[]=new Node[max_node];
public DoubleList(){
jnode=0;
awal=-1;
akhir=-1;
for(int i=0;i
item[i]=new Node(0);
}
}
public int getJNode(){
return(jnode);
}
public void insert (int data, int pos){
if(jnode==0){
awal=0;
akhir=0;
item[0].setData(data);
jnode++;
}
else{
if (jnode!=max_node){
int p=FindEmpty();
if (pos==0){
item[p].setData(data);
item[p].setNext(awal);
item[awal].setPrev(p);
awal=p;
jnode++;
}
else if(pos>=jnode){
item[p].setData(data);
item[p].setPrev(akhir);
item[akhir].setNext(p);
akhir=p;
jnode++;
}
else {
if (pos>jnode/2){
int i =akhir;
for (int j=0;j
i=item[i].getPrev();
}
item[p].setData(data);
item[p].setNext(i);
item[p].setPrev(item[i].getPrev());
item[item[i].getPrev()].setNext(p);
item[i].setPrev(p);
jnode++;
}
else {
int i=awal;
for(int j=0;j
i=item[i].getNext();
}
item[p].setData(data);
item[p].setPrev(i);
item[p].setNext(item[i].getNext());
item[item[i].getNext()].setPrev(p);
item[i].setNext(p);
jnode++;
}
}
}
else {
System.out.println("Double List is Full!!!!");
}
}
return;
}
private int FindEmpty () {
int i;
for (i=0;i
if (i!=akhir && item[i].getNext()==-1){
return(i);
}
}
return(-1);
}
}
Node Class
package rizal;
public class Node {
private int prev, data, next;
public Node (int n){
data = n;
prev = -1;
next = -1;
}
public void setData(int n){
data = n;
}
public int getData(){
return (data);
}
public void setPrev(int n){
prev = n;
}
public void setNext(int n){
next = n;
}
public int getPrev(){
return (prev);
}
public int getNext(){
return (next);
}
}
Tidak ada komentar:
Posting Komentar