Implementation of Queue
Example # 1
To create a queue in C sharp, a simple method is used as we declare a dynamic memory by using a “new” operator; similarly, the queue is declared that way. First, we need to mention those libraries useful in executing the C sharp queue program. Two main header files of collection and system are mentioned here.
The new object is used to create a queue. This object is then assigned to the qt variable. Later on, all the operations on the queue are done by using this operator.
After the creation of the queue, we can now add items to the queue. Items are added through a built-in function of the queue that is initiated through the variable, to which we have assigned all the authorities to carry out all functions of the queue at the time of declaration of the queue. An enqueue function is used to add items in the queue.
The item that you want to enter is written in the parameter of the enqueue function. In this way, you can add values to the queue. This addition of elements is manual. We can also use loops for this purpose for adding several items with less time complexity.
To display all the items, we use a for each loop, which will take an object to iterate through the queue. Another feature of a queue that is used to display the total number of items added to it is to use a count function through the object.
MCS is the compiler that is used to compile the code in Ubuntu for C sharp language to get executed. After compilation of the source code, Mono plays a role in executing the cs file by using a .exe extension.
$ Mono file.exe
You can see that the resultant value contains all items present in the queue and the total number of items that are obtained through the count().
Example # 2
Like the addition of a queue and for the removal, a function of the queue is used to delete the items present in the queue. We have to use the function of removing in the example provided below.
First, add items through the enqueue function after the declaration. We have entered three items through the enqueue function. Now, we will remove one item. The first item that is present at the top of the queue will be first removed. For example, 16 is added first, so it will be removed first.
There is no need to mention the number or item in the parameter of the dequeue function because it is obvious for this built-in feature to remove the first item automatically. After removing the first item, the second item comes in the place of the first item. So, if you use the dequeue function again, the second item (new first) will be removed.
A for each loop will display all the items remaining after the removal. Now, execute the code, and you will see the results that the first item is removed while the second and third items are displayed.
Example # 3
If you want to remove or get the value of the queue that is present at the topmost position, then you can achieve this by using two simple features of the queue. One is the Peek() function, and the other one is the Dequeue() function:
- Peek(): This is the method that is used to return the object at the start of the queue without removing the item.
- Dequeue(): This function returns the object at the start with some modifications. This means that this removes the topmost item from the queue.
Now, we will use these features in the example to elaborate on their functionality. First, we will create a queue and add items to it. By using the count() function, we will display all the items added to the queue. My_queue() is the object of the newly declared queue, as shown below:
As we have discussed earlier, the dequeue method removes the item that is present at the top of the queue. So the topmost item will be obtained by the dequeue() function.
After removal, the count function is used to show the number of items left. Without removing the item, we want to know, at this point which element is present at the topmost position. When the first item was removed, the second becomes first automatically. So upon using the peak function, we will get the second item that is currently the new first after the FIFO approach.
Again, count the number of items now to ensure that the items are not removed through the peek function.
On execution, you can see that the total number of items was 5; the topmost element is displayed and then removed from the queue. Then, the count function will display the items again. The current topmost items will be obtained, and the count function shows that nothing is removed.
Example # 4
check it through a built-in feature. Unlike C++, C, or any other programming language, as it is hard for the item to be searched, we use different methods like looping to iterate through item searching for the relevant item. C sharp has made it easier by using a simple contain() function that checks the item’s availability by taking that item in its parameter.
Let us create and enter items in the queue. An “if-statement” uses the condition to check the item through the function contains().
If the item is present, then the message of availability will be displayed.
On the execution, you can see that the item is present in the queue.
Conclusion
C sharp queue is declared through the object of the new operator. In C sharp programming language, the queue contains a built-in function for almost every operation applied to the queue. For example, we can add items, remove them, search in the queue, or get the topmost items that are present currently in the queue. There are several applications in which queues are used, for example, in switches and routers. Besides daily life routine, switches are used in semaphores, CPU, and Disc scheduling. All of the features of the queue are used in the examples that are implemented in Linux operating system.
from https://ift.tt/E8fWFnA
0 Comments