Linear data structures queue
This presentation is the property of its rightful owner.
Sponsored Links
1 / 53

Linear Data Structures (Queue) PowerPoint PPT Presentation


  • 118 Views
  • Uploaded on
  • Presentation posted in: General

Linear Data Structures (Queue). Oleh : Nur Hayatin, S.ST. Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik 2010-2011. Sub Topik. Queue Operasi Queue Implementasi Queue Latihan. QUEUE (Antrian). Definisi. Urutan elemen yang mengikuti konsep FIFO.

Download Presentation

Linear Data Structures (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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Linear data structures queue

Linear Data Structures(Queue)

Oleh : Nur Hayatin, S.ST

Teknik Informatika - Universitas Muhammadiyah Malang (UMM)

Tahun Akademik 2010-2011


Sub topik

Sub Topik

  • Queue

  • Operasi Queue

  • Implementasi Queue

  • Latihan


Queue antrian

QUEUE(Antrian)


Definisi

Definisi

  • Urutan elemen yang mengikuti konsep FIFO.

  • FIFO(First In First Out)

  • Front  menunjuk pada elemen yang paling atas.

  • Rear  menunjuk pada elemen yang paling belakang.


Gambaran proses

Gambaran Proses

Front: Depan

Rear: Belakang


Queue antrian1

Queue (Antrian)

Front

Rear


Queue antrian2

Queue (Antrian)

Front

Rear


Queue antrian3

Queue (Antrian)

Front

Rear


Queue antrian4

Queue (Antrian)

Front

Rear


Queue antrian5

Queue (Antrian)

Front

Rear


Queue antrian6

Queue (Antrian)

Front

Rear


Operasi queue

Operasi Queue


Operasi

Operasi

  • Enqueue/put

    (operasi penambahan elemen)

  • Dequeue/remove

    (operasi penghapusan elemen)

  • Get front

    (operasi pengaksesan elemen terdepan)

  • Get rear

    (operasi pengaksesan elemen paling belakang)


Operasi enqueue add

Operasi Enqueue (add)

  • Operasi penambahan elemen pada antrian.

  • Dilakukan pada rear.

  • Increment rear.


Operasi dequeue remove

Operasi Dequeue (remove)

  • Operasi penghapusan elemen pada antrian.

  • Untuk Queue array terjadi pergeseran elemen dari belakang ke depan.

  • Dilakukan pada Front.

  • Decrement rear.


Operasi get front

Operasi Get Front

  • Pengambilan/pengaksesan elemen yang paling depan.

  • Elemen yang ditunjuk oleh front.


Operasi get rear

Operasi Get Rear

  • Pengambilan/pengaksesan elemen yang paling belakang.

  • Elemen yang ditunjuk oleh rear.


Contoh penerapan queue

Contoh Penerapan Queue

  • mailbox dalam komunikasi antar proses

  • simulasi dan modeling (misalnya simulasi sistem pengendali lalu lintas udara) dalam memprediksi performansi

  • Waiting Line pada Sistem Operasi


Queue dengan circular array

QUEUE DENGAN CIRCULAR ARRAY


Circular array

Circular Array

  • Mampu melakukan proses penghapusan elemen tanda melakukan pergeseran.


Aturan circular array

Aturan Circular Array

  • Proses penghapusan dilakukan dengan cara nilai depan (front) ditambah 1 : depan=depan + 1.

  • Proses penambahan elemen sama dengan queue linear array yaitu nilai belakang ditambah 1 : belakang=belakang + 1.

  • Jika depan = maks dan ada elemen yang akan dihapus, maka nilai depan = 1.

  • Jika belakang = maks dan depan tidak 1 maka jika ada elemen yang akan ditambahkan, nilai belakang=1.

  • Jika hanya tinggal 1 elemen di queue (depan = belakang), dan akan dihapus maka depan diisi 0 dan belakang diisi dengan 0 (queue kosong).


Circular array1

Circular Array


Proses circular array

Proses Circular Array


Proses circular array1

Proses Circular Array


Queue dalam program

Queue dalam Program


The interface queue

The Interface Queue

public interface Queue

{

public boolean isEmpty();

public Object getFrontEelement();

public Object getRearEelement();

public void put(Object theObject);

public Object remove();

}


Class arrayqueue

Class ArrayQueue


Inisialisasi awal

Inisialisasi Awal

public class ArrayQueue implements Queue

{

int front=0;

int rear=-1;

Object [] queue;


Constructor

Constructor

public ArrayQueue(int initialCapacity)

{

if (initialCapacity < 1)

throw new IllegalArgumentException

("initialCapacity must be >= 1");

queue = new Object [initialCapacity + 1];

}

public ArrayQueue()

{

this(10);

}


Method empty

Method empty()

public boolean isEmpty()

{

return rear == -1;

}


Method getfront

Method getFront()

public Object getFrontElement()

{

if (isEmpty())

return null;

else

return queue[front];

}


Method getrear

Method getRear()

public Object getRearElement()

{

if (isEmpty())

return null;

else

return queue[rear];

}


Method put

Method put()

public void put(Object theElement)

{

if (rear == queue.length - 1)

{

Object [] newArray = new Object [2*queue.length];

  System.arraycopy(queue, 0, newArray, 0, queue.length);

  queue = newArray;

}

  queue[++rear] = theElement;

}


Method remove

Method remove()

public Object remove()

{

if (isEmpty())

return null;

Object frontElement = queue[front];

for(int i = 1;i<=rear; i++)

queue[i-1] = queue[i];

--rear;

return frontElement;

}


Method main

Method main()

public static void main(String [] args)

{

int x;

ArrayQueue q = new ArrayQueue(3);

q.put(new Integer(1)); q.put(new Integer(2));

q.put(new Integer(3)); q.put(new Integer(4));

q.remove(); q.remove();

q.put(new Integer(5)); q.put(new Integer(6));

q.put(new Integer(7)); q.put(new Integer(8));

q.put(new Integer(9)); q.put(new Integer(10));

q.put(new Integer(11)); q.put(new Integer(12));

while (!q.isEmpty())

{

System.out.println("Rear element is " + q.getRearElement());

System.out.println("Front element is " + q.getFrontElement());

System.out.println("Removed the element " + q.remove());

}

}

}


Class linkedqueue

Class LinkedQueue


Class chainnode

Class ChainNode

class ChainNode

{

// package visible data members

Object element;

ChainNode next;

// package visible constructors

ChainNode() {}

ChainNode(Object element)

{this.element = element;}

ChainNode(Object element, ChainNode next)

{this.element = element;

this.next = next;}

}


Inisialisasi awal1

Inisialisasi Awal

public class LinkedQueue implements Queue

{

protected ChainNode front;

protected ChainNode rear;


Method isempty

Method isEmpty()

public boolean isEmpty()

{

return front == null;

}


Method getfront1

Method getFront()

public Object getFrontElement()

{

if (isEmpty())

return null;

else

return front.element;

}


Method getrear1

Method getRear()

public Object getRearElement()

{

if (isEmpty())

return null;

else

return rear.element;

}


Method put1

Method put()

public void put(Object theElement)

{

ChainNode p = new ChainNode(theElement, null);

if (front == null)

front = p; // empty queue

else

rear.next = p; // nonempty queue

rear = p;

}


Method remove1

Method remove()

public Object remove()

{

if (isEmpty())

return null;

Object frontElement = front.element;

front = front.next;

if (isEmpty())

rear = null; // enable garbage collection

return frontElement;

}


Method main1

Method main()

public static void main(String [] args)

{

int x;

LinkedQueue q = new LinkedQueue(3);

q.put(new Integer(1));

q.put(new Integer(2));

q.put(new Integer(3));

q.put(new Integer(4));

while (!q.isEmpty())

{

System.out.println("Rear element is " + q.getRearElement());

System.out.println("Front element is " + q.getFrontElement());

System.out.println("Removed the element " + q.remove());

}

}

}


Class circularqueue

Class CircularQueue


Inisialisasi awal2

Inisialisasi Awal

  • class CircularQueue

  • {

    • private intmaxSize;

    • private int[] queArray;

    • private int front;

    • private int rear;

    • private intnItems;


Constructor1

Constructor

  • public CircularQueue(int s)

  • {

    • maxSize = s;

    • queArray = new int[maxSize];

    • front = 0;

    • rear = -1;

    • nItems = 0;

  • }


Method isempty1

Method isEmpty()

  • public booleanisEmpty()

  • {

    • return (nItems==0);

  • }


Method peekfront

Method peekFront()

public intpeekFront()

{

return queArray[front];

}


Method insert

Method insert()

  • public void insert(int j)

  • {

    • if(rear == maxSize-1)

    • rear = -1;

    • queArray[++rear] = j;

    • nItems++;

  • }


Method remove2

Method remove()

  • public int remove()

  • {

    • int temp = queArray[front++];

    • if(front == maxSize)

    • front = 0;

    • nItems--;

    • return temp;

  • }


Method main2

Method main()

  • public static void main(String[] args)

  • {

    • CircularQueuetheQueue = new CircularQueue(5); // queue holds 5 items

    • System.out.println("front : " + theQueue.front + "rear : " + theQueue.rear);

    • theQueue.insert(10); theQueue.insert(20);

    • theQueue.insert(30);theQueue.insert(40);

    • theQueue.remove();

    • System.out.println(“remove 1x, front : " +theQueue.front+" rear : " +theQueue.rear);

    • theQueue.insert(50);

    • theQueue.insert(60);

    • System.out.println("add 2 elemen, front : " +theQueue.front+"rear:”+theQueue.rear);

    • theQueue.remove();

    • System.out.println(“remove 1x, front : " +theQueue.front+ “rear : " +theQueue.rear);

    • while( !theQueue.isEmpty() )

    • {

      • int n = theQueue.remove();

      • System.out.print(n);

      • System.out.print(" ");

    • }


Pustaka

Pustaka

  • Sartaj Sahni, Presentation L5 & L10

  • Jokonowo, Bambang S.Si, “Pemrograman Berorientasi Obyek”, Pusat pengembangan bahan ajar UMB, 2006.

  • Noviyanto, “Pemrograman Berorientasi Obyek (PBO) – Array”, 2005

  • Nugroho, Adi, “Algoritma dan Struktur Data Dalam Bahasa Java”, ANDI Yogyakarta, 2008.

  • Michaell Waite, ”Data Structures and Algorithms in Java”, SAMS, 2001


  • Login