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

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

ความคิดเห็น • 40

  • @lawniczakjohn
    @lawniczakjohn ปีที่แล้ว +9

    Oooh baby! DS&A time.
    Anyone discovering this channel for the first time, it’s a great resource.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +2

      Thanks John! :-) Lately I've wanted to make more DS&A videos, I'm thinking of doing some in C++ next...

  • @PortfolioCourses
    @PortfolioCourses  ปีที่แล้ว +7

    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.

  • @dimitrioskalfakis
    @dimitrioskalfakis ปีที่แล้ว +6

    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 ปีที่แล้ว +8

    I appreciate the effort you put to teach us! Thank you sir!

  • @yigitcoban9823
    @yigitcoban9823 ปีที่แล้ว +2

    I appreciate the coding sources, If ı miss something which is important, ı can get it from the source code's subtitle. Thank you Mr. Kevin.

  • @Matheusk0
    @Matheusk0 ปีที่แล้ว +2

    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!

  • @beholdenspore28
    @beholdenspore28 9 หลายเดือนก่อน +3

    returning null from qequeue() causes a warning with clang (idk about gcc). to fix it, just return 0 instead.

    • @Brad_Script
      @Brad_Script 5 หลายเดือนก่อน +1

      that's because it's supposed to return an int not a pointer, although the returned value could be anything because the operation failed.

    • @paradoxicalcat7173
      @paradoxicalcat7173 2 หลายเดือนก่อน

      Really, that's nonsense as NULL and zero are both 0. Time programmers got a bit smarter. It's the human that gives data meaning; a computer doesn't care if 0 is 0, naught, NULL, or zero, if they have the same value and isn't the string literal '0' (decimal value 48).

    • @beholdenspore28
      @beholdenspore28 2 หลายเดือนก่อน +1

      @@paradoxicalcat7173 I am aware that NULL and 0 both technically represent the value zero. I was only providing a way to silence the warning. It was annoying me and I thought I would share what I did to fix it. Also, there is more to the story than what you're describing. NULL and 0 indeed do share the same value. The difference lies in the implication that NULL is the standard convention for describing the 0x0 memory address and 0 is used to describe an integer. I understand where you're coming from, however I think the distinction between the two constants is still important. Thank you for the response.

    • @Brad_Script
      @Brad_Script 2 หลายเดือนก่อน

      @@paradoxicalcat7173 NULL is 0 casted into a void pointer in C, a void pointer is not an int that's why the compiler gives a warning. NULL is 0 in C++

  • @Neberdine
    @Neberdine ปีที่แล้ว +1

    You are the best. Thanks for all the help you provide.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      You're welcome, and thank for the kind words! :-)

  • @theayushagrawal2550
    @theayushagrawal2550 9 หลายเดือนก่อน +1

    Can you please create a video on implementation of queue using array (or stack)

  • @saad-jad
    @saad-jad ปีที่แล้ว +3

    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  ปีที่แล้ว +1

      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. :-)

  • @fbi873
    @fbi873 ปีที่แล้ว +1

    This is an amazing video.
    Thanks for your efforts.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

      I'm glad you enjoyed, and you're welcome! :-)

  • @wuvorwenda6733
    @wuvorwenda6733 ปีที่แล้ว +2

    I learned a lot thank you.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      I'm glad you learned a lot, and you're welcome! :-)

  • @anime--A
    @anime--A 7 หลายเดือนก่อน

    which compiler iis that

  • @jjfan4014
    @jjfan4014 9 หลายเดือนก่อน

    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  9 หลายเดือนก่อน +2

      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 9 หลายเดือนก่อน

      Thank you@@PortfolioCourses

    • @PortfolioCourses
      @PortfolioCourses  9 หลายเดือนก่อน

      @@jjfan4014 You're welcome! 🙂

  • @ivanraulsanchezdiaz7499
    @ivanraulsanchezdiaz7499 ปีที่แล้ว +1

    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  ปีที่แล้ว +3

      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!

  • @anoshiravan6809
    @anoshiravan6809 ปีที่แล้ว

    thank you

  • @paradoxicalcat7173
    @paradoxicalcat7173 2 หลายเดือนก่อน

    Why not memset() to initialize the structure?
    memset(queue,0,sizeof(queue));
    This ensures all bits are zero.
    Don't tell me it is "unsafe". I know how to count! Many functions are only unsafe if you're stupid.

    • @paradoxicalcat7173
      @paradoxicalcat7173 2 หลายเดือนก่อน

      PS: Yes, I'm aware the compiler can optimize it away - the dumbest thing ever. This is why sensitive info gets left laying around in memory, because someone thought they were smart by removing code that zeroized memory AFTER it was used. Morons!

    • @paradoxicalcat7173
      @paradoxicalcat7173 2 หลายเดือนก่อน

      YT is censoring my posts again. Heaven forbid someone should have a strong opinion of idiots. Note I'm not referring to the video creator.

  • @thesmug2750
    @thesmug2750 11 หลายเดือนก่อน

    Why do you talk like that? “Kudidastructure” 🤣