How to Implement a Queue in C

  Рет қаралды 61,058

Jacob Sorber

Jacob Sorber

Күн бұрын

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
How to implement a queue in C // Rounding out my collection of data structure videos, this one explores the queue, one of the simplest but most common data structures used in software development. This tutorial shows you how to implement a queue, using a linked list. You can also use an array, which I will pick up later in a future video.
Related videos:
Stacks - • How to Implement a Sta...
Linked List - • Understanding and impl...
New Course:
jacobsorber.thinkific.com/
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorber.com
people.cs.clemson.edu/~jsorber/
persist.cs.clemson.edu/
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
+ rep the channel with nerdy merch --- [teespring.com/stores/jacob-so...]
Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]

Пікірлер: 70
@y-yyy
@y-yyy Жыл бұрын
Thank you so much! Love your explanation of the code, very eloquent and clear.
@mikerowaveoven509
@mikerowaveoven509 3 жыл бұрын
Love the sound of the keyboard in the background :D
@viacheslav_sadko
@viacheslav_sadko 3 жыл бұрын
Really nice lesson, thank you so much!
@tastypie2276
@tastypie2276 2 жыл бұрын
You are helping me so much with studying. I hope I will reach the level when I'm able to create a soft that likes videos automatically. Then I will launch it and set it to like all of your videos!
@fallofmanbrand
@fallofmanbrand 4 жыл бұрын
Loved It!
@tails_the_god
@tails_the_god Жыл бұрын
Thanks! Great tutorial!
@marcelijankowski9593
@marcelijankowski9593 2 жыл бұрын
I Love Your explanations
@procyte2139
@procyte2139 4 жыл бұрын
Really interesting videos! I really like it. I think the next step-up for your channel would be to improved visuals like 2D animations. Then this would be a incredible channel.
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks, and I completely agree. It's always just an issue of how much time I can put into each video. Sometimes there's a little extra time for animation. Sometimes there's not.
@dusanmalusev9530
@dusanmalusev9530 4 жыл бұрын
Wow, such a good video, maybe you can do a follow up on thread safe queue, i wish i have teachers like you in my university
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks, Dusan. I'll keep it on the list. In the meantime, I hit close to that topic in my thread safety video (the bacon one) and in one of my multithreaded webserver videos.
@furball_vixie
@furball_vixie 4 жыл бұрын
i love these videos, will you be doing more data structures?
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks. Yes, I've got a few more on the plan for the coming weeks/months. Hoping to get to trees and queues, and hopefully some fancier structures after that.
@karimkohel3240
@karimkohel3240 4 жыл бұрын
YES MORE DATA STRUCTERS
@GlobalYoung7
@GlobalYoung7 Жыл бұрын
thank you 👍
@atkuriajaykumar3701
@atkuriajaykumar3701 3 жыл бұрын
Jacob which videos I should watch to understand this better I understood theoretical part well.
@kubic-c3186
@kubic-c3186 2 жыл бұрын
you could instead use a memory pool instead of malloc, and then resize it every time you ran out of space(if enqueue is called more the dequeue)
@diegovidal451
@diegovidal451 Жыл бұрын
What keyboard are you using? My gosh it sounds really good!
@alexbright5140
@alexbright5140 Жыл бұрын
we definitely don't want our tail dangling there 😂 5:40 AMAZING CONTENTS
@notyetdecided
@notyetdecided 4 жыл бұрын
thanks
@karimkohel3240
@karimkohel3240 4 жыл бұрын
Patiently waiting for the graph video
@JacobSorber
@JacobSorber 4 жыл бұрын
I know. Thanks for your patience. Also, are you more interested in adjacency matrix or adjacency list-style graphs? I might have time to do both, but figured I would ask just in case.
@karimkohel3240
@karimkohel3240 4 жыл бұрын
@@JacobSorber i don't know what that means so it would be awesome if we started at that point XD
@HasanBasri-vf2kg
@HasanBasri-vf2kg 4 жыл бұрын
Dear teacher. Thanks a lot for your effort and videos. Can you take a video about semaphores, mutexes? Greetings from Turkey, İzmir👋
@enestastan7147
@enestastan7147 4 жыл бұрын
You can check this video of him: kzbin.info/www/bejne/b5Lbpmt5ituCgbs I don’t know if that covers all the things you are curious about.
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks, Enes.
@user-dl3rc2bb1r
@user-dl3rc2bb1r 8 ай бұрын
hi, I have a question, in this specific scenario, can we replace 'q->tail = newNode' (line 36) with 'q->tail = q->tail->next' as an alternative? (because we've already assigned q->tail->next to newNode)
@janruui5506
@janruui5506 4 жыл бұрын
You are the best thank you very much I love your Videos👍👍👍👍👍👍👍👍👍 Can you make a Video and explaine this kind of code in C: int (*ptr) (int, int) = &function; And this int *ptr[10]; int (*ptrr) [10]; We had this in the lecture but I don't understand for what we use this
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks. You might want to check out my videos on function pointers, double pointers, and the relationship between pointers and arrays. Might help here.
@janruui5506
@janruui5506 4 жыл бұрын
@@JacobSorber thank you👍
@csbnikhil
@csbnikhil 4 жыл бұрын
​@@janruui5506 You can check out Demystifying Pointers - Function Pointers, by @theteachr for an animated version.
@opensourcemundo3415
@opensourcemundo3415 3 жыл бұрын
Amazing video, I just don't get one thing: why do we save the head in a tmp to obtain the result? Wouldn't it be the same if I just did "int result = q->head->value" ?
@JacobSorber
@JacobSorber 3 жыл бұрын
Thanks. Yes, you can definitely do that. Saving the pointer is just so you can free it, after you remove it from the queue. Whether you get the result from the temporary pointer or from q->head, doesn't matter.
@windowsbuilderthegreat3121
@windowsbuilderthegreat3121 7 ай бұрын
When looking at your enqueue function, it looks like it can cause a memory leak. wouldn’t it be better to dereference the value into that in the node and then free the pointer after finishing with it?
@chimichanga7033
@chimichanga7033 3 жыл бұрын
So we are not technically removing a number from a queue its still there just moving the pointer head to the next one and printing the number on the screen, right?
@cassiewinger7041
@cassiewinger7041 4 жыл бұрын
awesome videos, as always thanks can you make a tutorial on how linkers work? and/or introduce some references/documents or whatever? thank you so much
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks. Great suggestion. Do you have a specific linker question, because it's a pretty significant topic.
@cassiewinger7041
@cassiewinger7041 4 жыл бұрын
​@@JacobSorber if you could explain the general structure/internals of object files and how linker combines object files, it'd be really great. 'cause i looked for tutorials and resources on these topics, didn't find any. make a few tutorials structure of object files and how linker combines object files. PLEASE. thanks
@cassiewinger7041
@cassiewinger7041 4 жыл бұрын
@@JacobSorber I don't generally know how linkers work myself. If you could just make one or two tutorials and point the way so I/we can learn linkers better, it'd really really great. THANKS
@JacobSorber
@JacobSorber 4 жыл бұрын
@@cassiewinger7041 Ok. I'll add it to the list and see what I can do.
@cassiewinger7041
@cassiewinger7041 4 жыл бұрын
@@JacobSorber thanks a lot. can't wait
@JosephKlem1
@JosephKlem1 2 жыл бұрын
This was super helpful, thanks, Jacob. One question: was QUEUE_EMPTY declared somewhere as an int constant? The dequeue function is supposed to return an int, so I wasn't sure how it could return something called QUEUE_EMPTY unless that somehow carried an int value. Can you explain? Thanks.
@matty2128
@matty2128 2 жыл бұрын
Most likely he set an int constant somewhere else that signifies an empty result. So early on he could have done something similar to define QUEUE_EMPTY -1. He could then have a return if statement that checks the result of what is being returned. That being said an int is still being returned.
@mariusel9916
@mariusel9916 Жыл бұрын
it was defined in the stack video as #define QUEUE_EMPTY INT_MIN... but for this you'll need
@ChocolateMilkCultLeader
@ChocolateMilkCultLeader 4 жыл бұрын
I wish I had this last year
@JacobSorber
@JacobSorber 4 жыл бұрын
Me too.
@MA-ek1xl
@MA-ek1xl 2 жыл бұрын
I wasn't able to get the code to work actually. could you post them in a link in the description?
@devilminty_fresh
@devilminty_fresh 4 жыл бұрын
Hey! Can you show us how you would implement a priority queue?
@cassiewinger7041
@cassiewinger7041 4 жыл бұрын
Priority queues are implemented using heaps (max heaps and min heaps). u gotta learn heaps first, just take a look at the wikipedia page, it'll take up to 10 mins good luck
@nassar_productions
@nassar_productions 3 жыл бұрын
Hello, if I created.a queue, the enqueue function has a malloc allocation, so I want to deallocate it before exiting using free(). But I can't seem to properly and successfully deallocate that? any suggestions?
@JacobSorber
@JacobSorber 3 жыл бұрын
There's not enough info here to know what's going on, but I would recommend freeing the memory (the same address you got from malloc-you can use your debugger to make sure you're doing that) after you dequeue the nodes from the list.
@nassar_productions
@nassar_productions 3 жыл бұрын
Yes, I figured out that freeing the head in dequeue function does the trick. Thanks !
@csbnikhil
@csbnikhil 4 жыл бұрын
The last few lines for enqueing are only useful for the first insertion. It's going to be wasteful until the list gets empty again. Is there a better way to achieve it?
@JacobSorber
@JacobSorber 4 жыл бұрын
You could combine the two if statement blocks. That might help a little. In the end, though, the cost of this check will be really small compared to the malloc call. So, I'm not sure whether it's worth worrying about.
@randomman5280
@randomman5280 4 жыл бұрын
Hello Jacob, i hope you are healthy and safe too. I am a student and I am working on a project in which I am integrating Python code in C / C ++. There is the Cython tool that I use to generate C code so far everything is fine. The problem: The generated .c file "ther is no .h" cannot be learned at all. maybe you know how to generate a library from .c files only and also how to use them? I thank you in advance. greetings from Syria Kamil Alsaleh Alkhalil P.S. I rediscovered your channel. it's pretty helpful.
@JacobSorber
@JacobSorber 4 жыл бұрын
Hi Kamil. Thanks and welcome. I'll put cython on the topic list for future videos. In the meantime, I do have a few videos about libraries. They might be able to help.
@swallace21
@swallace21 8 ай бұрын
great video, the typing is super loud thought, really hard to hear your voice when there is typing
@gauravshinde6306
@gauravshinde6306 4 жыл бұрын
Can u do a vid in vectors in cpp
@JacobSorber
@JacobSorber 4 жыл бұрын
Probably. Do you have a specific question about them, or just looking for a general overview of how they work and how to use them?
@gauravshinde6306
@gauravshinde6306 4 жыл бұрын
@@JacobSorber A general overview of them would be great as well as their applications. I know java linked lists look similar but I think everyone would like to know their applications
@HasanBasri-vf2kg
@HasanBasri-vf2kg 4 жыл бұрын
@@JacobSorber Sir I think in channel less videos about C++ than C. Can you take tutorial videos important topics in C++? Like as friend ask, why do we need STL? Are these things advantageous or not than using own your own data structures?
@franciscrypto9143
@franciscrypto9143 2 жыл бұрын
where is the source of code of this code?
@wocannaseilai5983
@wocannaseilai5983 4 жыл бұрын
Seco d
@ajmaln2387
@ajmaln2387 Жыл бұрын
ELEC 278 moment
@yusraazrademirel2195
@yusraazrademirel2195 Жыл бұрын
why cant i understand :(((
@AngryGamer-RC_Maniac
@AngryGamer-RC_Maniac 3 жыл бұрын
You could have made this video far more easier mate, but you choosed a complex way with multiple typedefs which led to unnecessary notations as '->'.
@JacobSorber
@JacobSorber 3 жыл бұрын
Sweet. I opened up an opportunity for you to make a simpler one. 😀
@robertcarmona5142
@robertcarmona5142 2 жыл бұрын
was about to follow along but all my professors have told me real programmers don't use Global variables.... really bad practice
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
Pity you didn't pay attention or stick around. NO(!) "Global" vars were used or abused in this video...
Fixed and Variable Length Arrays in C and C++
20:24
Jacob Sorber
Рет қаралды 43 М.
How to Implement a Stack in C (+ encapsulation)
15:38
Jacob Sorber
Рет қаралды 58 М.
Homemade Professional Spy Trick To Unlock A Phone 🔍
00:55
Crafty Champions
Рет қаралды 57 МЛН
I wish I could change THIS fast! 🤣
00:33
America's Got Talent
Рет қаралды 71 МЛН
Implementing a queue in C
13:18
CodeVault
Рет қаралды 21 М.
Understanding and implementing a Hash Table (in C)
24:54
Jacob Sorber
Рет қаралды 342 М.
Learn Queue data structures in 10 minutes 🎟️
10:07
Bro Code
Рет қаралды 106 М.
How to Implement a Tree in C
14:39
Jacob Sorber
Рет қаралды 91 М.
Make your Data Type more Abstract with Opaque Types in C
13:41
Jacob Sorber
Рет қаралды 48 М.
The Trie Data Structure (Prefix Tree)
21:07
Jacob Sorber
Рет қаралды 73 М.
How to make memory read-only in your C programs.
12:57
Jacob Sorber
Рет қаралды 19 М.
Uncle Bob’s SOLID Principles Made Easy 🍀 - In Python!
19:09
ArjanCodes
Рет қаралды 285 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2 МЛН
Homemade Professional Spy Trick To Unlock A Phone 🔍
00:55
Crafty Champions
Рет қаралды 57 МЛН