Queues JavaScript Implementation Pseudocode and Algorithm
Queues JavaScript Implementation Pseudocode and Algorithm
Implementation Pseudocode and Algorithm of Queues JavaScript .
Algorithm for Queue
- Create a Array for Queue.
- Create Insert function for inserting value to queue from the back side.
- This function will take value from the user and will insert to queue.
- Create Delete function for deleting value from Queue from front side.
- Delete function will check if the queue is empty then it will show the message. else it will delete the value from the front side.
- Create Display function to display the values in Queue.
- It will run a for loop to display all values in Queue from starting point to end point.
Pseudocode for Queue:
- Three buttons for display, insert & delete.
- Var variable= new array()
- Var start
- Var end
- Function insert()
- Get value from the user and add it to the array to end side
- Function delete()
- If(the end is on initial point or start is greater than the end)
- Print empty Queue
- Function display()
- For(starting from start; run until end; increment)
- Print all queue.
First, we have to describe what is a Queue. The queue is an array that store values on a FIFO basis. The value that will comes first will out first. The value that will enter last will out last.
we have to declare an array that will store values for queue.and take 2 variables for front value and back value. Initialy gave them -1 value that means the Queue is empty. If we want to add value than both value will increase to 0 index. If we want to add more values than only back value will be increased. If we want to delete a value that front value will be increased. For display, we will check if the front number value is -1 then it will show a message that there is no value in Queue. If the front value is greater than -1 it means that there is some value in Queue. We will use for loop to show all values in the array. It will be initialized from front value and it will run until it is equal or less than back value. These 3 functions will be called from onclick events.
We will give the title queue to our Assignment. Adding a paragraph for requesting to the user to type this button to insert value. Then adding an onclick event on this button. When we click on the button that will call insert function.
We will do the same for delete and display.
Implementation of Queues in JavaScript
Here, we are sharing the code of Implementation of Queues in JavaScript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<html> <head> <title> Queue </title> </head> <body> <h1> T4Tutorials </h1> <p> Type insert button to add value to queue </p> <button type="button" onclick="insert()"> insert </
button> <p> Type Delete button to Delete value from queue </p> <button type="button" onclick=" del() "> delete </button> <p> Type Display button to print value in queue </p> <button type="button" onclick="display()">
; Display </button> <script> var ary = new Array(); var f=-1; var r=-1; function insert() { var p=prompt (" please enter the number that u want to add "); var x = parseInt (p); if(f==-1) { f=0; } r++; ary[r] = x ; } function display() { if(f==-1) document.write ("Queue is empty"); else{ for(var i=f;i<=r;i++){ document.write (ary[i]+ "<br>");} } } function del() { if(f==-1 || f>r){ document.write ("Queue is empty"); }f++; } </script> </body> </html> |
Output