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. The video and code below will cover everything.
If you like videos like this, it helps to tell Google+ [googleplusone]
Code From the Video
TheStack.java
TheQueue.java
Been watching your tutorial starting from linked list to stacks and queues since July 28, 2013. And still having a hard time understanding them a bit.
What is confusing you? I’ll gladly try to help
For the stack code, I think you should move line 48 to line 43, then add “\n” in front of “Sorry” for line 44.
if you fill the stack, then pushing another item wont add anything to the stack, yet your code will say it was added.
Hi Derek, Thank you very much for you videos. I like your way of teaching, it’s a good pace and straight to the point. My question is where exactly I can use stack concept in javascript? Because I can achieve same results using JS builtin arrays also.
You’re very welcome:) It would be a bit complicated to translate this code directly into JavaScript because of how objects are set up. It would be better to use built in tools with JS in my opinion.
I converted these into JS for my understanding.
http://jsfiddle.net/samneo/JqgbD/1/
http://jsfiddle.net/samneo/DcSFu/
Now I got when to use these. Thanks
Very cool! Thank you for sharing 🙂
I believe there may be an error with your insert. Your if simply based on numberOfItems with no regard for what the front index of the array is. There if you fill up the queue, remove some, and then add more I don’t think it will work. Your numberOfItems will be < queueSize and your rear will then go out of bounds.
my version of insert
public void insert (String value){
System.out.println(“Display of queue before entering “+value);
displayTheQueue();
if (numberOfItems+1<queueSize){
if (rear==queueSize)rear=0;
queueArray[rear]=value;
rear++;
numberOfItems++;
}
else{System.out.println("Sorry queue is full");}
System.out.println("Status of queue after entering "+value);
displayTheQueue();
}
Thank you for the input 🙂
I love your toturials.
But In public void priorityInsert(String input)
there is no check to see that the queue is full.
Am I not seeing it?
I have to go back and look. Sorry I made this a while back. I may have thought since the first example made sure an overflow didn’t occur that I forgot about it here.
Hello Derek,
Great video tutorials! Easy to understand.
I just came across this while implementing Stack. Withing the Stack class’s peek(), lets say I call this method even before pushing elements onto the Stack. Wouldn’t peek throw an ArrayIndexOutOfBoundException? Hence we need to check for topOfArray >=0 within the peek method.
Please correct me if otherwise.
Thanks,
Sowjhanya
Hello Sowjhanya,
Yes you are correct that I should always check for a values existence before calling. I wrote this largely out of my head and it wasn’t optimized. I hope that makes sense.
Hi, Awesomely explained but there is a problem in the pop() of stack. It will always return -1. Instead you should first store the value in a different string and then return it. Please see the modified code below.
Please confirm if my understanding is correct.
public String pop (){
System.out.println(“Display stack before removal”);
displayTheStack();
String str;
if (topOfStack >0){
System.out.println(“Value to be deleted = “+stackArray[topOfStack]);
str=stackArray[topOfStack];
stackArray[topOfStack] = “-1″;
topOfStack–;
}
else{
System.out.println(” Stack is empty”);
str=”-1″;
}
System.out.println(“Stack after removal “);
displayTheStack();
System.out.println(“topOfStack = “+topOfStack);
System.out.println(“str = “+str);
return str;
}
Great video. I thought I should mention though that after ten items are inserted into this array, and deleted then we simply cannot insert anything anymore.
A quick way to get around this is to do queueArray[rear % queueSize] or queueArray[front%queueSize]. Now rear and front will always be within the queue range because of the modulus operation.
A problem with this approach is if you ever inserted/deleted 2^32 (Int max size) of items then front will suddenly become -2^32.
So to get around this problem we modify rear and front directly.
rear = (++rear % queueSize);
front = (++front % queueSize);
Now we can add as many items as we want, and only worry about the max size of the array.
Thank you for posting the tip 🙂
Great piece of work Derek !
Kudos…
Thank you 🙂
Do you have any implementation of deques data structures on your website? 🙂
No I didn’t. It is just a queue that pulls data from the front and end. I’m planning on making more data structure tutorials asap