0% found this document useful (0 votes)
10 views2 pages

Queue en Que

This C++ program implements a queue data structure using an array. It defines a Queue struct with data, head, and tail fields. The main function contains a menu to allow enqueueing (adding) data to the queue or exiting. The Enqueue function adds an element to the tail of the queue if not full, and the tampil function prints the queue elements from head to tail.

Uploaded by

ichaaja395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Queue en Que

This C++ program implements a queue data structure using an array. It defines a Queue struct with data, head, and tail fields. The main function contains a menu to allow enqueueing (adding) data to the queue or exiting. The Enqueue function adds an element to the tail of the queue if not full, and the tampil function prints the queue elements from head to tail.

Uploaded by

ichaaja395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

nclude <iostream>

#include <stdlib.h>
#include <conio.h>
#define MAX 5
using namespace std;

typedef struct {
int data[MAX];
int head;
int tail;
} Queue;

Queue antrian;

void Enqueue(int data);


void tampil();

int main () {
antrian.head=antrian.tail=-1;
int pilih;
menu:
{
system("cls");
cout<<"Tampilan Data"<<endl;
cout<<"============="<<endl;
tampil();
cout<<"============="<<endl;
cout<<endl<<endl;

cout<<"Menu Program"<<endl;
cout<<"============="<<endl;
cout<<"1. Enqueue (Tambah)"<<endl;
cout<<"2. Exit"<<endl;
cout<<"============="<<endl;
cout<<endl;
cout<<"Pilih : ";cin>>pilih;
switch(pilih)
{
case 1:
int data;
cout<<"Input Data : ";cin>>data;
Enqueue(data);
getch();
break;
case 2:
exit(0);
}
goto menu;
}
}

int IsEmpty(){
if(antrian.tail== -1)
return 1;
else
return 0;
}

int IsFull(){
if(antrian.tail==MAX-1)
return 1;
else
return 0;
}

void Enqueue (int data){


if(IsEmpty()==1){
antrian.head=antrian.tail=0;
antrian.data[antrian.tail]=data;
cout<<" Data : "<<antrian.data[antrian.tail];
cout<<" Masuk antrian ke "<<antrian.tail + 1;
}
else if(IsFull()==0){
antrian.tail++;
antrian.data[antrian.tail]=data;
cout<<" Data : "<<antrian.data[antrian.tail];
cout<<" Masuk antrian ke "<<antrian.tail + 1;
}
else if(IsEmpty()==0 || IsFull()==1){
cout<<" Antrian Penuh !"<<endl;
}
}

void tampil(){
if(IsEmpty()==0){
for(int i=antrian.head; i<=antrian.tail; i++){
cout<<" Antrian "<<i+1;
cout<<" Data : "<<antrian.data[i]<<endl;
}
}
else {
cout<<" Tidak Ada Data"<<endl;
}
}

You might also like