Queue Code

WilliamFiset · Beginner ·⚡ Algorithms & Data Structures ·9y ago
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 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

This video implements a queue in Java using a linked list and discusses the benefits and drawbacks of this approach compared to using a static array. The code is available on GitHub and viewers are encouraged to implement their own array-based queue as homework.

Key Takeaways
  1. Create a new Java class for the queue implementation
  2. Implement the peek method to return the front element of the queue
  3. Implement the poll method to remove and return the front element of the queue
  4. Implement the offer method to add an element to the back of the queue
  5. Implement an iterator to traverse the elements of the queue
  6. Consider implementing an array-based queue as an alternative
💡 Array-based queues can be more efficient than linked list-based queues, especially when the maximum size of the queue is known in advance.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →