Contoh Program Queue Dengan Linked List
Contoh Program Queue Dengan Linked List Rating: 8,4/10 1969 reviews
Here you will learn about linear queue in C++.
Program Queue Menggunakan Double Linked List » Berlangganan Masukkan alamat surel Anda untuk berlangganan blog ini dan menerima pemberitahuan tulisan-tulisan baru melalui email.
What is Queue?
The queue is a linear data structure where operations of insertion and deletion are performed at separate ends that are known as front and rear. The queue follows FIFO (First in First Out) concept. First element added to the queue will be first one to be removed.
In this program we will implement linear queue using linked list. It is a menu driven program that contains four options insert, delete, display and exit. The program will ask the user to enter the choice and then appropriate functions are invoked to perform specific operation according to the user’s choice.
Also Read: Circular Queue in C
Program for Linear Queue in C++
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 | #include<stdlib.h> usingnamespacestd; structnode intdata; }*front=NULL,*rear,*temp; voidins() temp=newnode; cin>>temp->data; front=rear=temp; { rear=temp; } voiddel() if(frontNULL) else temp=front; cout<<'Deleted node is '<<temp->data<<'n'; } { cout<<'Queue is emptyn'; { while(temp!=NULL) cout<<temp->data<<'->'; } } intmain() intch; { cout<<'nn*** Menu ***'<<'n1.Insertn2.Deleten3.Displayn4.Exit'; cin>>ch; { break; break; break; break; } } |
Output
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice(1-4):1
Enter data:8
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice(1-4):1
Enter data:12
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice(1-4):3
8->12->
*** Menu ***
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice(1-4):4
In the insertion operation, temp points to the new node. If this is first node to be inserted then front will be NULL and now both front and rear points to this new node. If front is not NULL then insertion is similar to adding the node at the end of linked list. The next pointer of rear points to temp and rear becomes temp.
Deletion
For deletion purpose, it is first checked whether front is NULL, if it is NULL, we display the message “Queue is empty”. In case the queue is not empty, deletion is done in such a way that temp pointer points to front and front pointer points to its next node. After displaying data for the node to be deleted, node is deleted by delete(temp) function.
Display
Contoh Program Queue Dengan Linked List C++
For display, it is first checked whether front is NULL, if it is NULL, we display the message “Queue is empty”. If queue is not empty, front pointer is assigned to temp and data for all the nodes are displayed till temp does not become NULL.
If you have any doubts related to above linear queue in C++ program then you can ask it by commenting below.