0% found this document useful (0 votes)
57 views9 pages

Modul Praktikum Struktur Data Dan Algoritma

The document discusses the implementation of single linked lists, double linked lists, circular single linked lists, and circular double linked lists in Java. It includes the Node class implementation for each with data fields and references to next and previous nodes. Methods for each class are also outlined for adding and removing nodes from the front and back, printing the lists, and determining if the list is empty. Examples of using the different list classes in a main method are provided.

Uploaded by

yuly fadiahaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views9 pages

Modul Praktikum Struktur Data Dan Algoritma

The document discusses the implementation of single linked lists, double linked lists, circular single linked lists, and circular double linked lists in Java. It includes the Node class implementation for each with data fields and references to next and previous nodes. Methods for each class are also outlined for adding and removing nodes from the front and back, printing the lists, and determining if the list is empty. Examples of using the different list classes in a main method are provided.

Uploaded by

yuly fadiahaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Modul Pertemuan 2 Praktikum Struktur Data dan Algoritma

1. Single Linked List non Circular

Class SLLNC
public class SLLNC {

public int data;


public SLLNC next;
public SLLNC head;
public SLLNC bantu;

public SLLNC() {
}

public SLLNC(int d) {
data = d;
next = null;
}

public boolean jikaEmpty() {


if (head == null) {
return true;
} else {
return false;
}
}

public void tambahDepan(int dataBaru) {


SLLNC baru;
baru = new SLLNC(dataBaru);
baru.next = baru;

if (jikaEmpty() == true) {
head = baru;
head.next = null;
} else {
baru.next = head;
head = baru;
}
}

public void tambahBelakang(int databaru) {


//isi kan method untuk tambah belakang
}

public void hapusDepan() {


SLLNC hapus;

if (head.next == null) {
head = null;
} else {
hapus = head;
head = head.next;
hapus = null;
}
}
public void hapusBelakang() {
//isikan method untuk hapus belakang
}

public void hapusBelakang2() {


SLLNC hapus;

if (head.next == null) {
head = null;
} else {
bantu = head;
while (bantu.next.next != null) {
bantu = bantu.next;
}
bantu.next = null;
}
}

public void printDepan() {


if (jikaEmpty() == false) {
bantu = head;
while (bantu != null) {
bantu.displayNode();
bantu = bantu.next;
}
}
}

public void displayNode() {


System.out.print(data + " ");
}
}

Class SLLNCApp
public class SLLNCApp {

public static void main(String args[]){


SLLNC List = new SLLNC ();

List.tambahDepan(5);
List.tambahDepan(2);
List.tambahDepan(7);
List.tambahBelakang(9);
List.printDepan();
System.out.println();
List.tambahDepan(6);
System.out.println();
List.printDepan();
List.hapusDepan();
System.out.println();
List.printDepan();
List.hapusBelakang();
System.out.println();
List.printDepan();
List.tambahBelakang(4);
System.out.println();
List.printDepan();
}
}

2. Single Linked List Circular

