Stack dapat di artikan sebagai kumpulan data yang seolah olah itu ada data yang di taruh di atas data sebelumnya (di tumpuk),di dalam stack kita bisa mangambil dan menambahkan.stack punya sifat LIFO(last in, first out).
Stack dapat dibuat dengan menggunakan array maupun linked list.Bentuk penyajian bisa berupa array tapi kurang tepat karena banyak elemen array bersifat statis. Sedangkan dalam stack banyak element yang bervariasi dan dinamis. Pada suatu saat nanti ukuran stack sama dengan ukuran array,bial di teruskan dlm menambah data akan OVERLOW. Ada 2 macam untuk mencatat ujung stack (SINGLE DAN DOUBLE).
Operasi pada Stack:
- Deklarasi
- Inisialisasi
- Cek kosong
- Cek penuh
- Penambahan
- Pengambilan
- Pengaksesan
Soal:
- Masukan ke dalam tumpukan yaitu 30,10,50,40,20.
- Buat metode untuk mengakses seluruh data yang ada di tumpukan, dari paling atas sampai paling bawah tanpa menghapus atasnya.
- Buat 3 tumpukan TPA,TPB,TPC lalu masukan data ke tumpukan TPA dengan data kaya no 1.
- Pindah isi tumpukan TPA ke TPB kemudia tampilkan hasilnya.
- Dengan tumpukan TPA,TPB,TPC urutkanlah data yang di masukan di no 1 agar urut dengan operasi push dan pop pada data yang sudah urut di taruh di tumpukan TPC.
Jawaban :
1.
package stack;
import java.util.EmptyStackException;
/*
*
* @author Beauty Clouds
*/
public class Stack {
static int tumpukan[];
static int array_size;
static int top;
static void inisialisasi(int arrSize) {
array_size = arrSize;
tumpukan = new int[array_size];
top = -1;
}
static boolean isEmpty() {
return (top == -1);
}
static boolean isFull() {
return (top == array_size - 1);
}
static int popStack() {
if (isEmpty()) {
throw new EmptyStackException();
}
return tumpukan[top--];
}
static void pushStack(int data) {
if (isFull()) {
System.out.println("Stack penuh");
tumpukan = resizing(tumpukan);
}
tumpukan[++top] = data;
}
static int[] resizing(int[] element) {
int[] newArr = new int[2 * element.length];
array_size = newArr.length;
System.arraycopy(element, 0, newArr, 0, array_size);
return newArr;
}
static void peekAllStack() {
int baru = top;
for (int i = baru; i >= 0; i--) {
System.out.println(tumpukan[i]);
}
}
public static void main(String[] args) {
inisialisasi(5);
pushStack(30);
pushStack(10);
pushStack(50);
pushStack(40);
pushStack(20);
peekAllStack();
}
}
2.
package javaapplication82;
import java.util.EmptyStackException;
import java.util.Stack;
/**
*
* @author Beauty Clouds
*/
public class JavaApplication82 {
static int tumpukan[];
static int array_size;
static int top;
static void inisialisasi(int arrSize){
array_size=arrSize;
tumpukan=new int[array_size];
top =-1;
}
static boolean IsEmpty(){
return (top==-1);
}
static boolean isFull(){
return (top==array_size-1);
}
static int popStack(){
if (IsEmpty())
throw new EmptyStackException();
return tumpukan[top--];
}
static int []resizing(int[]element){
int[] newArr=new int[2*element.length];
array_size=newArr.length;
System.arraycopy(element, 0, newArr, 0, array_size);
return newArr;
}
static void pushStack(int data){
if (isFull()) {
System.out.println("Stack Penuh");
tumpukan=resizing(tumpukan);
}
tumpukan[++top]=data;
}
static void peekAllStack(){
int baru=top;
for (int i = baru; i >=0; i--) {
System.out.println(tumpukan[i]);
}
}
public static void main(String[] args) {
Stack s = new Stack();
s.push(30);
s.push(10);
s.push(50);
s.push(40);
s.push(20);
System.out.println("isi stack:" + s);
}
}
3.
package javaapplication80;
import java.util.EmptyStackException;
/**
*
* @author Beauty Clouds
*/
public class JavaApplication80 {
static int TPA[],TPB[],TPC[];
static int array_size_TPA,array_size_TPB,array_size_TPC;
static int top_TPA,top_TPB,top_TPC;
static void inisialisasi(int arrSize){
array_size_TPA=array_size_TPB=array_size_TPC=arrSize;
TPA = new int[array_size_TPA];
TPB = new int[array_size_TPB];
TPC = new int[array_size_TPC];
top_TPA=top_TPB=top_TPC=-1;
}
static boolean IsEmpty_TPA(){
return (top_TPA==-1);
}
static boolean IsEmpty_TPB(){
return (top_TPB==-1);
}
static boolean IsEmpty_TPC(){
return (top_TPC==-1);
}
static boolean isFull_TPA(){
return (top_TPA==array_size_TPA-1);
}
static boolean isFull_TPB(){
return (top_TPB==array_size_TPB-1);
}
static boolean isFull_TPC(){
return (top_TPC==array_size_TPC-1);
}
static int popStack_TPA(){
if(IsEmpty_TPA()){
throw new EmptyStackException();
}
return TPA[top_TPA--];
}
static int popStack_TPB(){
if(IsEmpty_TPB()){
throw new EmptyStackException();
}
return TPB[top_TPB--];
}
static int popStack_TPC(){
if(IsEmpty_TPC()){
throw new EmptyStackException();
}
return TPC[top_TPC--];
}
static int[]resizing_TPA(int[]element){
int[] newArr=new int[2*element.length];
array_size_TPA=newArr.length;
System.arraycopy(element, 0, newArr, 0, array_size_TPA);
return newArr;
}
static int[]resizing_TPB(int[]element){
int[] newArr=new int[2*element.length];
array_size_TPB=newArr.length;
System.arraycopy(element,0,newArr,0,array_size_TPB );
return newArr;
}
static int[]resizing_TPC(int[]element){
int[] newArr=new int[2*element.length];
array_size_TPC=newArr.length;
System.arraycopy(element,0,newArr,0,array_size_TPC);
return newArr;
}
static void pushStack_TPA(int data){
if(isFull_TPA()){
System.out.println("stack penuh");
TPA=resizing_TPA(TPA);
}
TPA[++top_TPA]=data;
}
static void pushStack_TPB(int data){
if(isFull_TPB()){
System.out.println("stack penuh");
TPB=resizing_TPB(TPB);
}
TPB[++top_TPB]=data;
}
static void pushStack_TPC(int data){
if(isFull_TPC()){
System.out.println("stack penuh");
TPA=resizing_TPB(TPC);
}
TPC[++top_TPC]=data;
}
static void peekAllStack_TPA(){
int baru = top_TPA;
for(int i=baru;i>=0;i--){
System.out.println(TPA[i]);
}
}
static void peekAllStack_TPB(){
int baru = top_TPA;
for(int i=baru;i>=0;i--){
System.out.println(TPB[i]);
}
}
static void peekAllStack_TPC(){
int baru = top_TPC;
for(int i=baru;i>=0;i--){
System.out.println(TPC[i]);
}
}
public static void main(String[] args) {
inisialisasi(5);
pushStack_TPA(30);
pushStack_TPA(10);
pushStack_TPA(50);
pushStack_TPA(40);
pushStack_TPA(20);
peekAllStack_TPA();
}
}
4.
package javaapplication81;
import java.util.EmptyStackException;
/**
*
* @author Beauty Clouds
*/
public class JavaApplication81 {
static int TPA[],TPB[],TPC[];
static int array_size_TPA,array_size_TPB,array_size_TPC;
static int top_TPA,top_TPB,top_TPC;
static void inisialisasi(int arrSize){
array_size_TPA=array_size_TPB=array_size_TPC=arrSize;
TPA = new int[array_size_TPA];
TPB = new int[array_size_TPB];
TPC = new int[array_size_TPC];
top_TPA=top_TPB=top_TPC=-1;
}
static boolean IsEmpty_TPA(){
return (top_TPA==-1);
}
static boolean IsEmpty_TPB(){
return (top_TPB==-1);
}
static boolean IsEmpty_TPC(){
return (top_TPC==-1);
}
static boolean isFull_TPA(){
return (top_TPA==array_size_TPA-1);
}
static boolean isFull_TPB(){
return (top_TPB==array_size_TPB-1);
}
static boolean isFull_TPC(){
return (top_TPC==array_size_TPC-1);
}
static int popStack_TPA(){
if(IsEmpty_TPA()){
throw new EmptyStackException();
}
return TPA[top_TPA--];
}
static int popStack_TPB(){
if(IsEmpty_TPB()){
throw new EmptyStackException();
}
return TPB[top_TPB--];
}
static int popStack_TPC(){
if(IsEmpty_TPC()){
throw new EmptyStackException();
}
return TPC[top_TPC--];
}
static int[]resizing_TPA(int[]element){
int[] newArr=new int[2*element.length];
array_size_TPA=newArr.length;
System.arraycopy(TPA,0,TPB,0,array_size_TPA);
return newArr;
}
static int[]resizing_TPB(int[]element){
int[] newArr=new int[2*element.length];
array_size_TPB=newArr.length;
System.arraycopy(element,0,newArr,0,array_size_TPB );
return newArr;
}
static int[]resizing_TPC(int[]element){
int[] newArr=new int[2*element.length];
array_size_TPC=newArr.length;
System.arraycopy(element,0,newArr,0,array_size_TPC);
return newArr;
}
static void pushStack_TPA(int data){
if(isFull_TPA()){
System.out.println("stack penuh");
TPA=resizing_TPA(TPA);
}
TPA[++top_TPA]=data;
}
static void pushStack_TPB(int data){
if(isFull_TPB()){
System.out.println("stack penuh");
TPB=resizing_TPB(TPB);
}
TPB[++top_TPB]=data;
}
static void pushStack_TPC(int data){
if(isFull_TPC()){
System.out.println("stack penuh");
TPA=resizing_TPB(TPC);
}
TPC[++top_TPC]=data;
}
static void peekAllStack_TPA(){
int baru = top_TPA;
for(int i=baru;i>=0;i--){
System.out.println(TPA[i]);
}
}
static void peekAllStack_TPB(){
int baru = top_TPA;
for(int i=baru;i>=0;i--){
System.out.println(TPB[i]);
}
}
static void peekAllStack_TPC(){
int baru = top_TPC;
for(int i=baru;i>=0;i--){
System.out.println(TPC[i]);
}
}
static void copyStack(int []asal,int []tujuan){
for (int r=top_TPA;r>=0;r--){
pushStack_TPB(asal[r]);
}
}
public static void main(String[] args) {
inisialisasi(5);
pushStack_TPA(30);
pushStack_TPA(10);
pushStack_TPA(50);
pushStack_TPA(40);
pushStack_TPA(20);
peekAllStack_TPA();
System.out.println("stack A");
peekAllStack_TPA();
System.out.println("stack B");
copyStack(TPA,TPB);
peekAllStack_TPB();
}
}
0 comments:
Post a Comment