7/25/2023 0 Comments Java stack program codeWe also increment the number of items by 1. This is done by incrementing the rear pointer and appending it to that index. The enqueue function takes the element to be inserted and checks whether the queue is full, before inserting it to the rear. Now let’s get into the actual implementation of the enqueue and dequeue operations. Check empty and check full is used to check whether the queue has any items or not. Peeking is used to view the current item at the rear and front. We have four utility functions as shown below: Then we have an array for the queue itself, the maximum size, and also a count of the number of items. This is because, in a queue, insertion happens at the rear and deletion happens from the front. The queue is very similar to the stack class that we saw earlier. Another visualisation of pushing and popping When we call the pop function, the topmost element is deleted and the value of top is decremented by 1. Once top is equal to 2 (which is maxSize – 1), we cannot insert elements further. The below diagram is a helpful visualisation of the entire process. On the last line, we pop off the top element. We insert 3 elements into it after which no insertion is possible. Yours should look a little like this.Ĭheck out the driver code above to create a stack of size 3. The peek function is simply a utility function that allows us to view which element is at the top currently. If not, we print out the element that is currently at the top and decrement it. If that's the case, we cannot pop anything out of it. We first check if top is equal to -1, which is the initial condition of the stack being empty. If it's not full, we increment the top value and insert the item at that index in the array. At this point, check if the top is pointing to maxSize – 1, which means that the stack would be full and we cannot insert. The function accepts an integer value that is to be pushed onto the stack. Now, let’s take a look at the insertion and deletion operations, more popularly known as pushing and popping. Remember Java uses zero-based indexing which means that the first element would be at index 0, the second one would be at index 1, and so on. You might be thinking about why top is initialised to -1. After this, an array of that size is created and top is initialised to -1. We have a constructor that is used to initialise the stack with the max size value. We have three variables that determine the stack size, the elements themselves, and the top element. Firstly, create a class and name it Stack. Implementing them in Java is pretty straightforward, so you'll enjoy trying this. You might have used a network printer to print documents and this uses a queue internally so that multiple people can request at the same time, but will be processed sequentially.įirst up, we have stacks. They are widely used for controlling access to resources. Queues are also extremely popular in networking as well as system design. Insertion operation is known as enqueue and deletion is known as dequeue. They represent an actual queue in real-life where people who enter first are serviced first and so on. Queues, on the other hand, follow the FIFO principle, which is first in first out. They are also used when control passes from one point of a program to another to store the return addresses. Stacks are extremely useful data structures used very widely in computer science for recursive function calls. The operations are referred to as push and pop for insertion and deletion respectively. This is referred to as the top in stack terminology. This simply means that insertion of a new node and removal of a node takes place from the end. Stacks are linear data structures that follow the LIFO principle, that is, last in first out. Our take? Knowing how to implement data structures from scratch and performing operations with them will not only enhance your skills as an engineer but also makes you more attractive to potential employers. We'll go through some of the theory behind stacks and queues, and then dive straight into Java code and see how we can implement them from scratch. So how can you implement these? As usual, we’ve got you covered with this tech tutorial. You’ll find these data structures highly used in Computer Science to implement functionality and logic. This time, we’re delving into the fundamental data structures that are stacks, and queues. In one of our previous articles (which you can find here) we looked at how to implement Linked Lists in Java.
0 Comments
Leave a Reply. |