Rabu, 21 Maret 2018

Stack & Queue


1.     Buatlah 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)

Ø Kodingan
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#define max 10

using namespace std;

struct queue{
int head;
int tail;
int data [max];
int tampung;
   };

queue antri;

void awal(){
 antri.head=-1;
 antri.tail=-1;
   }

int kosong(){
 if (antri.tail==-1) {
   antri.head=-1;   return 1;
   }
   else
   return 0;
      }

int penuh(){
 if (antri.tail==max-1)
 return 1;
 else
 return 0;
}


void input(int data){
 if(kosong()==1)
    {antri.head=0;
    antri.tail++;
    antri.data[antri.tail]=data;
    cout<<"data berhasil"; }
   else if(penuh()==0)
    {antri.head=0;
    antri.tail++;
    antri.data[antri.tail]=data;
    cout<<"data berhasil"; }

   else
       cout<<"data penuh";
   }

void ambil(){
   if(kosong()==0){
      cout<<"data terambil ";
        for (int i=antri.head;i<=antri.tail;i++)
        antri.data[i]=antri.data[i+1];
        antri.tail--;
   }
   else
   cout<<"Data kosong";
   }

void tampil(){



 if(kosong()==0)
   {

         for(int i=0;i<=antri.tail;i++)
        {
            cout<<antri.data[i]<<"  ";
        }
   cout<<endl;
   for (int i=0;i<=antri.tail;i++){
       cout<<"\nIndex ke "<<i<<"="<<antri.data[i];
   }

     }
     else
           cout<<"\nData kosong";
   }


void bersih(){
 antri.head=antri.tail=-1;

cout<<"\nData kosong!";
}

int main(){
int pil,data;
awal();
do
{
cout<<"\nMenu :\n1. Input (Enqueue) \n2. Ambil Data (Dequeue) \n3. Tampil\n4. Bersihkan (CLEAR)\n5. Keluar\nMasukkan pilihan :";
cin>>pil;

switch(pil)
 {case 1:cout<<"\nMasukkan data  = ";cin>>data;
      input(data);
           break;
    case 2:ambil();
       break;
    case 3:tampil();
       break;
    case 4:bersih();
       break;
    }
    getch();
system("cls");     }
while(pil!=5);
}

Ø Hasil
a.     Enqueue


b.     Dequeue

c.      Print

d.     Clear

 2.     Buatlah 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>
#include <conio.h>


using namespace std;

int o(char a) {
    int temp;
    if (a == '^')
        temp = 1;
    else if (a == '*' || a == '/')
        temp = 2;
    else if (a == '+' || a == '-')
        temp = 3;
    return temp;
}

int main() {
    string infix;
    cout<<"\t\t\tKonversi Infix Ke Postfix\n\n";
    cout<<"Masukkan data yang ingin di ubah dari infix ke postfix\n";
    cout << "Masukan Infix yang ingin di ubah : ";
    getline(cin, infix);

    stack<char> opr_stack;

    stringstream postfix;

    for (unsigned i = 0; i < infix.length(); i++) {
        if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
            while (!opr_stack.empty() && o(opr_stack.top()) <= o(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];
        }
    }

    while (!opr_stack.empty()) {
        postfix << opr_stack.top();
        opr_stack.pop();
    }

    cout << "Postfix adalah : " << postfix.str() << endl;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return 0;
    getch();
}

Ø Hasil






Tidak ada komentar:

Posting Komentar