- Home »
- Pemrograman »
- Belajar C++ Stack and Queue
Nama : Ahmad Khairun A.
NIM : E1E115054
STACK
Stack disebut juga tumpukan dimana data hanya dapat dimasukkan dan diambil dari satusisi. Karena itu, stack bersifat LIFO (Last In First Out). Artinya data yang terakhir masuk akan keluar lebih dulu.
QUEUE
Queue disebut juga antrian dimana data akan masuk dari satu sisi dan keluar di sisi yang lain.Karena itu, queue bersifat FIFO (First In First Out). Artinya data yang pertama kali masuk akan keluar lebih dulu.
Membuat program QUEUE dengan ketentuan:
a. Memiliki fungsi PUSH/ENQUEUE (input data)
b. Memiliki fungsi POP/DEQUEUE (ambil satu data)
c. Memiliki fungsi CLEAR (delete all data)
d. Memiliki fungsi PRINT (cetak data pada layar)
e. (opsional) jika user ingin menampilkan data yang diinputkan, maka data tersebut
telah terurut secara ascending
Kodingan :
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
using namespace std;
struct antri_data
{
int data[MAX];
int kepala;
int ekor;
};
antri_data antrian;
void create()
{
antrian.kepala=antrian.ekor=-1;
}
int isEmpty()
{
if(antrian.ekor==-1)
{
return 1;
}
else
{
return 0;
}
}
int isFull()
{
if(antrian.ekor==MAX-1)
{
return 1;
}
else
{
return 0;
}
}
void masuk(int d)
{
antrian.kepala=0;
antrian.ekor++;
antrian.data[antrian.ekor]=d;
cout<<"data berhasil dimasukan ";
}
void keluar()
{
int e=antrian.data[antrian.kepala];
for (int i=antrian.kepala;i<=antrian.ekor-1;i++){
antrian.data[i]=antrian.data[i+1];
}
antrian.ekor--;
cout<<"data terambil "<<e<<" ";
}
void hapus(){
antrian.kepala=antrian.ekor=-1;
cout<<"Data terhapus ";
}
void lihat(){
for(int i=antrian.kepala;i<=antrian.ekor;i++)
{
cout<<antrian.data[i]<<" ";
}
}
int ascending()
{
}
void printout()
{
int tampung;
for(int i=antrian.kepala;i<=antrian.ekor;i++)
{
for(int j=antrian.ekor;j>=i;j--)
if(antrian.data[j]<antrian.data[j-1])
{
tampung=antrian.data[j];
antrian.data[j]=antrian.data[j-1];
antrian.data[j-1]=tampung;
}
}
for(int i=antrian.ekor;i>=antrian.kepala;i--)
{
cout<<antrian.data[i]<<" ";
}
}
int main()
{
int a;
int d;
create();
do
{
cout<<"\nMenu\n1.enque\n2.deque\n3.clear\n4.print\n5.Ascending \n6.terminate\n\nMasukkan Pilihan Anda = ";
cin>>a;
fflush(stdin);
switch(a)
{
case 1:
cout<<"\n\n1.enque \n";
if(isFull()==1)
{
cout<<"queue Penuh ! "<<endl;
}
else
{
cout<<" Masukkan Data = ";cin>>d;
}
fflush(stdin);
masuk(d);
break;
case 2:
cout<<"\n\n2.dequeue \n";
if(isEmpty()==1)
{
cout<<"queue Kosong ! ";
}
else
{
keluar();
}
break;
case 3:
cout<<"\n\n3.Clear \n";
hapus();
cout<<endl<<"queue Kosong"<<endl;
break;
case 4:
cout<<"\n\n4.Print data \n";
if(isEmpty()==1)
{
cout<<"queue Kosong"<<endl;
}
else
{
cout<<"Data yang dicetak : ";
}
lihat();
break;
case 5:
cout<<"Descending data : ";
printout();
break;
case 6:
cout<<"\ntekan enter untuk mengakhiri";
}
getch();
system("cls");
}while (a!=6);
getch();
return 0;
}
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
using namespace std;
struct antri_data
{
int data[MAX];
int kepala;
int ekor;
};
antri_data antrian;
void create()
{
antrian.kepala=antrian.ekor=-1;
}
int isEmpty()
{
if(antrian.ekor==-1)
{
return 1;
}
else
{
return 0;
}
}
int isFull()
{
if(antrian.ekor==MAX-1)
{
return 1;
}
else
{
return 0;
}
}
void masuk(int d)
{
antrian.kepala=0;
antrian.ekor++;
antrian.data[antrian.ekor]=d;
cout<<"data berhasil dimasukan ";
}
void keluar()
{
int e=antrian.data[antrian.kepala];
for (int i=antrian.kepala;i<=antrian.ekor-1;i++){
antrian.data[i]=antrian.data[i+1];
}
antrian.ekor--;
cout<<"data terambil "<<e<<" ";
}
void hapus(){
antrian.kepala=antrian.ekor=-1;
cout<<"Data terhapus ";
}
void lihat(){
for(int i=antrian.kepala;i<=antrian.ekor;i++)
{
cout<<antrian.data[i]<<" ";
}
}
int ascending()
{
}
void printout()
{
int tampung;
for(int i=antrian.kepala;i<=antrian.ekor;i++)
{
for(int j=antrian.ekor;j>=i;j--)
if(antrian.data[j]<antrian.data[j-1])
{
tampung=antrian.data[j];
antrian.data[j]=antrian.data[j-1];
antrian.data[j-1]=tampung;
}
}
for(int i=antrian.ekor;i>=antrian.kepala;i--)
{
cout<<antrian.data[i]<<" ";
}
}
int main()
{
int a;
int d;
create();
do
{
cout<<"\nMenu\n1.enque\n2.deque\n3.clear\n4.print\n5.Ascending \n6.terminate\n\nMasukkan Pilihan Anda = ";
cin>>a;
fflush(stdin);
switch(a)
{
case 1:
cout<<"\n\n1.enque \n";
if(isFull()==1)
{
cout<<"queue Penuh ! "<<endl;
}
else
{
cout<<" Masukkan Data = ";cin>>d;
}
fflush(stdin);
masuk(d);
break;
case 2:
cout<<"\n\n2.dequeue \n";
if(isEmpty()==1)
{
cout<<"queue Kosong ! ";
}
else
{
keluar();
}
break;
case 3:
cout<<"\n\n3.Clear \n";
hapus();
cout<<endl<<"queue Kosong"<<endl;
break;
case 4:
cout<<"\n\n4.Print data \n";
if(isEmpty()==1)
{
cout<<"queue Kosong"<<endl;
}
else
{
cout<<"Data yang dicetak : ";
}
lihat();
break;
case 5:
cout<<"Descending data : ";
printout();
break;
case 6:
cout<<"\ntekan enter untuk mengakhiri";
}
getch();
system("cls");
}while (a!=6);
getch();
return 0;
}
Hasil
3. Infix to Postfix
Membuat program untuk mengkonversi inputan infix menjadi postfix,
program tersebut akan menghasilkan output berupa hasil konversi ke postfix dan hasil
perhitungannya.
Kodingan :
#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;
int opr(char p) {
int temp;
if (p == '^')
temp = 1;
else if (p == '*' || p == '/')
temp = 2;
else if (p == '+' || p == '-')
temp = 3;
return temp;
}
int hasil_akhir;
int eksekusi(int op1, int op2, string op3)
{
stringstream con;
char op4;
con << op3;
con >> op4;
int ans;
switch(op4){
case '+':
ans = op2 + op1;
break;
case '-':
ans = op2 - op1;
break;
case '*':
ans = op2 * op1;
break;
case '/':
ans = op2 / op1;
break;
}
return ans;
}
bool isOpr(string opr)
{
if((opr=="*") || (opr=="/")||(opr=="%")||(opr=="+") || (opr=="-")||(opr=="^"))
return true;
else
return false;
}
int main() {
string infix, titip;
cout << "Masukan Infix : ";
getline(cin, infix);
int hasil,bil[infix.length()], op_tambah=0;
string op[infix.length()];
bool ada=false;
stack<char> opr_stack;
stack<char> opr_hasil;
stringstream postfix;
stringstream cek[infix.length()];
int cek_tambah=0;
for (int i = 0; i < infix.length(); i++)
{
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^')
{
ada = true;
while (!opr_stack.empty() && opr(opr_stack.top()) <= opr(infix[i]))
{
postfix << opr_stack.top();
opr_stack.pop();
}
opr_stack.push(infix[i]);
}
else if (infix[i] == '(') {
opr_stack.push(infix[i]);
}
else if (infix[i] == ')')
{
while (opr_stack.top() != '(')
{
postfix << opr_stack.top();
opr_stack.pop();
}
opr_stack.pop();
}
else
{
postfix << infix[i];
}
if(ada==true)
{
op[op_tambah]=infix[i];
op_tambah++;
ada=false;
}
else
{
op[op_tambah]='0';
op_tambah++;
}
}
while (!opr_stack.empty())
{
postfix << opr_stack.top();
opr_stack.pop();
}
cout << "\n\nPostfix : " << postfix.str() << endl;
/*
postfix >> titip;
for(int i=0; i<infix.length();i++)
{
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^')
{
cek_tambah++;
}
else
{
cek[cek_tambah] << infix[i];
}
}
for(int i=0; i<cek_tambah+1; i++)
{
cek[i] >> bil[i];
cout<< endl<<bil[i]<<" ";
}
string titip_stack, titip_op;
postfix >> titip_stack;
stringstream nitip[100];
int nitip_tam=0;
int ekse[4], ekse_tam=0;
int a,b,c,d=0;
int bil_tam=0;
for(int i=0; i<postfix.str().length(); i++)
{
if (postfix.str()[i] == '+' || postfix.str()[i] == '-' || postfix.str()[i] == '*' || postfix.str()[i] == '/' || postfix.str()[i] == '^')
{
d++;
nitip[nitip_tam] <<postfix.str()[i];
nitip[nitip_tam] >> titip_op;
nitip_tam++;
cout<<endl<<eksekusi(ekse[0], ekse[1], titip_op);
ekse_tam=1;
}
else
{
if(bil[bil_tam]!=postfix.str()[i])
{
nitip[nitip_tam] << postfix.str()[i];
cout<<endl<<ekse[ekse_tam];
}
else
{
nitip[nitip_tam] >> ekse[ekse_tam];
cout<<endl<<ekse[ekse_tam];
ekse_tam++;
nitip_tam++;
}
cout<<endl<<ekse[ekse_tam];
}
cout<<endl<<ekse[ekse_tam];
}
*/
return 0;
}
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;
int opr(char p) {
int temp;
if (p == '^')
temp = 1;
else if (p == '*' || p == '/')
temp = 2;
else if (p == '+' || p == '-')
temp = 3;
return temp;
}
int hasil_akhir;
int eksekusi(int op1, int op2, string op3)
{
stringstream con;
char op4;
con << op3;
con >> op4;
int ans;
switch(op4){
case '+':
ans = op2 + op1;
break;
case '-':
ans = op2 - op1;
break;
case '*':
ans = op2 * op1;
break;
case '/':
ans = op2 / op1;
break;
}
return ans;
}
bool isOpr(string opr)
{
if((opr=="*") || (opr=="/")||(opr=="%")||(opr=="+") || (opr=="-")||(opr=="^"))
return true;
else
return false;
}
int main() {
string infix, titip;
cout << "Masukan Infix : ";
getline(cin, infix);
int hasil,bil[infix.length()], op_tambah=0;
string op[infix.length()];
bool ada=false;
stack<char> opr_stack;
stack<char> opr_hasil;
stringstream postfix;
stringstream cek[infix.length()];
int cek_tambah=0;
for (int i = 0; i < infix.length(); i++)
{
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^')
{
ada = true;
while (!opr_stack.empty() && opr(opr_stack.top()) <= opr(infix[i]))
{
postfix << opr_stack.top();
opr_stack.pop();
}
opr_stack.push(infix[i]);
}
else if (infix[i] == '(') {
opr_stack.push(infix[i]);
}
else if (infix[i] == ')')
{
while (opr_stack.top() != '(')
{
postfix << opr_stack.top();
opr_stack.pop();
}
opr_stack.pop();
}
else
{
postfix << infix[i];
}
if(ada==true)
{
op[op_tambah]=infix[i];
op_tambah++;
ada=false;
}
else
{
op[op_tambah]='0';
op_tambah++;
}
}
while (!opr_stack.empty())
{
postfix << opr_stack.top();
opr_stack.pop();
}
cout << "\n\nPostfix : " << postfix.str() << endl;
/*
postfix >> titip;
for(int i=0; i<infix.length();i++)
{
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^')
{
cek_tambah++;
}
else
{
cek[cek_tambah] << infix[i];
}
}
for(int i=0; i<cek_tambah+1; i++)
{
cek[i] >> bil[i];
cout<< endl<<bil[i]<<" ";
}
string titip_stack, titip_op;
postfix >> titip_stack;
stringstream nitip[100];
int nitip_tam=0;
int ekse[4], ekse_tam=0;
int a,b,c,d=0;
int bil_tam=0;
for(int i=0; i<postfix.str().length(); i++)
{
if (postfix.str()[i] == '+' || postfix.str()[i] == '-' || postfix.str()[i] == '*' || postfix.str()[i] == '/' || postfix.str()[i] == '^')
{
d++;
nitip[nitip_tam] <<postfix.str()[i];
nitip[nitip_tam] >> titip_op;
nitip_tam++;
cout<<endl<<eksekusi(ekse[0], ekse[1], titip_op);
ekse_tam=1;
}
else
{
if(bil[bil_tam]!=postfix.str()[i])
{
nitip[nitip_tam] << postfix.str()[i];
cout<<endl<<ekse[ekse_tam];
}
else
{
nitip[nitip_tam] >> ekse[ekse_tam];
cout<<endl<<ekse[ekse_tam];
ekse_tam++;
nitip_tam++;
}
cout<<endl<<ekse[ekse_tam];
}
cout<<endl<<ekse[ekse_tam];
}
*/
return 0;
}
Hasil