Home > Ren's Free Time > Queue dengan C++

Queue dengan C++

December 17, 2010 Leave a comment Go to comments

Queue merupakan struktur data dengan konsep FIFO ( First In First Out ), sama seperti dengan Stack dalam membuat Queue dapat dilakukan dengan Array dan Linked List ( as far as I know ). Array atau Linked List dalam membuat Queue tergantung dari masalah yang akan diselesaikan dengan struktur data Queue. Pada Queue terdapat 2 operasi dasar yaitu deQueue untuk menghapus data dan enQueue untuk memasukkan data dalam Queue.

Queue

Ilustrasi Queue

Berikut ini listing code Queue dengan Linked List dalam bahasa C++. dengan tool yang digunakan CodeBlocks 8.02.

#include <iostream>

using namespace std;
struct node{
int data;
node *next;
};
class Queue{
private:
node *head;
node *tail;
public:
Queue();
void enQueue(int i);
void deQueue();
void printElement();
void printHeadTail();
};

Queue::Queue()
{
head = NULL;
tail = NULL;
}
void Queue::enQueue(int i)
{
node *temp;
temp = new node;
temp->data = i;
temp->next = NULL;
if( head == NULL)
{
head = temp;
tail = temp;
}
else{
tail->next = temp;
tail= temp;
}
}

void Queue::deQueue()
{
node *temp;
if(head != NULL)
{
temp = head;
head = head->next;
delete temp;
cout<<"Head removed";
}
else
{
cout<<"Queue Empty"<<endl;
}
}
void Queue::printElement()
{
node *temp;
if( head == NULL)
{
cout<<"Queue Empty"<<endl;
}
else
{
temp = head;
while( temp != tail)
{
cout << temp->data<<"\n";
temp = temp->next;
}
}
}
void Queue::printHeadTail()
{
if(head != NULL)
{
cout<<"Head: "<< head->data <<endl;
cout<<"Tail: "<< tail->data <<endl;
}
else
cout<<"Queue Empty"<<endl;
}

int main()
{
Queue q;
int temp;
int pilihan;
while(1)
{
cout<<"********Queue**********"<<endl;
cout<<"1.enQueue"<<endl;
cout<<"2.deQueue"<<endl;
cout<<"3.View Head and Tail"<<endl;
cout<<"4.View All Data"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your choice:";
cin>>pilihan;
switch(pilihan)
{cout<<endl;
case 1:
cout<<"Insert Data:"<<endl;
cin>>temp;
q.enQueue(temp);
break;
case 2:
q.deQueue();
break;
case 3:
q.printHeadTail();
break;
case 4:
q.printElement();
break;
case 5:
return 0;
}

}

return 0;
}

Berikut ini link Download Code lengkapnya

Semoga membantu😀

Sumber : dari berbagai referensi

Salah koreksi saya. Danke

  1. January 8, 2011 at 14:48

    mancabs,,,🙂

    • Ren
      January 8, 2011 at 14:57

      halo om prio, sehat?

  2. February 22, 2011 at 10:19

    do your best…I like it

    • Ren
      February 22, 2011 at 11:14

      Thanks

  1. September 14, 2013 at 11:11

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: