C Programming Exercises, Practice, Solution : Queue
C Queue [13 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
From Wikipedia -
In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. By convention, the end of the sequence at which elements are added is called the back, tail, or rear of the queue, and the end at which elements are removed is called the head or front of the queue, analogously to the words used when people line up to wait for goods or services.
1. Write a C program to implement a queue using an array. Programs should contain functions for inserting elements into the queue, displaying queue elements, and checking whether the queue is empty or not.
Expected Output:
Initialize a queue! Check the queue is empty or not? Yes Insert some elements into the queue: Queue elements are: 1 2 3 Insert another element into the queue: Queue elements are: 1 2 3 4 Check the queue is empty or not? No
2. Write a C program to implement a queue using an array. Create a function that removes an element from the queue.
Expected Output:
Initialize a queue! Insert some elements into the queue: Queue elements are: 1 2 3 Delete an element from the said queue: Queue elements are: 2 3 Insert another element into the queue: Queue elements are: 2 3 4
3. Write a C program to implement a queue using a linked list. Programs should contain functions for inserting elements into the queue, displaying queue elements, and checking whether the queue is empty or not.
Expected Output:
Initialize a queue! Check the queue is empty or not? Yes Insert some elements into the queue: 1 2 3 Insert another element into the queue: 1 2 3 4 Check the queue is empty or not? No
4. Write a C program to implement a queue using an array. Create a function that removes an element from the queue.
Expected Output:
Initialize a queue! Check the queue is empty or not? Yes Insert some elements into the queue: 1 2 3 Insert another element into the queue: 1 2 3 4 Check the queue is empty or not? No
5. Write a C program to count the number of elements in a queue.
Expected Output:
Initialize a queue! Check the queue is empty or not? Yes Number of elements in queue: 0 Insert some elements into the queue: Queue elements are: 1 2 3 Number of elements in queue: 3 Delete two elements from the said queue: Queue elements are: 3 Number of elements in queue: 1 Insert another element into the queue: Queue elements are: 3 4 Number of elements in the queue: 2
6. Write a C program to reverse the elements of a queue.
Expected Output:
Queue elements are: 1 2 3 4 5 Reverse Queue, elements are: 5 4 3 2 1 Add two elements to the said queue: Queue elements are: 5 4 3 2 1 100 200 Reverse Queue, elements are: 200 100 1 2 3 4 5
7. Write a C program to calculate the sum of the elements in a queue.
Expected Output:
Queue elements are: 1 2 3 4 5 Sum of the elements in the queue is: 15 Remove 2 elements from the said queue: Queue elements are: 3 4 5 Sum of the elements in the queue is: 12 Insert 3 more elements: Queue elements are: 3 4 5 300 400 500 Sum of the elements in the queue is: 1212
8. Write a C program to compute the average value of the elements in a queue.
Expected Output:
Queue elements are: 1 2 3 4 5 Average of the elements in the queue is: 3.000000 Remove 2 elements from the said queue: Queue elements are: 3 4 5 Average of the elements in the queue is: 4.000000 Insert 3 more elements: Queue elements are: 3 4 5 300 427 519 Average of the elements in the queue is: 209.666672
9. Write a C program to find the maximum element in a queue.
Expected Output:
Queue elements are: 1 2 3 4 5 Maximum value in the queue is: 5 Remove 2 elements from the said queue: Queue elements are: 3 4 5 Maximum value in the queue is: 5 Insert 3 more elements: Queue elements are: 3 4 5 600 427 519 Maximum value in the queue is: 600
10. Write a C program to find the minimum element in a queue.
Expected Output:
Queue elements are: 1 2 3 4 5 Minimum value in the queue is: 1 Remove 2 elements from the said queue: Queue elements are: 3 4 5 Minimum value in the queue is: 3 Insert 3 more elements: Queue elements are: 3 4 5 600 -427 519 Minimum value in the queue is: -427
11. Write a C program to delete the nth element of a queue.
Firstly, this code checks if the queue is empty or if the position to delete is invalid. If the position is valid, it deletes the first n-1 elements, then deletes the nth element by calling the dequeue() function.
Expected Output:
Insert some elements into the queue: Queue elements are: 1 2 3 4 5 Delete the 7th element of the said queue: Error: Invalid position Delete the 3rd element of the said queue: Queue elements are: 4 5
12. Write a C program to sort the elements of a queue in ascending order.
Expected Output:
Input some elements into the queue: Elements of the queue: 4 2 7 5 1 Sort the said queue: Elements of the sorted queue in ascending order: 1 2 4 5 7 Input two more elements into the queue: Elements of the queue: 1 2 4 5 7 -1 3 Sort the said queue: Elements of the sorted queue in ascending order: -1 1 2 3 4 5 7
13. Write a C program to find the median of the elements in a queue. >
From Wikipedia,
In statistics and probability theory, the median is the value separating the higher half from the lower half of a data sample, a population, or a probability distribution.
The median of a finite list of numbers is the "middle" number, when those numbers are listed in order from smallest to greatest.
If the data set has an odd number of observations, the middle one is selected. For example, the following list of seven numbers,
1, 3, 3, 6, 7, 8, 9 has the median of 6, which is the fourth value.
If the data set has an even number of observations, there is no distinct middle value and the median is usually defined to be the arithmetic mean of the two middle values. For example, this data set of 8 numbers1, 2, 3, 4, 5, 6, 8, 9 has a median value of 4.5, that is (4+5)/2 . (In more technical terms, this interprets the median as the fully trimmed mid-range).
Expected Output:
Input some elements into a queue: Queue elements are: 1 2 3 4 5 The median of the elements in the queue is 3.000000 Input one more element: Queue elements are: 1 2 3 4 5 6 The median of the elements in the queue is 3.500000
C Programming Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/queue/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics