Skip to content

Commit

Permalink
improv bab 6 - stackqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
poncoe committed Mar 9, 2019
1 parent 7c729b7 commit d94ca5c
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 0 deletions.
64 changes: 64 additions & 0 deletions bab6_stackqueue/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include "stack.h"
#include "queue.h"
#include <stdlib.h>
using namespace std;

int main()
{
Stack S;
cout<<"PERHATIKAN "<<endl;
cout<<"cara mendeklarasikan address pada stack : addressS P"<<endl;
cout<<"cara mendeklarasikan address pada queue : addressQ P"<<endl;
cout<<"cara mendeklarasikan isi elemen Stack : info(P)"<<endl;
cout<<"cara mendeklarasikan isi elemen waktu pada queue : waktu(P)"<<endl;
cout<<"cara mendeklarasikan isi elemen ID pada queue : ID(P)"<<endl;
cout<<"\t[press enter to continue]"<<endl<<endl;
cin.get();
system("CLS");
cout<<"SOAL 1 : isi prosedur dalam stack.cpp"<<endl;
cout<<"contoh output"<<endl;
cout<<"Masukkan Operasi Matematika : ( a * ( b - c ) + ( ( j + k ) / ( p + q * r ) ) ^ y ). "<<endl;
cout<<"SEIMBANG"<<endl<<endl;

cout<<"Masukkan Operasi Matematika : (a + b) * c + (a - b "<<endl;
cout<<"TIDAK SEIMBANG"<<endl<<endl;

cout<<"Program anda "<<endl<<endl;
soal1();
cin.get();
cout<<endl<<endl<<"\t[press enter to continue]"<<endl;
cin.get();
system("CLS");


cout<<"SOAL 2 : isi prosedur dalam stack.cpp"<<endl;
cout<<"buatlah 3 Stack baru dengan createS lalu inputkan beberapa karakter ke dalam S1"<<endl;
cout<<"lalu S1 tersebut dimasukkan ke dalam S2 dan S3 serta tampilkan isi dari ketiga Stack tersebut"<<endl;
cout<<"perhatikan ilustrasi dalam soal sbg contoh"<<endl<<endl;
cout<<"Program anda "<<endl<<endl;

soal2();
cin.get();
cout<<"\t[press enter to continue]"<<endl;
cin.get();
system("CLS");

cout<<"SOAL 3 : isi prosedur dalam queue.cpp"<<endl;
cout<<"inputkan 5 data ke dalam Queue"<<endl;
cout<<"program anda harus menampilkan seluruh data yg telah di input"<<endl;
cout<<"lakukan proses dequeu enqueu di setiap literasi dan tampilkan setiap perubahannya sampai Queuenya kosong"<<endl;

cout<<"Program anda"<<endl<<endl;
soal3();
cin.get();
cout<<"\t[press enter to continue]"<<endl;
cin.get();
system("CLS");

cout<<endl;

return 0;


}
102 changes: 102 additions & 0 deletions bab6_stackqueue/queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include<iostream>
#include"queue.h"

bool isEmptyQ(Queue Q){

if(head(Q) == NULL && tail(Q)==NULL){
return true;
}else{
return false;
}

}

void createQ(Queue &Q){
head(Q) = NULL;
tail(Q) = NULL;
}

void createNewElmtQ(string ID,int waktu,addressQ &P){
P = new elmQ;
ID(P) = ID;
waktu(P) = waktu;
next(P) = NULL;
}

void enqueue(Queue &Q,addressQ P)
{
if(isEmptyQ(Q))
{
head(Q) = P;
tail(Q) = P;
}
else
{
next(tail(Q)) = P;
tail(Q) = P;
}
}

void dequeue(Queue &Q,addressQ &P)
{


if(!isEmptyQ(Q))
{
P = head(Q);
if(head(Q) == tail(Q))
{
head(Q) = NULL;
tail(Q) = NULL;
}
else
{
head(Q) = next(head(Q));
next(P) = NULL;
}

}
else{
P = NULL;
}
}

void soal3()
{
Queue Q; addressQ W;

createQ(Q);
createNewElmtQ("T25", 13, W);
enqueue(Q, W);
createNewElmtQ("T1", 4, W);
enqueue(Q, W);
createNewElmtQ("T11", 5, W);
enqueue(Q, W);
createNewElmtQ("T21", 21, W);
enqueue(Q, W);
createNewElmtQ("T8", 3, W);
enqueue(Q, W);
addressQ R;
while(!isEmptyQ(Q)){
dequeue(Q, W);
if (waktu(W)>5){
waktu(W) = waktu(W)-5;
enqueue(Q, W);
}
R = head(Q);
if (!isEmptyQ(Q)){
cout<<"Antrian saat ini :";
while (R!=NULL){
cout<<" ID : "<<ID(R)<<" waktu: "<<waktu(R)<<" ";
R = next(R);
}
cout<<endl;
} else{
cout<<"Antrian sudah kosong"<<endl;
}
}

}



35 changes: 35 additions & 0 deletions bab6_stackqueue/queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
#include <iostream>
#define Nil NULL
#define ID(P) P->ID
#define waktu(P) P->waktu
#define next(P) P->next
#define head(Q) Q.head
#define tail(Q) Q.tail

using namespace std;

typedef struct elmQ * addressQ;

struct elmQ{
string ID;
int waktu;
addressQ next;
};

struct Queue{
addressQ head;
addressQ tail;
};
bool isEmptyQ(Queue Q);
void createQ(Queue &Q);
void createNewElmtQ(string ID,int waktu,addressQ &P);
void enqueue(Queue &Q,addressQ P);
void dequeue(Queue &Q,addressQ &P);
void soal3();




#endif // QUEUE_H_INCLUDED
122 changes: 122 additions & 0 deletions bab6_stackqueue/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <stdio.h>
#include "stack.h"


bool isEmptyS(Stack S){


if (top(S) == Nil) {
return true;
}
else{
return false;
}

}

void createS(Stack &S){
top(S) = NULL;
}

void createNewElmtS(infotype X, addressS &P){
P = new elmS;
info(P) = X;
next(P) = Nil;
}

void push(Stack &S, addressS P){

if(isEmptyS(S))
{
top(S) = P;
}
else{
next(P) = top(S);
top(S) = P;
}
}

void pop(Stack &S, addressS &P){

if (!isEmptyS(S)){
P = top(S);
top(S) = next(top(S));
next(P) = Nil;
} else {
P = NULL;
}
}

void soal1()
{
Stack S; addressS Q;

createS(S); infotype input;
int jumB = 0;
int jumT = 0;
cin>>input;
while(input!='.'){
createNewElmtS(input, Q);
push(S, Q);
cin>>input;
}
while(!isEmptyS(S)){
pop(S, Q);
if (info(Q)=='('){
jumB++;
}else if (info(Q)==')'){
jumT++;
}
}
if (jumB == 0 && jumT ==0){
cout<<"Tidak ada tanda kurung";
}else if (jumB==jumT){
cout<<"SEIMBANG";
}else{
cout<<"TIDAK SEIMBANG";
}
//
}

void soal2()
{

Stack S1,S2,S3;

infotype input;
addressS Q;
createS(S1);
createS(S2);
createS(S3);
cin>>input;
while(input!='.'){
if ( (input>= 'A' && input<='Z') ||(input>= 'a' && input<='z') ){
createNewElmtS(input, Q);
push(S1, Q);
}
cin>>input;
}
while(!isEmptyS(S1)){
pop(S1, Q);
if (info(Q)=='a' || info(Q)=='i' || info(Q)=='u' || info(Q)=='e' || info(Q)=='o' || info(Q)=='A' || info(Q)=='I' || info(Q)=='U' || info(Q)=='E' || info(Q)=='O'){
push(S2, Q);
}else {
push(S3, Q);
}
}
if (isEmptyS(S1)){
cout<<"Stack S1 sudah kosong.";
}
cout<<endl<<"Stack S2 : ";
while (!isEmptyS(S2)){
pop(S2, Q);
cout<<info(Q);
}
cout<<endl<<"Stack S3 : ";
while (!isEmptyS(S3)){
pop(S3, Q);
cout<<info(Q);
}
}


33 changes: 33 additions & 0 deletions bab6_stackqueue/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED

#include<iostream>
#define Nil NULL
#define info(P) (P)->info
#define next(P) (P)->next
#define top(S) ((S).top)

using namespace std;

typedef char infotype;
typedef struct elmS *addressS;

struct elmS{
infotype info;
addressS next;
};

struct Stack{
addressS top;
};

bool isEmptyS(Stack S);
void createS(Stack &S);
void createNewElmtS(infotype X, addressS &P);
void push(Stack &S, addressS P);
void pop(Stack &S, addressS &P);
void soal1();
void soal2();


#endif // STACK_H_INCLUDED

0 comments on commit d94ca5c

Please sign in to comment.