Queues
- A queue is a inear list in which data can be inserted only at one end called the rear, and deleted on the other end called the front.
Queue Operations
- There are two basic operations. The enqueue and dequeue. Although there are similarities between stacks and queues, one significant structural difference is that the queue implementation needs to keep track of the front and the rear of the queue, whereas the stack needs to worry about one end: the top.
Enqueue
- The queue insert operation is known as enqueue. After the data have been inserted into the queue, the new element becomes the rear.
For instance, a queue has two elements 123 and 757. The element 123 is the front and the element 757 is the rear. If we want to insert a new element in a queue, we are going to insert it at the rear. And the new element becomes the rear.
If there i s niot enough room for onother element in the queue, the queue is an overflow state.
Dequeue
- The queue delete operation is known as dequeue. The data at the front of the queue are returned to the user and removed from the queue.
For example, a queue has three elements: 123, 757, and 459. 123 is the front and 459 is the rear. If we are going to delete an element from the queue, we deleted it at the front and the element next to it becomes the front.
If there are no data in the queue when dequeue is attempted, the queue is an underflow state.
Queue Link List Design
- We implement our queue using a linked list.
- We ned two different structures to implement the queue: a queue head structure and a data node structure. After it is created, the queue will have one head node a nd zero depending on its current state.
Queue Head
- The queue requires two pointers and a count. These fields are stored in the queue head structure.
The two pointers are the front and the rear. A count counts of how many datas are in the queue.
Queue Data Node
- The queue data node contains the user data and a link field pointing to the next node, if any. These nodes are stored in a dynamic memory and are inserted and deleted as requested by the using program.
No comments:
Post a Comment