Follow Me

Queue exercises in C Programming in C

  • Maximum value in a Binary tree of integers: Write each time a function that returns the maximum value of all the nodes in a binary tree of integers. Assume all values are nonnegative; return -1 if the tree is empty. recursive function iterartive using a queue iterative using a stack ...
  • Sorting distinct elements of a queue: Write a function that sorts the distinct elements of a queue of integers in such a way that : Even elements are in the same order at the beginning of the queue. Odd elements are in reverse order at the end of the queue. Use only the ADT operations ex...
  • Implementing pop operation on a stack using 2 queues: The purpose of this question is to simulate the operation of a stack using 2 queues. Consider the following code: typedef ... element ;typedef struct {Queue L, R;} Stack ;void Push(Stack *s, element e){    Enqueue(&(s->L),e) ;} Give the cod...
  • Implementation of a deque: A deque is a data structure that allows both deletion as well as insertion of elements to be done at both ends of a queue. Consider the following code: \(\texttt{typedef $\cdots$ element ;}\\\texttt{struct cell \{ element data; struct cell *next; \}}\\\texttt{typedef struct \{ struct cell *front, *rear; \} deque;}\)...
  • Reverting a queue: Write a C function that reverts a queue....
  • Implementing a queue using two stacks: Implement a queue using two stacks....
  • Moving all the entries from a Stack into a Queue: Use the methods for stacks and queues developed in the course to write the following function: "Move all the entries from a Stack into a Queue". In writing your function, be sure to check for empty and full structures as appropriate. Your function ma...
  • Moving all the entries from a Queue onto a Stack: Use the methods for stacks and queues developed in the course to write the following function: "Move all the entries from a Queue onto a Stack". In writing your function, be sure to check for empty and full structures as appropriate. Your function ma...
  • Stacking a stack on top of a reversed stack: Use the methods for stacks and queues developed in the course to write the following function: "Empty one Stack onto the top of another Stack in such a way that the entries that were in the first Stack are in the reverse of their original order". In...
  • Reversing a stack using a queue: Use the methods for stacks and queues developed in the course to write the following function: "Use a local Queue to reverse the order of all the entries in a Stack". In writing your function, be sure to check for empty and full structures as appropr...
  • String processing using a queue: Write a function that will read one line of input from the terminal. The input is supposed to consist of two parts separated by a colon \(\texttt{:}\). As its result, your function should produce a single character as follows: \(\begin{array}{c l }\texttt{N} & \texttt{No colon on the line}.\\\texttt{L} & \texttt{The left part (before the colon) is longer than the right}.\\\texttt{R} & \texttt{The right part (after the colon) is longer than the left}.\\\texttt{D} & \texttt{The left and right parts have the same length but are different}.\\\texttt{S} & \texttt{The left and right parts are exactly the same}.\\\end{array}\)...
  • Variable static implementation of a queue - version 1: The array-based queue implementation introduced in class uses a fixed length array. As a result, it is possible for the queue to become full. Rewrite the \(\texttt{enqueue}\) method so that it doubles the length of the array when the array is full. R...
  • 2 players Game - version 2: Consider a game where 2 players ($A$ and $B$) each has a bunch of cards, and play alternately.Playing a shot for a player ($A$) consists in taking the first 2 cards from his bunch and comparing them. We distinguish the following two cases: Both cards...
  • Variable static implementation of a queue - version 2: Implement queue using dynamic memory allocation such that the implementation should abide with the following constraints. The user should use memory allocation from the heap using \(\texttt{malloc()}\) function. You need to take care of the max value...
  • Simulating a boarding desk: The purpose of this question is to simulate the work done on the boarding desks of an airline company at the airport. Passengers wanting to leave on a flight, and after going through the check-in phase, must go through the boarding phase which gives ...
  • Simulating a garage: The garage in your home town contains a single lane that holds up to ten cars. Cars arrive at the south end of the garage and leave from the north end. If a customer arrives to pick up a car that is not nearest the northernmost, all cars to the north...
  • Ordering jobs: Consider a queue of \(\textbf{JOB}\)s having each an identifier (integer) and a priority between 0 and \(\texttt{N-1}\) (\(\texttt{N}\) constant).We want to transform the queue so that tasks of priority 0 are in the front, followed by tasks of priori...
  • Ordering elements in a queue based on colors: Consider a queue of objects each having an identifier (integer) and a color among the three values (blue, white, red). We want to change the queue so that the blue elements are in the beginning of queue, followed by white elements, and finally red on...
  • Ordering elements in a queue based on priorities: Consider a queue of tasks each having an identifier (integer) and a priority between 0 and \(N-1\) (\(N\) constant).We want to transform the queue so that tasks of priority 0 are in front, followed by tasks of priority 1 ... finally, tasks of priorit...
  • Solitaire Card Game: People have spent so much time playing card games of solitaire. A form of solitaire is described below. Your task is to write a computer program to play the game thus freeing hours of time for people to play more useful games. To begin the game, 28 c...
  • Picking up sticks: Pick up sticks is a fascinating game. A collection of colored sticks are dumped in a tangled heap on the table. Players take turns trying to pick up a single stick at a time without moving any of the other sticks. It is very difficult to pick up a st...
  • Beverages: Zahraa has just finished college and decided to go out with friends. Zahraa has some strange habits and thus she decided to celebrate this important moment of her life drinking sugar a lot. She will start drinking beverages with low sugar content, li...
  • Changing strings: Consider the following declaration:typedef struct {    int V;    int E;    int **Adj; } Graph;Given a $$\texttt{start string}$$, $$\texttt{end string}$$ and a $$\texttt{set of strings}$$, find if there exists a path between the $$\texttt{start ...
  • Bellman-Ford Algorithm: If the graph has negative edge costs, then Dijkstra's algorithm does not work. The problem is that once a vertex $$u$$ is declared known, it is possible that from some other, unknown vertex $$v$$ there is a path back to $$u$$ that is very negative. I...
  • Dynamic implementation of a priority queue: A priority queue is an abstract data type which is like a regular queue, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. In som...
  • Iterative BFS traversal of a graph using an adjacency list: Perform an iterative BFS traversal over a graph represented using an adjacency list....
  • Iterative BFS traversal of a graph using an adjacency matrix: Perform an iterative BFS traversal over a graph represented using an adjacency matrix....
  • Path between two vertices: Write a program that checks whether a path exists between two vertices in a graph. Implement the graph using an adjacency list and use DFS....
  • Connected graph: Write a program that checks whether a graph is connected. Implement the graph using an adjacency list and use DFS....
  • Topological sort using BFS traversal of a graph using an adjacency matrix: Perform a topological sort using BFS traversal of a graph using an adjacency matrix....
  • Topological sort using BFS traversal of a graph using an adjacency list: Perform a topological sort using BFS traversal of a graph using an adjacency list....
  • Shortest path from a source in an unweighted graph using an adjacency matrix: Give a ahortest path from a source in an unweighted graph using an adjacency matrix....
  • Shortest path from a source in an unweighted graph using an adjacency list: Give a ahortest path from a source in an unweighted graph using an adjacency list....
  • Width of binary trees - iterative version: Write a function that computes the maximum width of a dynamically implemented BT. We define the maximum width of a BT as the maximum number of nodes located at the same level....
  • N-ary tree: A n-ary tree is a tree in which each node has at most $\texttt{n}$ children. The binary tree is a special case of n-ary tree in which n = 2.  Given the following n-ary tree structure #define n ...typedef ... element;typedef struct node {     e...
  • Binary Search Tree of Events: A historian wants to classify a set of events. Each event is characterized by a $$\textit{title}$$, a $$\textit{location}$$ and a $$\textit{period}$$ of time (as most of the events are spread over several years). $$\textit{Title}$$ and $$\textit{loca...
  • Statically implemented Binary Tree: Write a recursive function $\texttt{int max$\_$rec(Btree B, int root$\_$index)}$ that returns the maximum element in a binary tree statically implemented rooted at index $\texttt{root$\_$index}$.Example: Consider the following Binary tree and its r...
  • Maximum width and depth: We define the maximum width of a BT as the maximum number of nodes located at the same level, and the maximum depth as the longest path from the root to a leaf.  Write a function that computes the maximum width of a dynamically implemented BT. Wri...
  • Statically implemented Complete Binary Tree: A binary tree is called complete if all its non-leaf nodes have two children. Write a function that tests whether a binary tree statically implemented is complete....
  • Fairly well developed binary tree: We define the width of a level of a BT as the number of nodes located at this level. A BT is a fairly well developed if the width of each level of the BT is $>$ than the one of the previous level. Write a function that determines whether a BT is ...
  • Reverse level order: Write a function that prints the level order data in reverse order. For example, the output for the below tree should be: 0 9 11 8 15 7 35 5 **img**zigzag.png##img##...
  • Deepest node of the binary tree: Write a function to find the deepest node of the binary tree....
  • Display elements at odd level in binary trees: Write a function that displays the elements of a binary tree located at odd left to right and top to bottom. The root of the tree is assumed to be at level 0. Example: **img**bsttraversals01.png##img## Expected result: 25 11 30 45...
  • Level with maximum sum in binary trees: Write a function that finds the level that has the maximum sum in the binary tree....
  • Hashing with queues: Consider a hash table $$\texttt{T[m]}$$ ($$m$$ is a constant) in which collisions are handled by separate chaining so those collide elements are added to a queue. Give the declaration of the data structure to use. Write a function that adds an elemen...
  • Calkin-Wilf tree: The Calkin-Wilf tree is a complete binary tree. The tree is rooted at the number 1, and any rational number expressed in simplest terms as the fraction $\frac{a}{b}$ has as its two children the numbers $\frac{a}{a+b}$ and $\frac{a+b}{b}$. Every posit...
  • Stern-Brocot tree: The Stern-Brocot tree is an elegant construction to represent the set of all positive fractions. It was independently discovered by German mathematician Moritz Stern in 1858 and by French watchmaker Achille Brocot in 1861. The construction starts at ...

Back to the list of exercises