Sie sind auf Seite 1von 4

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package strukdat22;

import java.util.Scanner;

/**
*
* @author Tama's
*/
public class Strukdat22 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Linkedlist list = new Linkedlist();
/*
* Let us create a unsorted linked list to test the functions
* created. The list shall be a: 2->3->20->5->10->15
*/
list.push(15);
list.push(10);
list.push(5);
list.push(20);
list.push(3);
list.push(2);

Scanner sc = new Scanner(System.in);


System.out.println("Masukin Angka Only!!");
System.out.println("Masukan Element Pertama :");
list.push(sc.nextInt());
System.out.println("Masukan Element Kedua :");
list.push(sc.nextInt());
//list.sortedMerge(null, null)

// Apply merge So2rt


list.head = list.mergeSort(list.head);
System.out.print("\n Sorted Linked List is: \n");
list.printList(list.head);
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package strukdat22;
/**
*
* @author Tama's
*/
public class Linkedlist {

node head = null;

// node a, b;

static class node {

int val;
node next;

public node(int val) {


this.val = val;
}
}

node sortedMerge(node a, node b) {


node result;
/* Base cases */
if (a == null) {
return b;
}
if (b == null) {
return a;
}

/* Pick either a or b, and recur */


if (a.val <= b.val) {
result = a;
result.next = sortedMerge(a.next, b);
} else {
result = b;
result.next = sortedMerge(a, b.next);
}
return result;
}

node mergeSort(node h) {
// Base case : if head is null
if (h == null || h.next == null) {
return h;
}

// get the middle of the list


node middle = getMiddle(h);
node nextofmiddle = middle.next;

// set the next of middle node to null


middle.next = null;

// Apply mergeSort on left list


node left = mergeSort(h);

// Apply mergeSort on right list


node right = mergeSort(nextofmiddle);

// Merge the left and right lists


node sortedlist = sortedMerge(left, right);
return sortedlist;
}

// Utility function to get the middle of the linked list


public static node getMiddle(node head) {
if (head == null) {
return head;
}

node slow = head, fast = head;

while (slow.next != null && fast.next.next != null) {


slow = slow.next;
fast = fast.next.next;
}
return slow;
}

void push(int new_data) {


/* allocate node */
node new_node = new node(new_data);

/* link the old list off the new node */


new_node.next = head;

/* move the head to point to the new node */


head = new_node;
}

// Utility function to print the linked list


void printList(node headref) {
while (headref != null) {
System.out.print(headref.val + " ");
headref = headref.next;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package strukdat21;

import java.util.LinkedList;
/**
*
* @author Tama's
*/
public class Strukdat21 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LinkedList<String> a = new LinkedList<>();

a.add("Lala");
a.add("Shani");
a.add("Gracia");
a.add("Kyla");
a.add("Gio");
a.add(0, "Jamal");
a.add(3, "Ageng");

//System.out.println("" + a.isEmpty());
//System.out.println("" + a.size());
System.out.println("" + a);

a.remove(2);
System.out.println("-------------------------");
System.out.println("" + a);

a.remove(4);
System.out.println("-------------------------");
System.out.println("" + a);
/*System.out.println(""+a.getFirst());
System.out.println(""+a.get(1));
System.out.println(""+a.get(2));
System.out.println(""+a.get(3));
System.out.println(""+a.get(4));
System.out.println(""+a.getLast());*/

System.out.println("Index OF A: " + a.indexOf("Lala"));


System.out.println("Index OF A: " + a.indexOf("Gracia"));

Das könnte Ihnen auch gefallen