1 / 15

QUEUE

QUEUE. Pertemuan ke-6 T 0026. D. D. R. R. C. C. C. R. B. B. B. B. R. F. A. A. A. A. R. F. F. F. F. Queue. Bentuk khusus dari ordered list Data di insert di akhir list Data di delete di awal list. Queue sebagai ADT. Misal, queue Q terdiri dari n elemen

italia
Download Presentation

QUEUE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. QUEUE Pertemuan ke-6T 0026

  2. D D R R C C C R B B B B R F A A A A R F F F F Queue • Bentuk khusus dari ordered list • Data di insert di akhir list Data di delete di awal list

  3. Queue sebagai ADT Misal, queue Q terdiri dari n elemen Q = ( ao , a1 , … , ai , ai+1 , …, an-1 ) Elemen paling depan FRONT Elemen paling belakang REAR • ao : elemen yang diinsert pertama kali • an-1 : elemen yang diinsert terakhir

  4. Representasi Queue • Yang paling sederhana : array 1-D • Disebut Sequential Queue • 2 variabel indikator REAR dan FRONT #define MAX_QUEUE_SIZE 100 eltype queue[MAX_QUEUE_SIZE]; int rear = -1; int front = -1;

  5. Aplikasi Queue dengan Array 1-D

  6. Sequential Queue : Add Queue void addq (int *rear, eltype item) { if (*rear == MAX_QUEUE_SIZE-1) { printf (“Queue penuh\n”); return; } queue[++*rear] = item; } /* indeks rear di-increment sebelum insert data baru */

  7. Sequential Queue : Delete Queue eltype deleteq(int *front, int rear){ if (front == rear) { printf (“queue empty\n”); return; } return queue(++*front); } /*indeks front di-increment sebelum passing nilai yang disimpan di queue */

  8. Kekurangan Sequential Queue • Setelah beberapa data insert dan delete dari queue, REAR dan FRONT bergerak ke kanan, hingga REAR mencapai MAX_QUEUE_SIZE-1. • Jika REAR mencapai maksimum, queue akan dideteksi PENUH, meskipun beberapa lokasi di depan FRONT kosong. • Solusi : elemen queue harus digeser ke depan shg FRONT=-1 dan REAR != MAX_QUEUE_SIZE-1 • Tetapi perlu waktu untuk menggeser elemen satu persatu. • Lebih baik gunakan circular array untuk queue

  9. [2] [2] [3] [3] F=4 R=4 [1] [1] [4] [4] [0] [0] [5] [5] F=0 R=0 Circular Queue : Empty Empty : front = rear

  10. Full : front = (rear + 1) % MAX_QUEUE_SIZE R F [2] [2] [3] [3] J2 J3 J8 J1 J4 [1] [1] [4] [4] J7 J4 J5 J6 J5 [0] [0] [5] [5] F R Circular Queue : Full

  11. [2] [3] J1 [1] [4] R [0] [5] F Circular Queue : Insert [2] [3] R J2 J1 [1] [4] [0] [5] F Insert : REAR berubah, FRONT tetap *rear = (*rear+1) % MAX_QUEUE_SIZE

  12. R F [2] [2] [3] [3] J8 J8 [1] [1] [4] [4] J7 J7 J4 J4 J6 J6 J5 J5 [0] [0] [5] [5] Circular Queue : Delete R Delete : FRONT berubah, REAR tetap *front = (*front+1) % MAX_QUEUE_SIZE F

  13. Circular Queue : Add Queue void addq(int front,int *rear, eltype item){ *rear = (*rear+1)%MAX_QUEUE_SIZE; if (front = *rear) { printf (“Queue full\n”); return; } queue[*rear] = item; }

  14. Circular Queue : Delete Queue eltype deleteq (int *front, int rear){ eltype item; if (*front == rear) { printf (“Queue empty\n”); return; } *front = (*front+1)%MAX_QUEUE_SIZE; return queue[*front]; }

  15. Implementasi Queue dengan Single Linked-List Typedef struct elqueue { char name[10]; float ip; struct elqueue *link; } nama ip link nama ip nama ip front link link Tono 3.8 Dewi 4.0 Ani 3.7 NULL rear

More Related