Contoh Program Queue Dengan Linked List
Mari belajar queue suatu struktur data antrian yang memiliki dua versi yaitu diterapkan dengan array atau dengan linked list yang saya contohkan di bawah ini dengan linked list. Sebelum mengenal jauh dengan queue alangkah baiknya kita memahmi dulu apa sih queue itu?? Queue Adalah suatu bentuk khusus dari linear list dengan operasi penyisipan. Contoh Program Queue Dengan Linked List 9,5/10 9534 reviews Struktur data queue (antrian) dapat diimplementasikan dengan menggunakan array maupun linked list sebagai penyimpanan datanya. Dalam contoh program berikut ini saya gunakan double linked list untuk implementasi queue.
/*Queue - Linked List implementation*/ |
#include<stdio.h> |
#include<stdlib.h> |
struct Node { |
int data; |
struct Node* next; |
}; |
// Two glboal variables to store address of front and rear nodes. |
struct Node* front = NULL; |
struct Node* rear = NULL; |
// To Enqueue an integer |
voidEnqueue(int x) { |
struct Node* temp = |
(struct Node*)malloc(sizeof(struct Node)); |
temp->data =x; |
temp->next = NULL; |
if(front NULL && rear NULL){ |
front = rear = temp; |
return; |
} |
rear->next = temp; |
rear = temp; |
} |
// To Dequeue an integer. |
voidDequeue() { |
struct Node* temp = front; |
if(front NULL) { |
printf('Queue is Emptyn'); |
return; |
} |
if(front rear) { |
front = rear = NULL; |
} |
else { |
front = front->next; |
} |
free(temp); |
} |
intFront() { |
if(front NULL) { |
printf('Queue is emptyn'); |
return; |
} |
return front->data; |
} |
voidPrint() { |
struct Node* temp = front; |
while(temp != NULL) { |
printf('%d',temp->data); |
temp = temp->next; |
} |
printf('n'); |
} |
intmain(){ |
/* Drive code to test the implementation. */ |
// Printing elements in Queue after each Enqueue or Dequeue |
Enqueue(2); Print(); |
Enqueue(4); Print(); |
Enqueue(6); Print(); |
Dequeue(); Print(); |
Enqueue(8); Print(); |
} |
commented May 25, 2015
Thnx |
Circular Queue Program package com.javainuse; import java.util.Arrays; public class CircularQueueImplementation { public static void main(String[] args). Linked List,Pointer dan Contoh Program C++ LINKED LIST Linked list (list bertaut) adalah salah satu struktur data dasar yang sangat fundamental dalam bidang ilmu komputer.
There is a mistake in dequeue method ..you need to do temp = temp->next; |
commented Apr 4, 2016
Shouldn't you return something with the Dequeue function? |
Why have you used the Front function? It's never really called. Please explain the purpose of this function definition. |
commented Jul 23, 2016
@MRSharff - The purpose of dequeue function is to delete a node from the linked list. It has nothing to do with returning something. |
Thanks a lot for this sample code. |
commented Nov 15, 2016 • edited
edited
Hey, There is a typo in Front function, when frontNULL - > return -1 or something instead of nothing. |
Your code is very helpful here is another example hope it adds to your concept. |
commented Apr 28, 2017
There is a simpler one here |
commented Nov 27, 2017 •
commented Dec 20, 2017
what we must do if the Data in our struct was int and string (number and name of student) |
can anyone tell me what is the use of the function int Front in this code?I am confused. |
commented Jan 29, 2018
@labeelola This is when you want to view, the latest value in front of the queue. This is not called, but that's not a problem, you can call it anywhere. It is just to view. |
Queue Using Linked List In C Program
Hey guys, Please help me to complete my Assignment, I dont know much about C Programming
Remark: ADT Queue is 'First in First Out'. enqueue - always add a new node at the back of the queue. dequeue - delete the head of the queue. |
commented Aug 10, 2018
Thank you so much |
sir make videos on other topics also,i observed that u r not uploading videos from past 2-3 years ,why sir? |
commented Oct 13, 2018
THANKS ....IT'S SIMPLE AND COOL .... |
Its to get the first element of the Queue(He didn't call it though) |
commented Feb 11, 2019
If anyone tried with local front and rear variables in main method ! |
in the Dequeue function when the second if(frontrear) runs we have to free the space of node pointed by front previously i.e first node is the one when front and rear are equal if we modify the pointer front and rear to NULL we are wasting the memory of first node that was created |
#include <iostream.h>
#include <conio.h>
class linked_list_queue{
private:
struct node{
int data;
node *next;
};
node *rear;
node *entry;
node *print;
node *front;
public:
linked_list_queue();
void enqueue();
void dequeue();
void print_list();
};
linked_list_queue::linked_list_queue(){
rear=NULL;
front=NULL;
}
void linked_list_queue::enqueue(){
int angka;
cout<<'masukkan angka kedalam queue:';cin>>angka;
entry=new node;
if(rearNULL){
entry->data=angka;
entry->next=NULL;
rear=entry;
front=rear;
}
else{
entry->data=angka;
entry->next=NULL;
rear->next=entry;
rear=entry;
}
cout<<endl;
cout<<'data '<<angka<<' telah masuk';
getch();
}
void linked_list_queue::dequeue(){
if(frontNULL){
cout<<'queue kosong';cout<<endl;
}
else{
int deleted_element=front->data;
node *temp;
Contoh Program Queue Dengan Linked List Powerpoint
temp=front;
front=front->next;
delete temp;
cout<<deleted_element<<' telah dihapus';
cout<<endl;
}
getch();
}
void linked_list_queue::print_list(){
print=front;
if(print!=NULL){
cout<<'angka dalam queue:';
}
else{
cout<<'queue kosong';cout<<endl;
Contoh Program Queue Dengan Linked List Pada
}while(print!=NULL){
cout<<print->data;
print=print->next;
}
getch();
}
main(){
int pilih;
linked_list_queue queue;
do{
clrscr();
cout<<'1.enqueue data'<<endl;
cout<<'2.dequeue data'<<endl;
cout<<'3.tampilkan data'<<endl;
cout<<'masukkan pilihan:';cin>>pilih;cout<<endl;
if(pilih1){
queue.enqueue();
}
if(pilih2){
queue.dequeue();
}
if(pilih3){
queue.print_list();
}
}
while(pilih!=0||pilih>3);
getch();
}