1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ 4+ < head >
5+ < meta charset ="UTF-8 ">
6+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
7+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
8+ < title > Queue</ title >
9+ </ head >
10+ 11+ < body >
12+ < div align ="center ">
13+ < h3 > Queue with Function</ h3 >
14+ < p > (Open console to check output)</ p >
15+ 16+ < div >
17+ < div >
18+ < input type ="text " id ="inputElem " />
19+ < button onclick ="enQueueData() "> EnQueue </ button >
20+ </ div >
21+ < br />
22+ < div >
23+ < button onclick ="deQueue() "> DeQueue </ button >
24+ < button onclick ="front() "> Get Front Data </ button >
25+ < button onclick ="rear() "> Get Rear Data </ button >
26+ < button onclick ="checkIsEmpty() "> Check Queue Empty </ button >
27+ < button onclick ="display() "> Display </ button >
28+ </ div >
29+ </ div >
30+ </ div >
31+ 32+ < script >
33+ let queue = [ ] ;
34+ let currentIndex = queue . length ;
35+ let maxSize = 6 ;
36+ 37+ function enQueue ( newVal ) {
38+ if ( currentIndex >= maxSize ) {
39+ console . error ( "Queue is already full" ) ;
40+ return false ;
41+ }
42+ queue [ currentIndex ] = newVal ;
43+ currentIndex ++ ;
44+ }
45+ 46+ function deQueue ( ) {
47+ if ( ! isEmpty ( ) ) {
48+ for ( let i = 0 ; i < queue . length ; i ++ ) {
49+ queue [ i ] = queue [ i + 1 ] ;
50+ }
51+ currentIndex -- ;
52+ queue . length -- ;
53+ } else {
54+ console . error ( "Queue is Empty." ) ;
55+ }
56+ }
57+ 58+ function display ( ) {
59+ console . log ( queue ) ;
60+ }
61+ 62+ function front ( ) {
63+ if ( ! isEmpty ( ) ) {
64+ console . log ( queue [ 0 ] ) ;
65+ } else {
66+ console . error ( "Queue is Empty." ) ;
67+ }
68+ }
69+ 70+ function rear ( ) {
71+ if ( ! isEmpty ( ) ) {
72+ console . log ( queue [ currentIndex - 1 ] ) ;
73+ } else {
74+ console . error ( "Queue is Empty." ) ;
75+ }
76+ }
77+ 78+ function isEmpty ( ) {
79+ return currentIndex <= 0 ? true : false ;
80+ }
81+ 82+ const enQueueData = ( ) => {
83+ let inputElem = document . getElementById ( "inputElem" ) ;
84+ enQueue ( inputElem . value ) ;
85+ inputElem . value = "" ;
86+ }
87+ 88+ const checkIsEmpty = ( ) => {
89+ console . log ( isEmpty ( ) ) ;
90+ }
91+ 92+ enQueue ( 5 ) ;
93+ enQueue ( 10 ) ;
94+ enQueue ( 8 ) ;
95+ display ( ) ;
96+ 97+ </ script >
98+ </ body >
99+ 100+ </ html >
0 commit comments