Queue
Queue
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>
//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;
}
}
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