Queue Data Structure & Operations (Linked List Based) | C Programming Example

  Рет қаралды 19,451

Portfolio Courses

Portfolio Courses

Күн бұрын

How to implement a queue data structure in C using a linked list as the underlying data structure, including a library of functions that implement the operations enqueue, dequeue, peek, size and is empty.
Source code: github.com/portfoliocourses/c....
See the Wikipedia article on Queue data structures: en.wikipedia.org/wiki/Queue_(....
Also see these videos on concepts used in this video...
Introduction To Pointers: • Introduction to Pointe...
Dynamic Memory Allocation: • Dynamic Memory Allocat...
Struct Basics: • struct Basics | C Prog...
Typedef Basics: • typedef Basics | C Pro...
Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 34
@lawniczakjohn
@lawniczakjohn Жыл бұрын
Oooh baby! DS&A time. Anyone discovering this channel for the first time, it’s a great resource.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thanks John! :-) Lately I've wanted to make more DS&A videos, I'm thinking of doing some in C++ next...
@dimitrioskalfakis
@dimitrioskalfakis Жыл бұрын
well thought-out narrative with a comprehensive and clear presentation.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's excellent to hear you found it to be comprehensive and clear, thank you for the kind feedback! :-)
@martinnyagah4313
@martinnyagah4313 Жыл бұрын
I appreciate the effort you put to teach us! Thank you sir!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Martin! :-)
@Neberdine
@Neberdine Жыл бұрын
You are the best. Thanks for all the help you provide.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome, and thank for the kind words! :-)
@fbi873
@fbi873 Жыл бұрын
This is an amazing video. Thanks for your efforts.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed, and you're welcome! :-)
@yigitcoban9823
@yigitcoban9823 Жыл бұрын
I appreciate the coding sources, If ı miss something which is important, ı can get it from the source code's subtitle. Thank you Mr. Kevin.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@Matheusk0
@Matheusk0 9 ай бұрын
Hey, I love your tutorials, thanks a lot for you work, pointers and data structures haven been a bit difficult for me to learn but your explanations always help me understand them with ease. I have a question: At the beginnning when you were defining the Node struct, you defined " typedef struct Node " and then typed "Node" again after the closing curly bracket. Why was the first "Node" needed? Since right after at the Queue struct definition you only set "Queue" once, after the closing curly bracket. Also, inside the Node struct definition, you used " struct Node *next; " to define the pointer to the next value in the list, and I was wondering if the "struct" word was needed for that, since you already had defined an alias for that struct. Couldn't that line have been just " Node *next; "? Or can we not use aliases when working with pointers? Thanks a lot again!
@saad-jad
@saad-jad Жыл бұрын
Inside the while loop in the destroy_queue function, you created a temp variable which I think is unnecessary, because currentNode is enough to traverse. Consider this code and correct me if I'm wrong. while (currentNode !=NULL){ currentNode=currentNode->next; free(queue->head); queue->head=currentNode; } // end of while I know the difference may not be noticeable, but I think less variables are better. Love your vids! Have a blessed day.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
No currentNode is not enough to traverse the list while also free-ing the current node. Here you're using queue->head in a bit of a similar way to the "temp variable", which is OK too. :-)
@wuvorwenda6733
@wuvorwenda6733 Жыл бұрын
I learned a lot thank you.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you learned a lot, and you're welcome! :-)
@beholdenspore28
@beholdenspore28 5 ай бұрын
returning null from qequeue() causes a warning with clang (idk about gcc). to fix it, just return 0 instead.
@Brad_Script
@Brad_Script Ай бұрын
that's because it's supposed to return an int not a pointer, although the returned value could be anything because the operation failed.
@theayushagrawal2550
@theayushagrawal2550 5 ай бұрын
Can you please create a video on implementation of queue using array (or stack)
@anoshiravan6809
@anoshiravan6809 11 ай бұрын
thank you
@laputa4825
@laputa4825 Жыл бұрын
Hello I have a question, I want to access structure variables but I have to do it through functions with a "custom" ADT. GET functions will return the requested variable, while SET functions will require the variable being changed and the new value as parameters. While there is a condition that direct access to the structure variables is not allowed outside of the ADT implementation. How would I approach this? I am very lost. Is there any videos that can help with understanding this?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
The only approach I know of to "hiding access to the structure members" is to have functions as part of a library which provide and work with void pointers, while using a struct internally. e.g. what is discussed here: stackoverflow.com/a/2672067. Unfortunately I'm unaware of a good resource to learn about how to do that, and I don't have a video on that.
@anime--A
@anime--A 4 ай бұрын
which compiler iis that
@ivanraulsanchezdiaz7499
@ivanraulsanchezdiaz7499 Жыл бұрын
What happens if I don't use malloc to create the queue?
@ivanraulsanchezdiaz7499
@ivanraulsanchezdiaz7499 Жыл бұрын
Queue create_queue() { Queue queue; queue.head = NULL; queue.tail = NULL; queue.size = 0; return queue; } the queue only needs the space of two pointers and the integer that indicates the size, right?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Good question Ivan! :-) If you don’t use malloc then the Queue will be in a place in memory called the stack rather than the heap. When you pass the Queue around or return it the Queue is being copied each time essentially, with malloc there is one copy of the Queue struct on the heap, and we just pass around and copy a pointer value to it. Either way works really!
@ivanraulsanchezdiaz7499
@ivanraulsanchezdiaz7499 Жыл бұрын
@@PortfolioCourses ohh i see, in some cases it's cheaper just to return the pointer, thanks a lot for you reply!
@jjfan4014
@jjfan4014 5 ай бұрын
In enqueue, queue->tail->next = newNode; this should be wrong because tail points to NULL at that time. It should be a segmentation fault.
@PortfolioCourses
@PortfolioCourses 5 ай бұрын
We check if the queue is empty before accessing the tail. If the queue is not empty, there is a tail, and we can access next. And if the queue is empty and we create a new node, then note how both head and tail point to that same node. if (is_empty(queue)) { queue->head = newNode; queue->tail = newNode; } else { queue->tail->next = newNode; queue->tail = newNode; }
@jjfan4014
@jjfan4014 5 ай бұрын
Thank you@@PortfolioCourses
@PortfolioCourses
@PortfolioCourses 5 ай бұрын
@@jjfan4014 You're welcome! 🙂
@PortfolioCourses
@PortfolioCourses Жыл бұрын
If you liked this video you might like learning more about Linked Lists! :-) I've made this "lowest possible price" coupon course for the Udemy Linked List course that expires on March 26th 11:50pm PST: www.udemy.com/course/linked-lists-with-c/?couponCode=5DAYDISCOUNT.
@thesmug2750
@thesmug2750 7 ай бұрын
Why do you talk like that? “Kudidastructure” 🤣
struct Basics | C Programming Tutorial
24:44
Portfolio Courses
Рет қаралды 134 М.
Secret Experiment Toothpaste Pt.4 😱 #shorts
00:35
Mr DegrEE
Рет қаралды 37 МЛН
Каха заблудился в горах
00:57
К-Media
Рет қаралды 10 МЛН
Queue Data Structure In STL | C++ Tutorial
18:44
Portfolio Courses
Рет қаралды 12 М.
Understanding and implementing a Linked List in C and Java
18:15
Jacob Sorber
Рет қаралды 234 М.
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 58 М.
every good programmer should know how to code this data structure (its easy)
21:08
10 FORBIDDEN Sorting Algorithms
9:41
Ardens
Рет қаралды 830 М.
Quicksort Algorithm Implementation | C Programming Example
20:37
Portfolio Courses
Рет қаралды 57 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 293 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 275 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Linked Lists for Technical Interviews - Full Course
1:27:24
freeCodeCamp.org
Рет қаралды 351 М.
Secret Experiment Toothpaste Pt.4 😱 #shorts
00:35
Mr DegrEE
Рет қаралды 37 МЛН