• Tidak ada hasil yang ditemukan

DIG1G3 Implementasi Struktur Data

N/A
N/A
Protected

Academic year: 2021

Membagikan "DIG1G3 Implementasi Struktur Data"

Copied!
34
0
0

Teks penuh

(1)

Program Studi Diploma III Teknik Informatika

Fakultas Ilmu Terapan

Telkom University

Dosen: Cahyana, S.T., M.Kom.

(2)

Apa yang dimaksud queue ?

Apa kegunaan queues ?

Properti apa saja yang dimiliki queues ?

(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

(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 );

(7)

Implementasi Queue

(8)
(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;

(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

(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 );

... }

(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 );

... }

(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 );

... }

(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 );

... }

(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 );

... }

(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 );

... }

(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 );

...

}

(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

(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

Referensi

Dokumen terkait

[r]

Pada saat Peraturan Daerah ini mulai berlaku, Peraturan Daerah Kabupaten Nomor 7 Tahun 2001 tentang Retribusi dan Sewa Pemakaian Kekayaan Daerah (Lembaran Daerah

Kosakata bahasa Indonesia, selain berasal dari bahasa asli yaitu bahasa Melayu (Riau), banyak bahasa dari etnis lainnya yang dipinjam sebagai bagian dalam bahasa

Proses pencatatan akuntansi pada LAZ DPU DT Cabang Semarang dimulai dengan membuat jurnal, buku besar kemudian dibuat laporan keuangan, dengan pengumpulan bukti-bukti

Data yang dikumpulkan meliputi karakteristik sensorik makanan meliputi warna, aroma, rasa dan tekstur makanan yang diperoleh melalui kuesioner makan pagi, siang dan sore

Kondisi SM Rimbang Baling sangat memprihatinkan saat ini, dan sangat disayangkan jika pada akhirnya, pemasalahan yang terjadi di kawasan konservasi menyebabkan

&#34;roses pengeluaran sputum dari paruparu, bronkus dan trakea yang dihasilkan oleh klien &#34;roses pengeluaran sputum dari paruparu, bronkus dan trakea yang dihasilkan oleh

[r]