Class SLLC
public class SLLC {

public int data;


public SLLC next;

public SLLC() {
}

public SLLC(int d) {
data = d;
next = null;
}

SLLC head;
SLLC bantu;

public boolean jikaEmpty() {


if (head == null) {
return true;
} else {
return false;
}
}

public void insertDepan(int databaru) {


SLLC baru;
baru = new SLLC(databaru);
baru.next = baru;

if (jikaEmpty() == true) {
head = baru;
head.next = head;
} else {
if (head.next == head) {
baru.next = head;
head.next = baru;
head = baru;
} else {
bantu = head;
while (bantu.next != head) {
bantu = bantu.next;
}
bantu.next = baru;
baru.next = head;
head = baru;
}
}
}
public void insertBelakang(int databaru) {
//isi kan method untuk tambah belakang
}

public void deleteDepan() {


if (head.next == head) {
head = null;
} else {
bantu = head;
while (bantu.next != head) {
bantu = bantu.next;
}
head = head.next;
bantu.next = head;
}
}

public void deleteBelakang() {


//isikan method untuk hapus belakang
}

public void cetakDepan() {


if (jikaEmpty() == false) {
bantu = head;
do {
bantu.displayNode();
bantu = bantu.next;
} while (bantu != head);
}
}

public void displayNode() {


System.out.print(data + " ");
}

Class SLLCApp
public class SLLCApp {

public static void main(String args[]) {


SLLC List = new SLLC();
List.insertDepan(5);
List.insertDepan(2);
List.insertDepan(7);
List.insertBelakang(9);
List.cetakDepan();
System.out.println();
List.insertDepan(6);
System.out.println();
List.cetakDepan();
List.deleteDepan();
System.out.println();
List.cetakDepan();
List.deleteBelakang();
System.out.println();
List.cetakDepan();
List.insertBelakang(4);
System.out.println();
List.cetakDepan();
}
}

Modul Pertemuan 3 Praktikum Struktur Data dan Algoritma

1. Double Linked List non Circular

Class DLLNC
public class DLLNC {

public int data;


public DLLNC next;
public DLLNC prev;
DLLNC head;
DLLNC bantu;

public DLLNC() {
}

public DLLNC(int d) {
data = d;
next = null;
prev = null;
}

public boolean jikaEmpty() {


if (head == null) {
return true;
} else {
return false;
}
}

public void tambahDpn(int databaru) {


DLLNC baru;
baru = new DLLNC(databaru);
baru.next = null;
baru.prev = null;

if (jikaEmpty() == true) {
head = baru;
head.next = null;
head.prev = null;
} else {
head.prev = baru;
baru.next = head;
head = baru;
}
}

public void tambahBlkg(int databr) {


//isi kan method untuk tambah belakang
}

public void hapusDepan() {


DLLNC hapus;
hapus = head;
if (head.next != null) {
head = head.next;
head.prev = null;
hapus = null;
} else {
head = null;
}
}

public void hapusBelakang() {


//isi kan method untuk hapus belakang
}

public void printDepan() {


if (jikaEmpty() == false) {
bantu = head;
while (bantu != null) {
bantu.displayNode();
bantu = bantu.next;
}
}
}

public void printBlkg() {


DLLNC bantu2;
if (jikaEmpty() == false) {
bantu = head;
while (bantu.next != null) {
bantu = bantu.next;
}
bantu2 = bantu;
while (bantu2 != null) {
bantu2.displayNode();
bantu2 = bantu2.prev;
}
}
}

public void displayNode() {


System.out.print(data + " ");
}
}

Class DLLNCApp
public class DLLNCApp {

public static void main(String args[]) {


DLLNC list = new DLLNC();
list.tambahDpn(6);
list.tambahDpn(3);
list.tambahDpn(9);
list.printDepan();
System.out.println();
list.printBlkg();
list.tambahBlkg(7);
System.out.println();
list.printDepan();
list.hapusDepan();
System.out.println();
list.printDepan();
list.hapusBelakang();
System.out.println();
list.printDepan();
}

2. Double Linked List Circular

Class linkDLLC
public class linkDLLC {

public int data;


public linkDLLC next;
public linkDLLC prev;

public linkDLLC(int d){


data=d;
next=null;
prev=null;
}

public void displayNode(){


System.out.print(data + " ");
}
}

Class DLLC
public class DLLC {

linkDLLC head;
linkDLLC bantu;

public boolean jikaEmpty() {


if (head == null) {
return true;
} else {
return false;
}
}

public void tambahDpn(int databaru) {


linkDLLC baru;
baru = new linkDLLC(databaru);
baru.next = baru;
baru.prev = baru;

if (jikaEmpty() == true) {
head = baru;
head.next = head;
head.prev = head;
} else {
bantu = head.prev;
head.prev = baru;
baru.next = head;
head = baru;
bantu.next = head;
head.prev = bantu;
}
}

public void tambahBlkg(int databr) {


//isi kan method untuk tambah belakang
}

public void hapusDepan() {


linkDLLC hapus;
hapus = head;

if (head.next != head) {
bantu = head.prev;
head = head.next;
bantu.next = head;
head.prev = bantu;
hapus = null;
} else {
head = null;
}
}

public void hapusBelakang() {


//isi kan method untuk hapus belakang
}

public void printDepan() {


if (jikaEmpty() == false) {
bantu = head;
do {
bantu.displayNode();
bantu = bantu.next;
} while (bantu != head);
}
}

public void printBlkg() {


linkDLLC bantu2;
if (jikaEmpty() == false) {
bantu = head;
while (bantu.next != head) {
bantu = bantu.next;
}
bantu2 = bantu;
do{
bantu2.displayNode();
bantu2 = bantu2.prev;
}while (bantu2.prev != bantu);
bantu2.displayNode();
}
}

public static void main(String[] args) {


DLLC List = new DLLC ();
List.tambahDpn(5);
List.tambahDpn(2);
List.tambahDpn(7);
List.tambahBlkg(9);
List.printDepan();
System.out.println();
List.tambahDpn(6);
List.printBlkg();
List.hapusDepan();
System.out.println();
List.printDepan();
List.hapusBelakang();
System.out.println();
List.printDepan();
}
}

Silahkan dicoba/dipraktekkan class-class diatas. Lalu silahkan dianalisa perbaris sesuai, tampilkan
output masing-masing class. Lakukan eksplorasi terhadap koding diatas !!!

Laporan Praktikum dikumpul ke google classroom. Batas pengumpulan hingga hari minggu (29
September 2019) pada jam 12.00 malam.

You might also like