Single dan Double OrderedLinkedList di java

Iseng posting tentang linkedlist java, mumpung sedang belajar itu juga di Algoritma dan Struktur Data. Itung itung menuhin blog juga ^_^
langsung aja, berikut listing programnya :
yg ini untuk file Node.java

public class Node {

	int data;
	Node next;
	Node prev;
	Node(int id){
		data=id;
	}
}

file AddtLinkedList.java

public class AddtLinkedList {

	static boolean kosongCek(Node head){
		return (head==null);
	}
	static void tukar(Node nd1, Node nd2){
		Node tmp = new Node(nd1.data);
		nd1.data=nd2.data;
		nd2.data=tmp.data;
	}
	static void tampil(Node head){
		Node current=head;
		while(current!=null){
			System.out.print(current.data+" ");
			current=current.next;
		}
		System.out.println();
	}
	static void tampil(Node head, Node tail, String a){
		if (a=="maju"){
			Node current=head;
			while(current!=null){
				System.out.print(current.data+" ");
				current=current.next;
			}
			System.out.println();
		} else if (a=="mundur"){
			Node current=tail;
			while(current!=null){
				System.out.print(current.data+" ");
				current=current.prev;
			}
			System.out.println();
		}

	}
}


file OrderedList.java

public class OrderedList {
	int jumlah;
	Node head, tail, current;
	OrderedList(){
		head=tail=current=null;
		jumlah=0;
	}
	void tambahAwal(int data){
		Node nd = new Node(data);
		if (AddtLinkedList.kosongCek(head)){
			head=tail=nd;
		} else {
			nd.next=head;
			head=nd;
		}
		jumlah++;
	}
	void tambahAkhir(int data){
		Node nd = new Node(data);
		if(AddtLinkedList.kosongCek(head)){
			head=tail=nd;
		} else {
			tail.next=nd;
			tail=nd;
		}
		jumlah++;
	}
	void tambahTengah(int data, int posisi){
		Node nd = new Node(data);
		if(AddtLinkedList.kosongCek(head)){
			head=tail=nd;
		} else if (posisi>jumlah){
			System.out.println("Posisi melebihi jumlah node, maka data di letakkan paling akhir!");
			tambahAkhir(data);
		} else {
			current=head;
			int i=1;
			while(i<(posisi-1)){
				current=current.next;
				i++;
			}
			nd.next=current.next;
			current.next=nd;
		}
		jumlah++;
	}
	void hapusTengah(int posisi){
		if(AddtLinkedList.kosongCek(head)){
			System.out.println("List masih kosong!");
		} else if (posisi>jumlah){
			System.out.println("Di luar jangkauan!");
		} else {
			current=head;
			int i=1;
			while(i<(posisi-1)){
				current=current.next;
				i++;
			}
			current.next=current.next.next;
		}
		jumlah--;
	}
	Node getHead(){
		return head;
	}
	Node getTail(){
		return tail;
	}
	int getJumlah(){
		return jumlah;
	}
}

file DoubleOrderedList.java

public class DoubleOrderedList {
	int jumlah;
	Node head, tail, current;
	DoubleOrderedList(){
		head=tail=current=null;
		jumlah=0;
	}
	void tambahAwal(int data){
		Node nd = new Node(data);
		if (AddtLinkedList.kosongCek(head)){
			head=tail=nd;
		} else {
			nd.next=head;
			head.prev=nd;
			head=nd;
		}
		jumlah++;
	}
	void tambahAkhir(int data){
		Node nd = new Node(data);
		if(AddtLinkedList.kosongCek(head)){
			head=tail=nd;
		} else {
			tail.next=nd;
			nd.prev=tail;
			tail=nd;
		}
		jumlah++;
	}
	void tambahTengah(int data, int posisi){
		Node nd = new Node(data);
		if(AddtLinkedList.kosongCek(head)){
			head=tail=nd;
		} else if (posisi>jumlah){
			System.out.println("Posisi melebihi jumlah node, maka data di letakkan paling akhir!");
			tambahAkhir(data);
		} else {
			current=head;
			int i=1;
			while(i<(posisi-1)){
				current=current.next;
				i++;
			}
			nd.next=current.next;
			nd.prev=current;
			current.next.prev=nd;
			current.next=nd;
		}
		jumlah++;
	}
	void hapusTengah(int posisi){
		if(AddtLinkedList.kosongCek(head)){
			System.out.println("List masih kosong!");
		} else if (posisi>jumlah){
			System.out.println("Di luar jangkauan!");
		} else {
			current=head;
			int i=1;
			while(i<(posisi-1)){
				current=current.next;
				i++;
			}
			current.next=current.next.next;
			current.next.prev=current;
		}
		jumlah--;
	}
	Node getHead(){
		return head;
	}
	Node getTail(){
		return tail;
	}
	int getJumlah(){
		return jumlah;
	}
}

file Main.java

public class Main {

	public static void main (String args[]) {

                OrderedList ol = new OrderedList();
		System.out.println("list awal...");
		ol.tambahAwal(3);
		ol.tambahAkhir(5);
		ol.tambahAkhir(7);
		ol.tambahAkhir(9);
		ol.tambahAkhir(0);
		AddtLinkedList.tampil(ol.getHead());
		System.out.println("disisipkan 4 pada posisi ke-2...");
		ol.tambahTengah(4, 2);
		AddtLinkedList.tampil(ol.getHead());

		DoubleOrderedList dol = new DoubleOrderedList();
		System.out.println("list awal...");
		dol.tambahAwal(3);
		dol.tambahAkhir(5);
		dol.tambahAkhir(7);
		dol.tambahAkhir(9);
		dol.tambahAkhir(0);
		AddtLinkedList.tampil(dol.getHead(), dol.getTail(), "maju");
		System.out.println("disisipkan 4 pada posisi ke-2...");
		dol.tambahTengah(4, 2);
		AddtLinkedList.tampil(dol.getHead(), dol.getTail(), "maju");
	}
}

untuk method method yang lain tinggal di masukkan sendiri 😀

Tinggalkan Balasan