Queue Code
Related videos:
Queue intro: https://youtu.be/KxzhEQ-zpDc
Queue implementation: https://youtu.be/EoisnPvUkOA
Queue code: https://youtu.be/HV-hpvuGaC4
Data Structures Source Code:
https://github.com/williamfiset/algorithms
My website: http://www.williamfiset.com
What You'll Learn
Queue implementation in Java using a linked list, with methods for peek, poll, offer, and iteration, and a discussion on alternative implementations using static arrays.
Full Transcript
all right now it's time to have a look at some source code for Q so I implemented a Q and you can find the source code at the following link on github.com slm my username SL dat Dash structures also make sure you have watched and understood Parts One and Two from the Q series before continuing all right here we are at some source code for a queue so this source code is in the Java programming language although you can probably translate it into any programming language that you need so the first thing to to remark is I have an instance variable here of a linked list so this is Java's implementation of a doubly linked list we also use this in the stack implementation as you'll see the Q and the stack implementations are very very similar so here I have two Constructors one that'll create just an empty q and another optional one which will create a q but with a first element in fact I should probably check if this is null but we we might want to allow n elements so let's just leave it like that so the next method is the size it just gets the size of the link list and similarly this checks if the link list is empty those are both pretty self-explanatory okay next interesting method is the peak method the peak method uh Returns the element that's at the front of the que but it will throw an error if your Q is empty because you can't Peak anything when your Q is empty similarly for pole uh this will pull the element at the front of the que but unlike Peak will actually remove the element from the que so next we scroll down a little bit I have offer which adds an element to the back of the que I guess I am allowing for null elements so if you don't want null elements just put an if statement and throw an error or something so the pole removed from the front and you can see offer adds to the back so remove first and add last and the last method in here is an iterator in case you want to be able to iterate through all the elements in your Q very short and very simple implementation just under 50 lines of code you can create a Q although there are faster ways of creating cues especially with arrays the idea with arrays especially static arrays if you know the maximum amount of elements that will be in your queue at any given time then you can create an array of that maximum size and have pointers to the front and the back positions in your que and add and remove move elements based on the relative position of those pointers if you ever get to a position where you're running off the edge of your array then you can loop around to the front of the array and keep processing elements like that this is a lot faster than having to maintain references to the next node such as in a linked list so I guess I'll say that that's your homework is to create a static array based queue so guys thank you so much for watching and I will catch you in the next video
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 39 of 60
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
▶
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
🎓
Tutor Explanation
DeepCamp AI