Source Code :
#include<iostream>
#include<cstdlib>
#define max 5
using namespace std;
struct QUEUE
{
int data[max];
int head;
int tail;
}antrian;
void init()
{
antrian.head=0;
antrian.tail=0;
}
bool kosong()
{
if (antrian.tail==0)
{
return true;
}
else
{
return false;
}
}
bool penuh()
{
if (antrian.tail==max)
{
return true;
}
else
{
return false;
}
}
void tampilData()
{
if (!kosong())
{
for (int i=antrian.head; i<=antrian.tail-1; i++)
{
cout<<antrian.data[i]<<" | ";
}
}
cout<<"\n";
}
void enqueue()
{
tampilData();
if (!penuh())
{
int baru;
cout<<" Data Yang Akan Di Masukkan : ";
cin>>baru;
antrian.data[antrian.tail]=baru;
antrian.tail++;
tampilData();
cout<<"Data telah ditambahkan\n";
}
else
{
cout<<"Antrian Kosong";
}
}
void dequeue()
{
tampilData();
if (!kosong())
{
cout<<"Mengambil data \" "<<antrian.data[antrian.head]<<" \" ... "<<endl;
for(int i=antrian.head; i<antrian.tail; i++)
{
antrian.data[i]=antrian.data[i+1];
}
antrian.tail--;
}
else
{
cout<<"Antrian Kosong";
}
}
void jumlah()
{
int jumlah = 0;
for(int i=antrian.head;i<antrian.tail;i++)
{
jumlah = jumlah + antrian.data[i];
}
cout<<"Jumlah = "<<jumlah<<endl;
}
int main()
{
int pil;
do
{
tampilData();
cout<<" Menu Utama \n";
cout<<"---------------\n";
cout<<" [1] Init \n";
cout<<" [2] Enqueue \n";
cout<<" [3] Dequeue \n";
cout<<" [4] Jumlah \n";
cout<<" [0] Keluar \n";
cout<<"---------------\n";
cout<<"Masukkan Pilihan : ";cin>>pil;
switch (pil)
{
case 1:
init();
break;
case 2:
enqueue();
break;
case 3:
dequeue();
break;
case 4 :
jumlah();
break;
}
}
while(pil!=0);
return 0;
}
Outputnya :
0 comments:
:)) :)] ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} ~x( :-t b-( :-L x( =))
Post a Comment