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

Struktur Data: Teori dan Kode 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)
5 views3 pages

Struktur Data: Teori dan Kode 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

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

Common questions

Powered by AI

Converting a queue from an array to a linked list mitigates fixed size constraints and reduces memory waste from unused space. Linked lists allow dynamic resizing and more efficient dequeue operations without shifting elements, as each node holds a pointer to the next, preserving the FIFO order naturally .

In operating systems, queues are vital in managing resource allocation through first-come, first-served scheduling and semaphores, which control access to resources by ordering requests in the sequence they are received, ensuring efficient management of multitasking operations .

Queues handle asynchronous data transfer by storing data temporarily until it can be processed at the receiving end. Examples include IO Buffers, media players buffering songs, and message queues in communication systems where messages are stored before being delivered .

The 'viewQueue' function prints the elements and positions within the queue, allowing users to visualize current queue status, identify which spots are filled or empty, and thus aids in managing and debugging queue operations effectively .

Using an array-based implementation for queues allows for fixed-size, easily accessible data storage and potentially faster access times, benefiting environments with predictable data sizes. However, such implementations can be inefficient due to fixed size limits, requiring a shift operation that complicates dequeue operations and causes computational overhead .

The 'enqueue' operation adds an element to the end of the queue, ensuring that new elements are appended in a FIFO manner, while 'dequeue' removes the front element, maintaining the order. For example, in the provided code, 'enqueueAntrian' adds to the back and 'dequeueAntrian' shifts elements forward allowing the first-in element to be accessed .

The 'isFull' function checks if the queue is at maximum capacity, preventing overflow, while 'isEmpty' determines if the queue has no elements, avoiding underflow. These functions are crucial for maintaining queue integrity and ensuring proper enqueue and dequeue operations .

In print spooling, documents are printed in the order they are submitted, ensuring fairness, while in message queuing, emails or messages in networks are sent in the order received, maintaining proper sequence and managing loads effectively on network systems .

A queue is beneficial in scenarios where data must be processed in the order it was received, such as CPU scheduling, where processes are handled in the order they arrive, and in networking, where data packets are processed in the order they are received .

A queue is a linear data structure that follows the First In First Out (FIFO) order for processing elements, meaning the earliest added item is the first to be removed. In contrast, a stack is a Last In First Out (LIFO) structure where the most recently added item is the first to be removed .

You might also like