Change Language. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. In queue , insertion and deletion happen at the opposite ends, so implementation is not as simple as stack. To implement a queue using array, create an array arr of size n and take two variables front and rear both of which will be initialized to 0 which means the queue is currently empty.
Element rear is the index upto which the elements are stored in the array and front is the index of the first element of the array. Now, some of the implementation of queue operations are as follows: Enqueue: Addition of an element to the queue. Adding an element will be performed after checking whether the queue is full or not. Dequeue: Removal of an element from the queue. Approach: Define a class for implementation of the queue data structure.
Take two variables front and rear both initialized to 0 which indicates that the queue is currently empty. Front is the index of the first element of the array. Rear is the index up to which the elements can be stored in the queue. The program will maintain a list of car models, their price, name of manufacturer, engine capacity etc. The menu should allow insertion of information related to new car models, delete obsolete models, answering queries such as listing all car models within a price range specified by the user and listing all the details given a car model.
The program can be implemented using any of the data structure you have studied in this course. Your email address will not be published. The queue is a type of data structure that can be implemented using an array or a linked list.
Here, we have given a brief knowledge of the process of implementing a queue using an array. A queue is data structure that is based on first-in first-out FIFO in which the first item input is also the first item removed. Items are added to the end of the line and removed from the beginning. When utilising an array to construct a queue, the fact that an array has a fixed size once declared poses an issue in the queue implementation. When elements are added to a queue and then deleted, a gap is created.
To fill the gap, we can rearrange the remaining components to fill the space, but it is a time-consuming procedure. Another alternative is to use a circular queue, with the front and back pointing to the beginning of the array after the maximum size has been achieved.
As you can see from the code there are insertions and deletions as the front and back progress towards the higher index. Though not implemented as a circular queue, it will result in the queue becoming full and unable to accept new items, even if space has been made in the front due to the deletions.
CurrentSize is a field that is increased with each entry and decremented with each removal to keep track of the current size. Similarly, after maxSize is reached, front can be wrapped to start from the beginning. JavaTpoint offers too many high quality services.
0コメント