Follow Me

Stack 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 ...
  • Checking whether elements of 2 BSTs are the same using an iterative function: Knowing that the inorder traversal of BSTs produces sorted lists, write an iterative function function that given 2 BSTs, the function checks whether the elements of both BSTs are the same or not. The time complexity of your algorithm should be \(O(max(m, n))\)...
  • Nth element of a stack: Write a C function that returns the Nth element of a stack. The top element is element number 1. ...
  • Insertion of an element in a stack at position k: Write a C function that inserts an element in a stack at position k (the top element is considered at position 1). ...
  • Deletion of an element in a stack at position k: Write a C function that deletes the kth element of a stack of integers (the top element is considered at position 1). ...
  • Interchanging the top two numbers on the stack: Write a C function that interchanges the top two numbers on the stack. ...
  • Adding all the numbers on the stack together: Write a C function that adds all the numbers on the stack together. ...
  • Computing the average of all numbers on the stack: Write a C function that computes the average of all numbers on the stack. ...
  • Reversing the order of elements on stack using 2 additional stacks: Write a C function that reverses the order of elements on stack using 2 additional stacks. ...
  • Checking whether a string is palindrome or not using a stack: Write a C function to check whether a string is palindrome or not using a stack. Write a main program to test your function. A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either directi...
  • Keeping two stacks within a single linear array: Design a method for keeping two stacks within a single linear array so that neither stack overflows until all of memory is used and an entire stack is never shifted to a different location within the array.Write usual stack routines to manipulate the...
  • 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...
  • Checking if an input character string is of the form xCy: Write a program to determine if an input character string is of the formxCywhere x is a string consisting of the letters ’A’ and ’B’, and where y is the reverse of x (that is, if x="ABABBA", y must equal "ABBABA"). At each point, you may read...
  • Implementation of a stack using an array where the first element is used to contain the index of the top element: Show how (by writing a code) to implement a stack of integers in C by using an array int s[STACKSIZE], where s[0] is used to contain the index of the top element of the stack, and where s[1] through s[STACKSIZE-1] contain the elements on the stack. W...
  • Implementation of an array using a stack: Consider a language that does not have arrays but does have stacks as a data type. That is, one can declare stack s; and the operations push, pop, top operations are defined. Show how a one-dimensional array can be implemented by using these operatio...
  • Printing a sequence of instructions on a machine with 1 register and 6 instructions: Assume a machine that has a single register and 6 instructions. \(\begin{array}{ l l l }\texttt{LD} & \texttt{A} & \textit{places the operand A into the register}\\\texttt{ST} & \texttt{A} & \textit{places the contents of the register into the variable A}\\\texttt{AD} & \texttt{A} & \textit{adds the contents of the variable A to the register}\\\texttt{SB} & \texttt{A} & \textit{substracts the contents of the variable A from the register}\\\texttt{ML} & \texttt{A} & \textit{multiplies the contents of the register by the variable} \\\texttt{DV} & \texttt{A} & \textit{divides the contents of the register by the variable}\\\end{array}\)...
  • 2 players Game - version 1: Consider a game where two players (\(\textit{A}\) and \(\textit{B}\)) each have a bunch of \(\texttt{N}\) (constant) \$ banknotes (1, 5, 10, 20 , \(\cdots\)) arranged in any order, and a third person (\(\textit{C}\)) plays the role of the bank who ha...
  • Recursively reverse a stack: Write a recursive function that inserts an element \(\texttt{e}\) at the bottom of a stack by using only stack operations.\(\textit{Prototype}: \texttt{void InsertAtBottom(Stack *s, element e)}\) Write a recursive function that reverses the elements...
  • Insert an element and keep the stack sorted: Write a recursive function that inserts an element e in a stack and keeps it sorted. The top element is the smallest one....
  • Variable static implementation of a stack: The array-based stack implementation introduced in class uses a fixed length array. As a result, it is possible for the stack to become full. Rewrite the \(\texttt{push}\) method so that it doubles the length of the array when the array is full. Rewr...
  • Find efficiently the minimum element in a stack using an auxiliary stack: Suppose we add a new operation to the stack ADT called \(\texttt{findMinimum}\) that returns a reference to the smallest element in the stack. Show that it is possible to provide an implementation for \(\texttt{findMinimum}\) that has a worst case ru...
  • Find efficiently the minimum element in a stack without using an auxiliary stack: Suppose we add a new operation to the stack ADT called \(\texttt{findMinimum}\) that returns a reference to the smallest element in the stack. Show that it is possible to provide an implementation for \(\texttt{findMinimum}\) that has a worst case ru...
  • Implementation of a stack to hold elements of 2 different types: Suggest an implementation of a stack to hold elements of 2 different types, such as structures and float numbers....
  • Conversion of a decimal number to a number in a base between 2 and 9: Write a program to convert a number from decimal notation to a number expressed in a number system whose base is a number between 2 and 9. The conversion is performed by repetitious devision by the base to which the number is being converted and then...
  • Adding very large floating-point numbers: Write a program for adding very large floating-point numbers. Extend this program to other arithmetic operations....
  • Adding very large integers numbers: Consider adding very large numbers. The largest magnitude of integers is limited, so we are not able to add 18,274,364,583,929,273,748,459,595,684,373 and 8,129,498,165,026,350,236, since integer variables cannot hold such large values, let alone the...
  • Implementation of 3 stacks within a single array: We can keep two stacks within a single linear array so that neither of both stacks overflow until all of memory is used and an entire stack is never shifted to a different location within the array.Thus, both stacks grow towards each other. **img**q2...
  • 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 2 stacks: 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 keep the same relative order". In writing your ...
  • 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...
  • Removing negative numbers from a stack: Consider a stack \( S \) of integers. Write a function \(\texttt{PositiveStack(...)}\) that takes as input a stack \( S \) and removes all negative integers in \( S \). The order of the other integers in the stack is preserved. You may use the stack ...
  • 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...
  • Iterative DFS traversal of a graph using an adjacency list: Perform an iterative DFS traversal over a graph represented using an adjacency list....
  • Topological sort using DFS traversal of a graph using an adjacency list: Perform a topological sort using DFS traversal of a graph using an adjacency list...
  • Same elements in binary search trees: Give the declaration of a Binary Search Tree of integers ($$\texttt{BST}$$). Write a recursive function that checks whether an element belongs to a $$\texttt{BST}$$. Write an iterative function that checks whether an element belongs to a $$\texttt{...
  • Trees Traversal: Consider the following tree: **img**bsttraversals01.png##img## Give the inorder traversal of the above tree. Write a recursive function that prints the inorder traversal of a tree dynamically implemented. Write an iterative function that prints the...
  • Target sum in a BST: Given a Balanced Binary Search Tree and a target sum, write an iterative function that returns $$\texttt{true}$$ if there is a pair with sum equals to target sum, otherwise return $$\texttt{false}$$. Expected time complexity is $$\texttt{O(n)}$$. A...
  • Forest of Binary Search Trees: Consider a forest represented in the form of a linked list where each node points to a Binary Search Tree of words of length $$\texttt{L}$$. The list is sorted according to the length of the word. Example: After the insertion of the following words i...
  • Zig zag level order: Write a function that takes as input a binary tree dynamically implemented and print out the tree in $$\texttt{zig zag}$$ level order (i.e., from left to right, then right to left for the next level and alternate between).  Example: **img**zigzag.p...
  • 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##...
  • 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 ...
  • Sorting an array using two stacks: We want to sort an array of $N$ integers in decreasing order using a stack. To do this, you should use two stacks: a current stack ($\texttt{$S_1$}$) and an auxiliary stack ($\texttt{$S_2$}$) that will be used temporarily for eventual pops.  Write a...

Back to the list of exercises