Thursday, 23 August 2018

Contoh Script Java Stack dan Queue

#include <iostream.h> 
#include <stdio.h>
#include <stdio.h>
#define MAX 10 //ukuran maksimum dari stack

void push(int stack[], int *top, int value);
void pop(int stack[], int *top, int *value);

void main()
{
                int stack[MAX]; //jumlah maksimal bilangan yg akan di stack
                int top =-1;
                int n, value;

                do
                {
                                do
                                {
                                                cout<<"Masukkan Nilai yang akan di Push : ";//input bilangan yg di push
                                                cin>>value;
                                                push(stack,&top, value);

                                                cout<<"tekan 1 untuk melajutkan"<<endl; //untuk melanjutkan proses
                                                cin>>n;
                                }
                                while (n==1);

                                cout<<"tekan 1 untuk Melakukan Pop"<<endl;
                                cin>>n;

                                while (n==1)
                                {
                                                pop(stack,&top,&value);
                                                cout<<"Nilai yang di Pop : "<<value;
                                                cout<<"tekan 1 untuk Melakukan pop sebuah elemen"<<endl;
                                                cin>>n;
                                }
                                                cout<<endl;
                                                cout<<"tekan 1 untuk melanjutkan"<<endl;
                                                cin>>n;
                                }
                                while (n==1);
                                }
                void push(int stack[], int *top, int value) //FUNGSI untuk insert nilai
                {
                                if(*top < MAX)
                                {
                                                *top = *top +1;
                                                stack[*top] = value;
                                }
                                else
                                {
                                                cout<<"Stack penuh, push nilai tidak dapat dilakukan"<<endl;
                                                exit(0);
                                }
                }
                 void pop(int stack[], int *top, int *value) //FUNGSI untuk delete nilai
                {
                                if(*top >=0)
                                {
                                                *value = stack[*top];
                                                *top = *top - 1;
                                }
                                else
                                {
                                                cout<<"Stack kosong, pop tidak dapat dilakukan"<<endl;
                                                exit(0);
                                }

                }

Contoh Hasil Stack



#include <iostream.h>
#include <stdlib.h>

#define MAX 10 //ukuran maksimum dari queue

void insert(int queue[], int *rear, int nilai);
void del(int queue[], int *front,int rear, int *nilai);

void main()
{
                int queue[MAX];
                int frount = rear;
                int n, nilai;

frount  = rear =(- 1);
                do
                {
                                do
                                {
                                                cout<<"Masukkan Nilai Elemen : ";
                                                cin>>nilai;
                                                push(queue,&rear, nilai);

                                                cout<<endl;
                                                cout<<"tekan 1 untuk melajutkan: ";
                                                cin>>n;
                                }
                                while (n==1);
                               
                                cout<<endl;
                                cout<<"tekan 1 untuk Menghapus sebuah elemen: ";
                                cin>>n;

                                while (n==1)
                                {
                                                del(queue,&front,&nilai);
                                                cout<<"Nilai yang telah dihapus: "<<nilai<<endl;
                                                cout<<endl;
                                                cout<<"tekan 1 untuk Menghapus sebuah elemen:";
                                                cin>>n;
                                }
                                                cout<<endl;
                                                cout<<"tekan 1 untuk melanjutkan:";
                                                cin>>n;
                                }
                                while (n==1);
                                }
                void insert(int queue[], int *rear, int nilai)
                {
                                if(*rear < MAX-1)
                                {
                                                *rear = *rear +1;
                                                queue[*rear] = nilai;
                                }
                                else
                                {
                                                cout<<"Queue penuh, insert nilai tidak dapat dilakukan"<<endl;
                                                exit(0);
                                }
                }
                 void del(int queue[], int *front,int rear, int *nilai)
                {
                                if(*front == rear)
                                {
                                                cout<<"queue kosong, delete tidak dapat dilakukan"<<endl;
                                                exit(0);
                                front = *front +1;
                                *nilai = queue[*front];
                }


Contoh Hasil Queue


Download Scrip Disini

No comments:

Post a Comment