0% found this document useful (0 votes)
27 views3 pages

Queue

The document discusses the concept of a queue data structure. A queue follows a First In First Out (FIFO) ordering principle. Elements are added to the rear of the queue and removed from the front. The guided section provides C++ code implementing a queue using an array and demonstrates enqueue, dequeue, count, clear and view functions. It is suggested to modify the code to implement the queue using a linked list instead of an array.

Uploaded by

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

Queue

The document discusses the concept of a queue data structure. A queue follows a First In First Out (FIFO) ordering principle. Elements are added to the rear of the queue and removed from the front. The guided section provides C++ code implementing a queue using an array and demonstrates enqueue, dequeue, count, clear and view functions. It is suggested to modify the code to implement the queue using a linked list instead of an array.

Uploaded by

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

Praktikum Struktur Data dan Algoritme

Materi : Queue

1. Teori Dasar
A Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO). A good example of a queue is any queue of
consumers for a resource where the consumer that came first is served first. The difference
between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.

Queue is used when things don’t have to be processed immediately, but have to be processed
in First In First Out order like Breadth First Search.
This property of Queue makes it also useful in following kind of scenarios.
1. When a resource is shared among multiple consumers. Examples include CPU
scheduling, Disk Scheduling.
2. When data is transferred asynchronously (data not necessarily received at same rate
as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.
3. In Operating systems:
• Semaphores
• FCFS ( first come first serve) scheduling, example: FIFO queue
• Spooling in printers
• Buffer for devices like keyboard
4. In Networks:
• Queues in routers/ switches
• Mail Queues
5. Variations: ( Deque, Priority Queue, Doubly Ended Priority Queue )
Some other applications of Queue:
• Applied as waiting lists for a single shared resource like CPU, Disk, Printer.
• Applied as buffers on MP3 players and portable CD players.
• Applied on Operating system to handle interruption.
• Applied to add song at the end or to play from the front.
• Applied on WhatsApp when we send messages to our friends and they don’t have an
internet connection then these messages are queued on the server of WhatsApp.
Reference: geeksforgeeks
2. Guided
a. Tulislah kode dibawah ini dan analisis baris perbaris dari kode tersebut
b. Silahkan lakukan uji coba kode dibawah ini, dan tentukan apakah kode tersebut
memberikan output yang sesuai dengan konsep queue
#include <iostream>

using namespace std;

//queue array
int maksimalQueue = 5;//maksimal antrian
int front = 0;//penanda antrian
int back = 0;//penanda
string queueTeller[5];

//fungsi pengecekan
bool isFull(){//pengecekan antrian penuh atau tidak
if(back == maksimalQueue){
return true;//=1
}
else{
return false;
}
}

//fungsi pengecekan
bool isEmpty(){//antriannya kosong atau tidak
if(back==0){
return true;
}
else{
return false;
}
}

//fungsi menambahkan antrian


void enqueueAntrian(string data){
if(isFull()){
cout << "antrian penuh"<<endl;
}
else{//nested if, nested for
if(isEmpty()){//kondisi ketika queue kosong
queueTeller[0]=data;
front++;//front = front +1;
back++;
}
else{//antrianya ada isi
queueTeller[back]=data;//queueTeller[1]=data
back++;//back=back+1; 2
}
}
}

//fungsi mengurangi antrian


void dequeueAntrian(){
if(isEmpty()){
cout << "antrian kosong"<<endl;
}
else{
for(int i=0; i<back; i++){
queueTeller[i]=queueTeller[i+1];
}
back--;
}
}

//fungsi menghitung banyak antrian


int countQueue(){
return back;
}

//fungsi menghapus semua antrian


void clearQueue(){
if(isEmpty()){
cout << "antrian kosong"<<endl;
}
else{
for(int i=0; i<back; i++){
queueTeller[i]="";
}
back=0;
front=0;
}
}

//fungsi melihat antrian


void viewQueue(){
cout << "data antrian teller : "<<endl;
for(int i =0; i<maksimalQueue; i++){
if(queueTeller[i]!=""){
cout << i+1 << ". " <<queueTeller[i]<<endl;
}
else{
cout << i+1 << ". (kosong)" <<endl;
}
}
}

int main()
{
enqueueAntrian("Andi");
enqueueAntrian("Maya");
viewQueue();
cout << "jumlah antrian = " << countQueue()<<endl;
dequeueAntrian();
viewQueue();
cout << "jumlah antrian = " << countQueue()<<endl;
clearQueue();
viewQueue();
cout << "jumlah antrian = " << countQueue()<<endl;

return 0;
}

3. Unguided
a. Ubahlah penerapan konsep queue pada bagian guided dari array menjadi linked list

You might also like