DIG1G3 Implementasi Struktur Data Program Studi Diploma III Teknik Informatika Fakultas Ilmu Terapan Telkom University Dosen: Cahyana, S.T., M.Kom. Indra Azimi, S.T., M.T.
2 Apa yang dimaksud queue? Apa kegunaan queues? Properti apa saja yang dimiliki queues? Bagaimana implementasinya?
3 Apa yang dimaksud queue? Queue adalah list yang memiliki aturan main dalam proses inserted dan removed data. Konsep queue adalah FIFO (first in first out) Insertion/Enqueue atau penambahan data hanya dilakukan di salah satu ujung list (rear) dan deletions/dequeue hanya dilakukan di ujung lainnya (front) Queue menyerupai sebuah antrian dalam dunia nyata
4 Apa kegunaan queue? Mengatur layanan terhadap suatu task dalam suatu sistem agar tercipta fair treatment client-server systems operating systems: antrian task, contoh antrian untuk mencetak (printing). simulation and modeling: contoh air traffic control, urban transport.
5 Queue ADT Operasi yang mungkin ada pada Queue: 1. Inisialisasi queue Q menjadi empty queue. 2. Menentukan sebuah queue kosong atau tidak, Q, is empty. 3. Menentukan sebuah queue penuh atau tidak, Q, is full. 4. Jika Q tidak penuh, sisipkan item baru pada ujung belakang (rear) queue, Q. 5. Jika Q tidak kosong, hapus sebuah item pada ujung depan (front) queue, Q.
6 Queue ADT interface /* File: Queue.h */ #define MAXQUEUESIZE 100 struct Queue int Count, Front, Rear; char Items[MAXQUEUESIZE]; ; /* Defined Operations */ Queue *QueueNew (); /* Initialize the queue Q to be the empty queue. */ int QueueEmpty ( Queue *Q ); /* Returns 1 (True) if (and only if) the queue Q is empty*/ int QueueFull ( Queue *Q ); /* Returns 1 (True) if (and only if) the queue Q is full */ void QueueInsert (char R, Queue *Q ); /* If Q is not full, insert new item R onto the end of Q */ void QueueRemove ( Queue *Q, char *F ); /* If Q is not empty, remove its first item, put it in F. */
7 Implementasi Queue Untuk representasi sekuensial menggunakan array:
8 Queues dengan Circular Track
9 Queues dengan Circular Track Bagaimana implementasi dalam program? Menggunakan linear array berukuran N. Menggunakan modular arithmetic: ekspresi X % N menghasilkan nilai X tetap dalam range 0..N-1. Set Rear: tambahkan nilai rear lama dengan 1; jika rear=n, set dengan 0. Rear = (Rear + 1) % N; Hal yang sama untuk Front: Front = (Front + 1) % N;
10 Sequential Queue Representation (I) Definisikan MAXQUEUESIZE sebagai konstanta. Sebuah queue merupakan struct yang mengandung: number of items pada queue sebagai Count member Array item queue sebagai Items member Array indeks (Front) Array indeks (Rear) File Queue.c berisi definisi dan operasi QueueNew, QueueEmpty, QueueFull, QueueInsert dan QueueRemove (slide sebelumnya)
11 Sequential Queue Representation (I) Initialization: inisialisasi queue Q menjadi queue kosong. sebuah queue disebut empty jika Count =0. Count diset = 0. indeks Front diset = 0. indeks Rear diset = 0.
12 Sequential Queue Representation /* Initialize the queue Q to be the empty queue. */ Queue * QueueNew () Queue *Q = new Queue; Q->Count = 0; Q->Front = 0; Q->Rear = 0; return Q ;
13 Sequential Queue Representation Empty test: menentukan sebuah queue kosong atau tidak, Q, is empty. Sebuah queue didefinisikan empty jika Count =0. /* Returns 1 (True) if (and only if) * the queue Q is empty. */ int QueueEmpty ( Queue *Q ) return ( Q->Count == 0 );
14 Sequential Queue Representation Full test: menentukan sebuah queue penuh atau tidak, Q, is full. sebuah queue didefinisikan full jika Count = MAXQUEUESIZE. /* Returns 1 (True) if (and only if) * the queue Q is full. */ int QueueFull ( Queue *Q ) return ( Q->Count == MAXQUEUESIZE );
15 Sequential Queue Representation Insert a new item onto the rear of the queue, Q. /* If Q is not full, insert a new item R onto rear of Q by: * 1. storing R in the item array at position Rear; * 2. incrementing Rear by one (WRAP AROUND!); * 3. incrementing Count by one. */ void QueueInsert (char R, Queue *Q) if (Q->Count == MAXQUEUESIZE) cout<<"insert:not allowed on full queue. <<endl; else Q->Items[Q->Rear] = R; Q->Rear = (Q->Rear + 1) % MAXQUEUESIZE; Q->Count++;
16 Sequential Queue Representation Jika queue Q is not empty, remove sebuah item dari front sebuah Q. /* If Q is not empty, remove first item and put it in F by: * 1. taking F from the item array at position Front; * 2. incrementing Front by one (WRAP AROUND!); * 3. decrementing Count by one. */ void QueueRemove ( Queue *Q, char *F ) if ( Q->Count == 0 ) cout<< REMOVE:Not allowed on empty q. <<endl; else *F = Q->Items [Q->Front]; Q->Front = (Q->Front + 1) % MAXQUEUESIZE; Q->Count--;
17 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main () Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Orang lain dapat menulis kode di atas tanpa mengetahui bagaimana queue tersebut diimplementasikan.
18 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main ( void ) Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Queue: Empty
19 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main ( void ) Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Queue: a
20 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main ( void ) Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Queue: a, b
21 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main ( void ) Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Queue: a, b, c
22 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main ( void ) Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Queue: b, c Print: Just got: a.
23 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main ( void ) Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Queue: c Print: Just got: b.
24 Using the Queue interface #include "QueueInterface.h" /* Assume ItemType is char.*/ /* Access operations and types for stacks.*/ int main ( void ) Queue *tq; /* Variable TestQueue of type Queue. */ char C; tq = QueueNew ( ); /* Make tq empty. */ QueueInsert ( a, tq ); QueueInsert ( b, tq ); QueueInsert ( c, tq ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueRemove ( tq, &C ); printf ( "Just got: %c.\n", C ); QueueInsert ( d, tq );... Queue: c, d
25 Linked Queue Representations Representasi berikut menggunakan pointer pada front dan rear. Setiap node terdiri atas item queue yaitu item member dan links ke node berikutnya pada list, menggunakan pointer pada link member.
26 Linked Queue Implementation Conditional checks untuk kondisi tertentu pada linked queue implementation:
27 Linked Queue Representations /*File: queuetypes.h*/ typedef struct tqueuenode char item; tqueuenode *address; QueueNode; typedef struct tqueue QueueNode *front; QueueNode *rear; Queue;
28 Linked Queue Implementation #include QueueInterface.h /*Initialize the queue Q to be the empty queue*/ void InitializeQueue(Queue *Q) Q->front=NULL; Q->rear=NULL;
29 Linked Queue Implementation Empty test: menentukan sebuah queue kosong atau tidak, Q, is empty. Sebuah queue didefinisikan empty jika front = NULL. boolean Empty(Queue *Q) return (Q->front==NULL);
30 Linked Queue Implementation Full test: menentukan sebuah queue penuh atau tidak, Q, is full. boolean Full(QueueNode *nb) return (nb==null);
31 Linked Queue Implementation Insert item baru ke ujung rear suatu queue, Q. void Insert(char R, Queue *Q) QueueNode *temp = new QueueNode; if (Full(temp)) systemerror( system storage is exhausted ) else temp->item = R; temp->address=null; if (Q->rear==NULL) Q->front=temp; Q->rear=temp; else Q->rear->address=temp; Q->rear=temp;
32 Linked Queue Implementation Terdapat Q tidak kosong, remove sebuah item dari front sebuah queue Q. void remove(queue *Q, char *F) QueueNode *temp; if (Q->front==NULL) systemerr( attempt to remove item from empty queue ); else *F=Q->front->item; temp=q->front; Q->front=temp->address; free(temp); if (Q->front==NULL) Q->rear=NULL;
33 Queue Applications Queues pada operating systems Menggunakan queues sebagai memory buffers Queues pada simulation experiments Clients - servers Simulating supermarket checkout lines
34 References Lectures stacks and queues. Dept of Computer Science, University of Bristol, 2004 Thomas A. Standish, Data Structures, Algorithms & Software Principles in C Addison- Wesley, 1995 p. 253 ff (Chapter 7) Tjokorda Agung Budi Wirayuda, PI1043 Struktur Data: Queue Data Structure.