Stacks and Queues

[ad_1]
Get the Code Here:

Welcome to my tutorial on Java Stacks and Queues. The data structures most are used to such as Arrays, linked lists, trees, etc. are best for data that represents real objects. Stacks and Queues are instead used to complete a task and are soon after discarded.

A major difference is that stacks and queues allow only a single item to be added or removed at a time. Stacks then provide access to the last item in, while queues provide access to the first item in.


Posted

in

by

Tags:

Comments

43 responses to “Stacks and Queues”

  1. Nikhil Avatar

    i have one doubt derek bans regarding the insertion operation that inside the insertion operation should we keep the if condition like this if(numberOfItems < queuesize) then we should go further i guess the condition if(numberOfItems +1 <= queueSize) is a burdern of an extra addition operation. Please correct me if i'm wrong.

  2. Nikhil Avatar

    one advice although a small one if you are taking input from the terminal using scanner use scanner object on nextLine() instead of next() method call as next will only pick the first word however we would want the entire line to be used and hence should use nextLine().

  3. Adam S Avatar

    There is a problem with your remove of the Queu. You need to move all element to the front of the array, otherwise you will end up with array overflow at some point.

  4. Wannabe Youtuberzz Ltd. Avatar

    Great Video, Subscribed 🙂

  5. Mohammed Hamouda Avatar

    I have a question? but before I ask it, please quys who click dislike, what are you, seriously what are you?
    now the question: in the remove method of the queue, when I type: front++; who will I access the indices that I left.
    what I mean, if the queue is 10 elements, when I remove an element, I will not be able to make them 10 again.

  6. William Darko Avatar

    Hey Derek your content is truly great, but can you try to be less abstract and make the lesson seem more organised than it is improved? That'll really help. Thanks.

  7. doctor1221a Avatar

    Derek, I believe that there is a bug with the insert method in TheQueue class. I was testing the class, and I'm getting an ArrayIndexOutOFBounds exception at the boundary condition when the underlying array has been "used up" . To fix, I replaced the insert method logic with …. if (numberofItems +1<= queueSize && queueSize!=front){

  8. KydroxHD Avatar

    You are teaching an entire generation of programmers. No matter if they have a piece of paper saying they can code or not. I'll owe my degree to you good sir! Compliments are simply insufficient for what you are doing.

  9. rohan shedge Avatar

    Thank you Derek , i think we can add generics to make it more complete

  10. Ankit Kannaujia Avatar

    displayTheStack() not implemented

  11. Vinay KR Avatar

    Well, what if I want the stack to have negative one as well? Not just as a way to represent empty value but as an actual value.

  12. Kaicheng Yan Avatar

    Thanks for your video, but I have a problem about pop() in stacks. When I pop the item from stacks, It still in stacks? But I think pop means remove the item and return it. Not only "not available or not be seen"

  13. lâm hoàng Avatar

    you shoud study slow

  14. lâm hoàng Avatar

    you study verry good , thank for my video >

  15. Ji Won Chong Avatar

    Hi. Could you tell us how you are typing some words so fast? Words such as queueArray, numberOfItems, etc.. Thanks.

  16. Red Bear Avatar

    Sat in 1+ hour lecture on stacks at college. Came here to actually learn it and in 15 minutes.

  17. David McCracken Avatar

    Thank you! I have a question, say i have an array size of 10, when i use them all up, and use the remove method, is it normal to make it -1? because should i then be able to insert another element? do queue data structures work like this?

    Thank you love the videos

  18. Jake Stokes Avatar

    Hi Derek, thanks for another great video!

    I'd like to point out something I noticed playing with this.

    Your loop in priorityInsert, as it stands "i = numberOfItems – 1" doesn't work if a prorityInsert is performed after a remove(), because the indexes get muddled.

    Changing it to "i = numberOfItems – 1 + front" seems to do the trick.

    Have I contributed? #FeelsGoodMan

  19. Adnan Saadeddin Avatar

    amazing man. 😀 I learn alot from you

  20. DjKilown Avatar

    any way to use text file to save the data in the queue ? most tut's do the pop push, peek on a stack and you (the user) never gets this… could you point me in the right direction …

    enter new info:
    remove any info:
    save file (info):
    close :

    that would be nice 🙂

  21. Eder Santos Avatar

    Dude !! you rock!!!!! best video period

  22. Duh Bruno Avatar

    Really nice content dude… keep doing the great job!

    Btw, nice reference to the Stephen King's master piece on the code… It was "shining" !

  23. Pia Nila Flor P. Ofalsa Avatar

    thank you so much for existing 🙂

  24. BHARAT KOTESWARAO Avatar

    Is there any site that gives solutions for problems in hackerrank or any such kind of problems ? plz give answer

  25. Uzias Lara Avatar

    This is sure to help in my Data Structures & Algorithms class. Thanks.

  26. Temps Avatar

    Isn't their a Stack<?> and Queue<?> class in java?

  27. Joanne Lee Avatar

    Your programming is amazingly proficient! I like all the algorithms videos.

  28. Joshua Escarilla Avatar

    Hi Sir Derek, I'm just stucked about this problem. Can I ask, what should be the output by the following:

    Refer methods to urvideo in queues class.

    public static void main(String[]args){
    Queues ac = new Queues(5);
    ac.insert("5");
    ac.insert("6");
    ac.insert("8");
    ac.remove();
    ac.remove();
    ac.remove();
    ac.insert("8");
    }

    Never mind for the indexes,
    a) | | | |8| |
    or
    b) |8| | | | |

    = I'm confused with these because when I run it using your code it outputs letter a above, and when I insert 2 values again, it will throw an exception ArrayIndexOutOfBoundsException. Try those codes on my main. Please help,

  29. Alex Carlson Avatar

    Forgive my ignorance as I am very new but in your pop method, the stack is displayed prior to the -1 assignment. This causes the display to show the value removed. I moved the <displayTheStack();> under the "-1" assignment, just above the return and now it displays the stack without the popped value. Am I correct in doing this or is there something I missed. Again, you rule!

    New version:
    public String pop(){

    if(topOfStack >= 0){

    System.out.println("POP " + stackArray[topOfStack] + " Was Removed From the Stackn");

    // Just like in memory an item isn't deleted, but instead is just not available

    stackArray[topOfStack] = "-1"; // Assigns -1 so the value won't appear

    displayTheStack();

    return stackArray[topOfStack–];
    }

  30. Alex Carlson Avatar

    I'm so glad you exist in the world. My instructors give me tasks but you teach me how to learn java. Thank you for doing what you do!

  31. Ghandy kozman Avatar

    for anyone that doesn't like the remove method because after removing 10 items you will reach the end index of the array, i made this method that solves that:

    public void remove(){
    if(numberOfItems>0){
    for(int i=0;i<numberOfItems-1;i++){
    queueArray[i]=queueArray[i+1];
    }
    queueArray[–numberOfItems]="-1";
    rear–;
    }else{
    System.out.println("Sorry But the Queue is Empty");
    }
    }

    any suggestions?

  32. Isaiah Keyes '19 Avatar

    This is so helpful. Thank you!

  33. Green T Avatar

    I hate how I'm struggling with data structures 🙁 cause I know I can do it I just need to do it on my own pace

  34. Docktor Dicking Avatar

    I like it how you explain everything very casually, you got another sub 😉

  35. Matt Wilson Avatar

    Just wondering, why are you building all those methods (push, pop etc).
    Aren't they available in java.util?

    Thanks

  36. Ryan Godfrey Avatar

    In a Data Structures and Algo class. I am struggling with a very boring professor. I am on video 3. It seems that you cover every chapter in my text book++. I am coding right along with you. You are exactly what I needed. So thankful. You are awesome!!!!

  37. Colton Milstead Avatar

    How would you make a recursive solution to searching through a stack for a specific value?

  38. Kyle Butler Avatar

    That's all well and good but is it really a must to learn all of those algorithms when they're already in java libraries? To me it's quite difficult to get intuitive with all of those methods and how they interact.

  39. Nicolas Melia Avatar

    Another great and short, but very informative tutorial!

  40. Farhan Utshaw Avatar

    What do you tell at the very beginning of video ??

  41. Silambarasan Nv Avatar

    thank you sir. great tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *