Kelas : Non-Reg
Npm : 193040184
Praktikum Pemograman I
Latihan 1
Code :
class Node {
private int data; // Atribut untuk menyimpan data pada node
private Node next; // Atribut untuk menunjukkan node selanjutnya dalam
struktur list
// Menghubungkan node-node
node1.setNext(node2);
node2.setNext(node3);
Latihan 2
Code :
class Node {
private int data; // Atribut untuk menyimpan data pada node
private Node next; // Atribut untuk menunjukkan node selanjutnya dalam
struktur list
class StrukturList {
private Node head; // Atribut kepala (pertama) dari struktur list
public StrukturList() {
head = null; // Inisialisasi struktur list dengan kepala kosong
}
if (head == null) {
head = newNode; // Jika struktur list kosong, node baru menjadi
kepala
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext(); // Iterasi hingga mencapai node
terakhir
}
current.setNext(newNode); // Menambahkan node baru sebagai node
terakhir
}
}
Latihan 3
Code :
class Node {
private int data;
private Node next;
class StrukturList {
private Node head;
public StrukturList() {
head = null;
}
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
}
strukturList.addTail(10);
strukturList.addTail(20);
strukturList.addTail(30);
Latihan 4
Code :
import java.util.Scanner;
class Node {
private int data;
private Node next;
class StrukturList {
private Node head;
public StrukturList() {
head = null;
}
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
}
System.out.println("Output:");
strukturList.displayElement();
}
}
Hasil :
Latihan 5
Code :
import java.util.Scanner;
class Node {
private int data;
private Node next;
class StrukturList {
private Node head;
public StrukturList() {
head = null;
}
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
}
System.out.println("Output:");
strukturList.displayElement();
}
}
Hasil :