-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.html
More file actions
1296 lines (933 loc) · 59.9 KB
/
deque.html
File metadata and controls
1296 lines (933 loc) · 59.9 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dequeue</title>
<!-- Google tag (gtag.js) -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-HCERBDV76D"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-HCERBDV76D');
</script>
<meta content="B0487B46A104E90209E8A3BEA24ECA0E" name="msvalidate.01"/>
<meta content="f044b3a12c7918f1" name="yandex-verification"/>
<!--end-->
<meta content="learnPython" name="author"/>
<meta content="Learn Python for free,learn python for beginners,Core Python,Web frameworks,Multiprocess architecture,Serverside templating language,python tutorials,python4" name="keywords"/>
<meta content="website" property="og:type"/>
<meta content="learn about data types, variables, lists, tuples, dictionaries,if else,DSA,loops,user-defined functions, oop, threading and scripting." name="description"/>
<meta content="US-CA" name="”geo.region”"/>
<meta content="353 Jane Stanford Way, Stanford, CA 94305, United States" name="”geo.placename”"/>
<meta content="37.430089898615456;-122.17332683124829" name="”geo.position”"/>
<meta content="37.430089898615456, -122.17332683124829" name="”ICBM”"/>
<link href="https://pythonread.github.io/?m=1" rel="alternate"/>
<link href="/favicon.png" rel="icon"/>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="/style1.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style2.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style0.css" media="all" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="nav-wrapper">
<div class="container">
<nav>
<div class="logo wave">
<a href="/" id="logo">
Python Tutorial
</a>
</div>
<div class="nav-toggle-icon" id="nav-toggle-icon" onclick="mobileMenu()">
<div class="material-hamburger">
<span>
</span>
<span>
</span>
<span>
</span>
</div>
</div>
<div class="menu-wrapper" id="menu-wrapper">
<div class="nav-indicator">
</div>
<ul class="menus">
<li>
<a class="wave" href="/">
Home
</a>
</li>
<li>
<a class="wave" href="/projects.html">
Projects
</a>
</li>
<li>
<a class="wave" href="/free-course.html" target="_blank">
Free Course
</a>
</li>
<li>
<a class="wave" href="/dsa.html">
DSA
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="contents contents--neg" style="margin-top: 60px">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="d-flex">
<div class="left-bar d-none d-lg-block">
<div class="card-alt mb-10x">
<h3>Page Index</h3>
<div class="list">
<ul>
<li><a href="/dsa.html#data-structure-1" title="Data Structures (I)">Data Structures
(I)</a></li>
<li><a href="/dsa.html#data-structure-2" title="Data Structures (II)">Data Structures
(II)</a></li>
<li><a href="/dsa.html#tree-1" title="Tree based DSA (I)">Tree based DSA (I)</a></li>
<li><a href="/dsa.html#tree-2" title="Tree based DSA (II)">Tree based DSA (II)</a></li>
<li><a href="/dsa.html#graph" title="Graph Data Structures and Algorithm">Graph based
DSA</a></li>
<li><a href="/dsa.html#sorting-searching" title="Sorting and Searching">Sorting and
Searching</a></li>
<li><a href="/dsa.html#greedy-algorithm" title="Greedy Algorithms">Greedy Algorithms</a>
</li>
<li><a href="/dsa.html#dynamic-programming" title="Dynamic Programming">Dynamic
Programming</a></li>
<li><a href="/dsa.html#other-algorithms" title="Other Algorithms">Other Algorithms</a>
</li>
</ul>
</div>
</div>
</div>
<div class="right-bar">
<!--afirst part end-------------------------------------------->
<iframe width="560" height="315" src="https://www.youtube.com/embed/jnXpHLacLTs" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div class="editor-contents">
<h1>Deque Data Structure</h1>
<p class="editor-contents__short-description">In this tutorial, you will learn what a double ended queue (deque) is. Also, you will find working examples of different operations on a deque in C, C++, Java and Python.</p>
<div id="node-1668" class="node node-algorithm clearfix" about="/dsa/deque" typeof="sioc:Item foaf:Document">
<span property="dc:title" content="Deque Data Structure" class="rdf-meta element-hidden"></span>
<div class="content">
<p id="definition">Deque or Double Ended Queue is a type of <a href="/dsa/queue.html">queue</a> in which insertion and removal of elements can either be performed from the front or the rear. Thus, it does not follow FIFO rule (First In First Out).</p>
<figure><img alt="representation of deque data structure" src="//cdn.programiz.com/sites/tutorial2program/files/deque.png" title="Double ended queue" width="637" height="102"><figcaption>Representation of Deque</figcaption></figure><hr><h2 id="types">Types of Deque</h2>
<ul><li><strong>Input Restricted Deque</strong><br>
In this deque, input is restricted at a single end but allows deletion at both the ends.</li>
<li><strong>Output Restricted Deque</strong><br>
In this deque, output is restricted at a single end but allows insertion at both the ends.</li>
</ul><hr><h2 id="operations">Operations on a Deque</h2>
<p>Below is <a href="/dsa/circular-queue.html">the circular array</a> implementation of deque. In a circular array, if the array is full, we start from the beginning.</p>
<p>But in a linear array implementation, if the array is full, no more elements can be inserted. In each of the operations below, if the array is full, "overflow message" is thrown.</p>
<p>Before performing the following operations, these steps are followed.</p>
<ol><li>Take an array (deque) of size <var>n</var>.</li>
<li>Set two pointers at the first position and set <code>front = -1</code> and <code>rear = 0</code>.</li>
</ol><figure><img alt="initialize an array and pointers for deque operations" src="//cdn.programiz.com/sites/tutorial2program/files/deque-array.png" title="Initialize array" width="383" height="191"><figcaption>Initialize an array and pointers for deque</figcaption></figure><h3>1. Insert at the Front</h3>
<p>This operation adds an element at the front.</p>
<ol><li>Check the position of front.
<figure><img alt="check the position of front" src="//cdn.programiz.com/sites/tutorial2program/files/deque-insert-front-1.png" title="Deque - insert at the front" width="377" height="167"><figcaption>Check the position of front</figcaption></figure></li>
<li>If <code>front < 1</code>, reinitialize <code>front = n-1</code> (last index).
<figure><img alt="if front is less than 1 shift front to the end" src="//cdn.programiz.com/sites/tutorial2program/files/deque-insert-front-2.png" title="Deque - insert at the front" width="377" height="167"><figcaption>Shift front to the end</figcaption></figure></li>
<li>Else, decrease <var>front</var> by 1.</li>
<li>Add the new key <var>5</var> into <code>array[front]</code>.
<figure><img alt="Insert element at the position of front" src="//cdn.programiz.com/sites/tutorial2program/files/deque-insert-front-3.png" title="Deque - insert at the front" width="377" height="167"><figcaption>Insert the element at Front</figcaption></figure></li>
</ol><h3>2. Insert at the Rear</h3>
<p>This operation adds an element to the rear.</p>
<ol><li>Check if the array is full.
<figure><img alt="check if deque is full" src="//cdn.programiz.com/sites/tutorial2program/files/deque-insert-rear-1.png" title="Deque - insert at the rear" width="377" height="167"><figcaption>Check if deque is full</figcaption></figure></li>
<li>If the deque is full, reinitialize <code>rear = 0</code>.</li>
<li>Else, increase <var>rear</var> by 1.
<figure><img alt="if deque is not full increase rear" src="//cdn.programiz.com/sites/tutorial2program/files/deque-insert-rear-2.png" title="Deque - insert at the rear" width="377" height="167"><figcaption>Increase the rear</figcaption></figure></li>
<li>Add the new key <var>5</var> into <code>array[rear]</code>.
<figure><img alt="insert element at the rear" src="//cdn.programiz.com/sites/tutorial2program/files/deque-insert-rear-3.png" title="Deque - insert at the rear" width="377" height="167"><figcaption>Insert the element at rear</figcaption></figure></li>
</ol><h3>3. Delete from the Front</h3>
<p></p><div class="clearfix"></div><p>The operation deletes an element from the front.</p>
<ol><li>Check if the deque is empty.
<figure><img alt="check if deque is empty" src="//cdn.programiz.com/sites/tutorial2program/files/deque-delete-front-1.png" title="Deque - delete from the front" width="377" height="167"><figcaption>Check if deque is empty</figcaption></figure></li>
<li>If the deque is empty (i.e. <code>front = -1</code>), deletion cannot be performed (<strong>underflow condition</strong>).</li>
<li>If the deque has only one element (i.e. <code>front = rear</code>), set <code>front = -1</code> and <code>rear = -1</code>.</li>
<li>Else if <var>front</var> is at the end (i.e. <code>front = n - 1</code>), set go to the front <code>front = 0</code>.</li>
<li>Else, <code>front = front + 1</code>.
<figure><img alt="increase the index of front" src="//cdn.programiz.com/sites/tutorial2program/files/deque-delete-front-2.png" title="Deque - delete from the front" width="377" height="167"><figcaption>Increase the front</figcaption></figure></li>
</ol><h3>4. Delete from the Rear</h3>
<p>This operation deletes an element from the rear.</p>
<ol><li>Check if the deque is empty.
<figure><img alt="check if deque is empty" src="//cdn.programiz.com/sites/tutorial2program/files/deque-delete-rear-1.png" title="Deque - delete from the rear" width="377" height="167"><figcaption>Check if deque is empty</figcaption></figure></li>
<li>If the deque is empty (i.e. <code>front = -1</code>), deletion cannot be performed (<strong>underflow condition</strong>).</li>
<li>If the deque has only one element (i.e. <code>front = rear</code>), set <code>front = -1</code> and <code>rear = -1</code>, else follow the steps below.</li>
<li>If <var>rear</var> is at the front (i.e. <code>rear = 0</code>), set go to the front <code>rear = n - 1</code>.</li>
<li>Else, <code>rear = rear - 1</code>.
<figure><img alt="decrease the position of rear" src="//cdn.programiz.com/sites/tutorial2program/files/deque-delete-rear-2.png" title="Deque - delete from the rear" width="377" height="167"><figcaption>Decrease the rear</figcaption></figure></li>
</ol><h3>5. Check Empty</h3>
<p>This operation checks if the deque is empty. If <code>front = -1</code>, the deque is empty.</p>
<h3>6. Check Full</h3>
<p>This operation checks if the deque is full. If <code>front = 0</code> and <code>rear = n - 1</code> OR <code>front = rear + 1</code>, the deque is full.</p>
<hr><h2 id="code">Deque Implementation in Python, Java, C, and C++</h2>
<div class="tabbed-editor">
<div id='py1'class="tabbed-editor__node tabbed-editor__node--active" onclick="changepy()"><a href="#python-code">Python</a></div>
<div id="java1" class="tabbed-editor__node" onclick="changejava()"><a href="#java-code">Java</a></div>
<div id="c1" class="tabbed-editor__node" onclick="changec()"><a href="#c-code">C</a></div>
<div id="cpp1" class="tabbed-editor__node" onclick="changecpp()"><a href="#cpp-code">C++</a></div>
</div>
<div class="code-editor code-editor--tabbed">
<div class="code-editor__area code-editor__area--active" id="python-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="python hljs"><span class="hljs-comment"># Deque implementaion in python</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Deque</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
self.items = []
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">isEmpty</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> self.items == []
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">addRear</span><span class="hljs-params">(self, item)</span>:</span>
self.items.append(item)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">addFront</span><span class="hljs-params">(self, item)</span>:</span>
self.items.insert(<span class="hljs-number">0</span>, item)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">removeFront</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> self.items.pop(<span class="hljs-number">0</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">removeRear</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> self.items.pop()
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">size</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> len(self.items)
d = Deque()
<span class="hljs-keyword">print</span>(d.isEmpty())
d.addRear(<span class="hljs-number">8</span>)
d.addRear(<span class="hljs-number">5</span>)
d.addFront(<span class="hljs-number">7</span>)
d.addFront(<span class="hljs-number">10</span>)
<span class="hljs-keyword">print</span>(d.size())
<span class="hljs-keyword">print</span>(d.isEmpty())
d.addRear(<span class="hljs-number">11</span>)
<span class="hljs-keyword">print</span>(d.removeRear())
<span class="hljs-keyword">print</span>(d.removeFront())
d.addFront(<span class="hljs-number">55</span>)
d.addRear(<span class="hljs-number">45</span>)
<span class="hljs-keyword">print</span>(d.items)</code></pre></div>
</div>
<div class="code-editor__area" id="java-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="java hljs"><span class="hljs-comment">// Deque implementation in Java</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Deque</span> </span>{
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> MAX = <span class="hljs-number">100</span>;
<span class="hljs-keyword">int</span> arr[];
<span class="hljs-keyword">int</span> front;
<span class="hljs-keyword">int</span> rear;
<span class="hljs-keyword">int</span> size;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Deque</span><span class="hljs-params">(<span class="hljs-keyword">int</span> size)</span> </span>{
arr = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[MAX];
front = -<span class="hljs-number">1</span>;
rear = <span class="hljs-number">0</span>;
<span class="hljs-keyword">this</span>.size = size;
}
<span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">isFull</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> ((front == <span class="hljs-number">0</span> && rear == size - <span class="hljs-number">1</span>) || front == rear + <span class="hljs-number">1</span>);
}
<span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">isEmpty</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> (front == -<span class="hljs-number">1</span>);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertfront</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-keyword">if</span> (isFull()) {
System.out.println(<span class="hljs-string">"Overflow"</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (front == -<span class="hljs-number">1</span>) {
front = <span class="hljs-number">0</span>;
rear = <span class="hljs-number">0</span>;
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (front == <span class="hljs-number">0</span>)
front = size - <span class="hljs-number">1</span>;
<span class="hljs-keyword">else</span>
front = front - <span class="hljs-number">1</span>;
arr[front] = key;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertrear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-keyword">if</span> (isFull()) {
System.out.println(<span class="hljs-string">" Overflow "</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (front == -<span class="hljs-number">1</span>) {
front = <span class="hljs-number">0</span>;
rear = <span class="hljs-number">0</span>;
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rear == size - <span class="hljs-number">1</span>)
rear = <span class="hljs-number">0</span>;
<span class="hljs-keyword">else</span>
rear = rear + <span class="hljs-number">1</span>;
arr[rear] = key;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deletefront</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">if</span> (isEmpty()) {
System.out.println(<span class="hljs-string">"Queue Underflow\n"</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Deque has only one element</span>
<span class="hljs-keyword">if</span> (front == rear) {
front = -<span class="hljs-number">1</span>;
rear = -<span class="hljs-number">1</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (front == size - <span class="hljs-number">1</span>)
front = <span class="hljs-number">0</span>;
<span class="hljs-keyword">else</span>
front = front + <span class="hljs-number">1</span>;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deleterear</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">if</span> (isEmpty()) {
System.out.println(<span class="hljs-string">" Underflow"</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (front == rear) {
front = -<span class="hljs-number">1</span>;
rear = -<span class="hljs-number">1</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rear == <span class="hljs-number">0</span>)
rear = size - <span class="hljs-number">1</span>;
<span class="hljs-keyword">else</span>
rear = rear - <span class="hljs-number">1</span>;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getFront</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">if</span> (isEmpty()) {
System.out.println(<span class="hljs-string">" Underflow"</span>);
<span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
}
<span class="hljs-keyword">return</span> arr[front];
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getRear</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">if</span> (isEmpty() || rear < <span class="hljs-number">0</span>) {
System.out.println(<span class="hljs-string">" Underflow\n"</span>);
<span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
}
<span class="hljs-keyword">return</span> arr[rear];
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
Deque dq = <span class="hljs-keyword">new</span> Deque(<span class="hljs-number">4</span>);
System.out.println(<span class="hljs-string">"Insert element at rear end : 12 "</span>);
dq.insertrear(<span class="hljs-number">12</span>);
System.out.println(<span class="hljs-string">"insert element at rear end : 14 "</span>);
dq.insertrear(<span class="hljs-number">14</span>);
System.out.println(<span class="hljs-string">"get rear element : "</span> + dq.getRear());
dq.deleterear();
System.out.println(<span class="hljs-string">"After delete rear element new rear become : "</span> + dq.getRear());
System.out.println(<span class="hljs-string">"inserting element at front end"</span>);
dq.insertfront(<span class="hljs-number">13</span>);
System.out.println(<span class="hljs-string">"get front element: "</span> + dq.getFront());
dq.deletefront();
System.out.println(<span class="hljs-string">"After delete front element new front become : "</span> + +dq.getFront());
}
}</code></pre></div>
</div>
<div class="code-editor__area" id="c-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="c hljs cpp"><span class="hljs-comment">// Deque implementation in C</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAX 10</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addFront</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span> *)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addRear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span> *)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">delFront</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span> *)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">delRear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span> *, <span class="hljs-keyword">int</span> *)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">count</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> arr[MAX];
<span class="hljs-keyword">int</span> front, rear, i, n;
front = rear = <span class="hljs-number">-1</span>;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < MAX; i++)
arr[i] = <span class="hljs-number">0</span>;
addRear(arr, <span class="hljs-number">5</span>, &front, &rear);
addFront(arr, <span class="hljs-number">12</span>, &front, &rear);
addRear(arr, <span class="hljs-number">11</span>, &front, &rear);
addFront(arr, <span class="hljs-number">5</span>, &front, &rear);
addRear(arr, <span class="hljs-number">6</span>, &front, &rear);
addFront(arr, <span class="hljs-number">8</span>, &front, &rear);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nElements in a deque: "</span>);
display(arr);
i = delFront(arr, &front, &rear);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nremoved item: %d"</span>, i);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nElements in a deque after deletion: "</span>);
display(arr);
addRear(arr, <span class="hljs-number">16</span>, &front, &rear);
addRear(arr, <span class="hljs-number">7</span>, &front, &rear);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nElements in a deque after addition: "</span>);
display(arr);
i = delRear(arr, &front, &rear);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nremoved item: %d"</span>, i);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nElements in a deque after deletion: "</span>);
display(arr);
n = count(arr);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nTotal number of elements in deque: %d"</span>, n);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addFront</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr, <span class="hljs-keyword">int</span> item, <span class="hljs-keyword">int</span> *pfront, <span class="hljs-keyword">int</span> *prear)</span> </span>{
<span class="hljs-keyword">int</span> i, k, c;
<span class="hljs-keyword">if</span> (*pfront == <span class="hljs-number">0</span> && *prear == MAX - <span class="hljs-number">1</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nDeque is full.\n"</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (*pfront == <span class="hljs-number">-1</span>) {
*pfront = *prear = <span class="hljs-number">0</span>;
arr[*pfront] = item;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (*prear != MAX - <span class="hljs-number">1</span>) {
c = count(arr);
k = *prear + <span class="hljs-number">1</span>;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">1</span>; i <= c; i++) {
arr[k] = arr[k - <span class="hljs-number">1</span>];
k--;
}
arr[k] = item;
*pfront = k;
(*prear)++;
} <span class="hljs-keyword">else</span> {
(*pfront)--;
arr[*pfront] = item;
}
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addRear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr, <span class="hljs-keyword">int</span> item, <span class="hljs-keyword">int</span> *pfront, <span class="hljs-keyword">int</span> *prear)</span> </span>{
<span class="hljs-keyword">int</span> i, k;
<span class="hljs-keyword">if</span> (*pfront == <span class="hljs-number">0</span> && *prear == MAX - <span class="hljs-number">1</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nDeque is full.\n"</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (*pfront == <span class="hljs-number">-1</span>) {
*prear = *pfront = <span class="hljs-number">0</span>;
arr[*prear] = item;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (*prear == MAX - <span class="hljs-number">1</span>) {
k = *pfront - <span class="hljs-number">1</span>;
<span class="hljs-keyword">for</span> (i = *pfront - <span class="hljs-number">1</span>; i < *prear; i++) {
k = i;
<span class="hljs-keyword">if</span> (k == MAX - <span class="hljs-number">1</span>)
arr[k] = <span class="hljs-number">0</span>;
<span class="hljs-keyword">else</span>
arr[k] = arr[i + <span class="hljs-number">1</span>];
}
(*prear)--;
(*pfront)--;
}
(*prear)++;
arr[*prear] = item;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">delFront</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr, <span class="hljs-keyword">int</span> *pfront, <span class="hljs-keyword">int</span> *prear)</span> </span>{
<span class="hljs-keyword">int</span> item;
<span class="hljs-keyword">if</span> (*pfront == <span class="hljs-number">-1</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nDeque is empty.\n"</span>);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
item = arr[*pfront];
arr[*pfront] = <span class="hljs-number">0</span>;
<span class="hljs-keyword">if</span> (*pfront == *prear)
*pfront = *prear = <span class="hljs-number">-1</span>;
<span class="hljs-keyword">else</span>
(*pfront)++;
<span class="hljs-keyword">return</span> item;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">delRear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr, <span class="hljs-keyword">int</span> *pfront, <span class="hljs-keyword">int</span> *prear)</span> </span>{
<span class="hljs-keyword">int</span> item;
<span class="hljs-keyword">if</span> (*pfront == <span class="hljs-number">-1</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nDeque is empty.\n"</span>);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
item = arr[*prear];
arr[*prear] = <span class="hljs-number">0</span>;
(*prear)--;
<span class="hljs-keyword">if</span> (*prear == <span class="hljs-number">-1</span>)
*pfront = <span class="hljs-number">-1</span>;
<span class="hljs-keyword">return</span> item;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">display</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr)</span> </span>{
<span class="hljs-keyword">int</span> i;
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n front: "</span>);
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < MAX; i++)
<span class="hljs-built_in">printf</span>(<span class="hljs-string">" %d"</span>, arr[i]);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">" :rear"</span>);
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">count</span><span class="hljs-params">(<span class="hljs-keyword">int</span> *arr)</span> </span>{
<span class="hljs-keyword">int</span> c = <span class="hljs-number">0</span>, i;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < MAX; i++) {
<span class="hljs-keyword">if</span> (arr[i] != <span class="hljs-number">0</span>)
c++;
}
<span class="hljs-keyword">return</span> c;
}</code></pre></div>
</div>
<div class="code-editor__area" id="cpp-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="cpp hljs"><span class="hljs-comment">// Deque implementation in C++</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAX 10</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Deque</span> {</span>
<span class="hljs-keyword">int</span> arr[MAX];
<span class="hljs-keyword">int</span> front;
<span class="hljs-keyword">int</span> rear;
<span class="hljs-keyword">int</span> size;
<span class="hljs-keyword">public</span>:
Deque(<span class="hljs-keyword">int</span> size) {
front = <span class="hljs-number">-1</span>;
rear = <span class="hljs-number">0</span>;
<span class="hljs-keyword">this</span>->size = size;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertfront</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertrear</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deletefront</span><span class="hljs-params">()</span></span>;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">deleterear</span><span class="hljs-params">()</span></span>;
<span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">isFull</span><span class="hljs-params">()</span></span>;
<span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">isEmpty</span><span class="hljs-params">()</span></span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getFront</span><span class="hljs-params">()</span></span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getRear</span><span class="hljs-params">()</span></span>;
};
<span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">Deque::isFull</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> ((front == <span class="hljs-number">0</span> && rear == size - <span class="hljs-number">1</span>) ||
front == rear + <span class="hljs-number">1</span>);
}
<span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">Deque::isEmpty</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> (front == <span class="hljs-number">-1</span>);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Deque::insertfront</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-keyword">if</span> (isFull()) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Overflow\n"</span>
<< <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (front == <span class="hljs-number">-1</span>) {
front = <span class="hljs-number">0</span>;
rear = <span class="hljs-number">0</span>;
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (front == <span class="hljs-number">0</span>)
front = size - <span class="hljs-number">1</span>;
<span class="hljs-keyword">else</span>
front = front - <span class="hljs-number">1</span>;
arr[front] = key;
}
<span class="hljs-keyword">void</span> Deque ::insertrear(<span class="hljs-keyword">int</span> key) {
<span class="hljs-keyword">if</span> (isFull()) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">" Overflow\n "</span> << <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (front == <span class="hljs-number">-1</span>) {
front = <span class="hljs-number">0</span>;
rear = <span class="hljs-number">0</span>;
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rear == size - <span class="hljs-number">1</span>)
rear = <span class="hljs-number">0</span>;
<span class="hljs-keyword">else</span>
rear = rear + <span class="hljs-number">1</span>;
arr[rear] = key;
}
<span class="hljs-keyword">void</span> Deque ::deletefront() {
<span class="hljs-keyword">if</span> (isEmpty()) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Queue Underflow\n"</span>
<< <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (front == rear) {
front = <span class="hljs-number">-1</span>;
rear = <span class="hljs-number">-1</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (front == size - <span class="hljs-number">1</span>)
front = <span class="hljs-number">0</span>;
<span class="hljs-keyword">else</span>
front = front + <span class="hljs-number">1</span>;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Deque::deleterear</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">if</span> (isEmpty()) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">" Underflow\n"</span>
<< <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (front == rear) {
front = <span class="hljs-number">-1</span>;
rear = <span class="hljs-number">-1</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rear == <span class="hljs-number">0</span>)
rear = size - <span class="hljs-number">1</span>;
<span class="hljs-keyword">else</span>
rear = rear - <span class="hljs-number">1</span>;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">Deque::getFront</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">if</span> (isEmpty()) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">" Underflow\n"</span>
<< <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
<span class="hljs-keyword">return</span> arr[front];
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">Deque::getRear</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">if</span> (isEmpty() || rear < <span class="hljs-number">0</span>) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">" Underflow\n"</span>
<< <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
}
<span class="hljs-keyword">return</span> arr[rear];
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-function">Deque <span class="hljs-title">dq</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span></span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"insert element at rear end \n"</span>;
dq.insertrear(<span class="hljs-number">5</span>);
dq.insertrear(<span class="hljs-number">11</span>);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"rear element: "</span>
<< dq.getRear() << <span class="hljs-built_in">endl</span>;
dq.deleterear();
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"after deletion of the rear element, the new rear element: "</span> << dq.getRear() << <span class="hljs-built_in">endl</span>;
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"insert element at front end \n"</span>;
dq.insertfront(<span class="hljs-number">8</span>);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"front element: "</span> << dq.getFront() << <span class="hljs-built_in">endl</span>;
dq.deletefront();
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"after deletion of front element new front element: "</span> << dq.getFront() << <span class="hljs-built_in">endl</span>;
}</code></pre></div>
</div>
</div>
<hr><h2 id="complexity">Time Complexity</h2>
<p>The time complexity of all the above operations is constant i.e. <code>O(1)</code>.</p>
<hr><h2 id="applications">Applications of Deque Data Structure</h2>
<ol><li>In undo operations on software.</li>
<li>To store history in browsers.</li>
<li>For implementing both <a href="/dsa/stack.html">stacks</a> and <a href="/dsa/queue.html">queues</a>.</li>
</ol></div>
</div>
<div class="tutorial-toc"><div class="tutorial-toc__inner"><h3 class="tutorial-toc__title">Table of Contents
<button class="btn btn--clear align-items-center">
<svg class="programiz-icon"><use xlink:href="/sites/all/themes/programiz/assets/feather-sprite.svg#x"></use></svg></button></h3><div class="tutorial-toc__links"><ul><li><a href="#definition">Definition</a></li>
<li><a href="#types">Types of Deque</a></li>
<li><a href="#operations">Operations on a Deque</a></li>
<li><a href="#code">Python, Java and C/C++ Examples</a></li>
<li><a href="#complexity">Deque Complexities</a></li>
<li><a href="#applications">Deque Applications</a></li>
</ul></div></div></div> </div>
<!--second------------------------------------------->
<div>
<ul>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/stack.html">
Stack</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/queue.html">
Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/types-of-queue.html">
Types of Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/circular-queue.html">
Circular Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/priority-queue.html">
Priority Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deque.html">
Deque</a></button>
<br/>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/linked-list.html">
Linked List</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/linked-list-operations.html">
Linked List Operations</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/linked-list-types.html">
Types of Linked List</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/hash-table.html">
Hash Table</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/heap-data-structure.html">
Heap Data Structure</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/fibonacci-heap.html">
Fibonacci Heap</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/decrease-key-and-delete-node-from-a-fibonacci-heap.html">
Decrease Key and Delete node from Fibonacci Heap</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/trees.html">
Tree Data Structure</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/tree-traversal.html">
Tree Traversal</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/binary-tree.html">
Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/full-binary-tree.html">
Full Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/perfect-binary-tree.html">
Perfect Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/complete-binary-tree.html">
Complete Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/balanced-binary-tree.html">
Balanced Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/binary-search-tree.html">
Binary Search Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/avl-tree.html">
AVL Tree</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/b-tree.html">
B Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-into-a-b-tree.html">
Insertion into B-tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deletion-from-a-b-tree.html">
Deletion from B-tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/b-plus-tree.html">
B+ Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-on-a-b-plus-tree.html">
Insertion on a B+ Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deletion-from-a-b-plus-tree.html">
Deletion from a B+ Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/red-black-tree.html">
Red Black Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-in-a-red-black-tree.html">
Insertion in Red Black Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deletion-from-a-red-black-tree.html">
Deletion from Red Black Tree</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph.html">
Graph Data Structure</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/spanning-tree-and-minimum-spanning-tree.html">
Spanning Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/strongly-connected-components.html">
Strongly Connected Components</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-adjacency-matrix.html">
Adjacency Matrix</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-adjacency-list.html">
Adjacency List</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-dfs.html">
DFS Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-bfs.html">
Breadth-first Search</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/bellman-ford-algorithm.html">
Bellman Ford's Algorithm</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/bubble-sort.html">
Bubble Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/selection-sort.html">
Selection Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-sort.html">
Insertion Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/merge-sort.html">