/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package uts;
import java.util.Scanner;
/**
* @author user
*/
public class UTS {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int jumlah,i,j, swap;
Scanner scan = new Scanner(System.in);
System.out.print("Masukkan Jumlah Data Yang Mau DiInputkan :");
jumlah = scan.nextInt();
int array[] = new int[jumlah];
System.out.println("\nMasukkan " + jumlah+" Buah Bilangan Integer");
System.out.println("===========================================");
for(i=0; i<jumlah;i++)
System.out.print("Bilangan Ke- " + (i+1)+" =");
array[i]=scan.nextInt();
}
System.out.println("\n Data = ");
for(int a=0;a<jumlah;a++)
System.out.print(array[a] +" ");
for(i=0; i<(jumlah-1);i++)
for(j=0;j<jumlah-i-1;j++)
if (array[j] > array[j+1])
swap = array[j];
array[j] = array[j+1];
array[j+1]=swap;
}
System.out.println("\n\literasi ke :");
for(i=0;i<jumlah; i++)
System.out.print(array[i] +" ");
}
}
OUTPUT PROGRAM NYA :
2.
public class soalUTS2 {
public static void main (String[] args){
UTS2 arrstack = new UTS2;
arrstack.push(dt:10);
arrstack.push(dt:10);
arrstack.tampil();
arrstack.push(40);
arrstack.tampil();
arrstack.pop();
arrstack.tampil();
arrstack.push(dt:50);
arrstack.push(dt:70);
arrstack.push(dt:60);
arrstack.push(dt:20);
arrstack.tampil();
arrstack.elemenTop();
arrstack.elemenBottom();
arrstack.inisialisasi();
arrstack.tampil();
arrstack.elemenTop();
arrstack.elemenBottom();
private boolean empty,full;
private int pos;//menujukkan tempat kosong
private int max_data = 100;
private int item [] = new int[max_data];
public Stack(){
full = false;
empty = true;
pos = 0;
}//end of constructor
//method isFull
public boolean isFull(){
return(full);
}//end of isFull method
//method isEmpty
public boolean isEmpty(){
return(empty);
}//end of isEmptyl method
//method push
public void push(int data){
if(!isFull()){
item[pos++] = data;
empty = false;
if(pos == max_data) full = true;
System.out.println(“Data sudah ditambahkan”);
}
else{
System.out.println(“Stack is Empty”);
}
return;
}//end of push method
//method pop
public int pop(){
int x = 0;
if(!isEmpty()){
x = item[--pos];
full = false;
System.out.println(“elemen paling atas :” + item[pos]);
System.out.println(“”);
item[pos]=0;
if(pos==0)empty = true;
else{
System.out.println(“Stack is Empty!”);
}
}else{
System.out.println(“Stack is Empty!\n”);
}
return(x);
}//end of pop method
//method Display
public void Display(){
System.out.println(“Isi Stack : “);
//printing list item
for(int i=0; i<pos; i++){
System.out.print(item[i]+" ");
}
System.out.println("\n");
}//end of Display
}
3.
public class UTS3 {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
// check if the queue is full
boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
// check if the queue is empty
boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
// insert elements to the queue
void enQueue(int element) {
// if queue is full
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}
rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}
// delete element from the queue
int deQueue() {
int element;
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
// remove element from the front of queue
element = items[front];
// if the queue has only one element
if (front >= rear) {
front = -1;
rear = -1;
}
else {
// mark next element as the front
front++;
}
System.out.println( element + " Deleted");
return (element);
}
}
// display element of the queue
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
}
else {
// display the front of the queue
System.out.println("\nFront index-> " + front);
// display element of the queue
System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
// display the rear of the queue
System.out.println("\nRear index-> " + rear);
}
}
public static void main(String[] args) {
// create an object of Queue class
Queue q = new Queue();
// try to delete element from the queue
// currently queue is empty
// so deletion is not possible
q.deQueue();
// insert elements to the queue
for(int i = 1; i < 6; i ++) {
q.enQueue(i);
}
// 6th element can't be added to queue because queue is full
q.enQueue(6);
q.display();
// deQueue removes element entered first i.e. 1
q.deQueue();
// Now we have just 4 elements
q.display();
}
}