Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
50 views4 pages

Java Linked List for Student Data

This document is about a program that inputs, sorts, and searches student names using a single linked list data structure. It contains code for a Node class to create nodes, and methods to add nodes, display the list, sort the list alphabetically, and search for a node by name. The main method tests the program by inputting student names, displaying the original and sorted lists, and searching for a name.

Uploaded by

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

Java Linked List for Student Data

This document is about a program that inputs, sorts, and searches student names using a single linked list data structure. It contains code for a Node class to create nodes, and methods to add nodes, display the list, sort the list alphabetically, and search for a node by name. The main method tests the program by inputting student names, displaying the original and sorted lists, and searching for a name.

Uploaded by

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

Tugas 11

Praktikum Struktur Data


Jurusan Teknik Informatika Fakultas Sains dan Teknologi
Nama : Dekis Aldamawan
NIM : 1197050135
Kelas : Praktikum A
Dosen : Popon Dauni, ST., M.Kom.
Program menginputkan, mengurutkan dan mencari data nama mahasiswa menggunakan single
Linked List
Source Code

import java.util.Scanner;
public class TugasLinkedList{
class Node{
String data;
Node next;
public Node(String data){
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void addNode(String data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public void display(){
if(head == null){
System.out.println("List kosong");
return;
}
while(current != null){
System.out.print("\n"+ current.data);
current = current.next;
}
System.out.println();
}
public void sortList(){
Node current = head, index = null;
String temp;
if(head == null){
return;
}
else {
while(current != null){
index = current.next;

while(index != null){
if(current.data .compareTo (index.data)>0) {
temp = current.data;
current.data = index.data;
index.data = temp;
}
index = index.next;
}
current = current.next;
}
}
}
public void searchNode(String data){
Node current = head;
int x = 1;
boolean flag = false;
if(head == null) {
System.out.println("List kosong");
}
else {
while(current != null){
if(current.data.equals(data)){
flag = true;
break;
}
x++;
current = current.next;
}
}
if(flag)
System.out.println("Nama di list saat ini berada di posisi : " + x);
else
System.out.println("Nama tidak ada di list");
}
public static void main(String[] args){
String n,search;
int i,j,k=1;
TugasLinkedList mList = new TugasLinkedList();
Scanner in = new Scanner(System.in);
System.out.print("\nMasukkan jumlah Mahasiswa : ");
j = in.nextInt();
System.out.println();
System.out.println("Masukkan nama mahasiswa ");
for(i = 0; i < j; i++){
System.out.print("Mahasiswa ke " +k+ ":" );
n=in.next();
mList.addNode(n);
k++;
}
System.out.println("");
System.out.print("Data nama mahasiswa dalam list");
mList.display();
System.out.println("");
System.out.print("Data nama mahasiswa setelah diurutkan secara berabjad");
mList.sortList();
mList.display();
System.out.println("");
System.out.print("Mencari nama mahasiswa berdasarkan data yang telah diurutkan");
System.out.print("\nMasukkan nama mahasiswa yang ingin dicari : ");
search=in.next();
mList.searchNode(search);

}
}

Screenshot

You might also like