C Programming Exercises, Practice, Solution : Stack
C Stack [17 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations:
- Push, which adds an element to the collection, and
- Pop, which removes the most recently added element that was not yet removed.
Stacks entered the computer science literature in 1946, when Alan M. Turing used the terms "bury" and "unbury" as a means of calling and returning from subroutines. Stack follows LIFO data structure type.
1. Write a C program to implement a stack using an array with push and pop operations.
Expected Output:
Elements in the stack are: 3 5 4 3 2 1
2. Write a C program to implement a stack using a singly linked list.
Expected Output:
Push data 1 Push data 2 Push data 3 Push data 4 Pop data: 4 Pop data: 3 Pop data: 2 Pop data: 1 Check a stack is empty or not? Stack is empty!
3. Write a C program to check a stack is full or not using an array with push and pop operations.
Expected Output:
Stack size: 3 Original Stack: 1 2 3 Push another value and check if the stack is full or not! Stack is full! Stack size: 3 Original Stack: 10 20 Check the said stack is full or not! Stack is not full!
4. Write a C program that accepts a string and reverse it using a stack.
Expected Output:
Input a string: w3resource Reversed string using a stack is: ecruoser3w
5. Write a C program to implement two stacks in a single array and performs push and pop operations for both stacks.
Expected Output:
Elements in Stack-1 are: 50 40 30 10 Elements in Stack-2 are: 70 60 50 40 20
6. Write a C program to sort a given stack using another stack.
Expected Output:
Original stack: 1 5 5 2 3 8 Sorted stack: 1 2 3 5 5 8
7. Write a C program that checks whether a string of parentheses is balanced or not using stack.
Expected Output:
Input an expression in parentheses: {[]) The expression is not balanced. ----------------------------------------- Input an expression in parentheses: ((())) The expression is balanced. ----------------------------------------- Input an expression in parentheses: ()) The expression is not balanced. ----------------------------------------- Input an text of parentheses: ([]){}[[(){}]{}] The expression is balanced. ----------------------------------------- Input an expression in parentheses: [(])) The expression is not balanced.
8. Write a C program to find the next greater element for each element in an array using a stack. Return -1 if there is no next-larger element.
Expected Output:
Elements in the array are: 1 2 3 4 5 6 The next larger elements are: 1 --> 2 2 --> 3 3 --> 4 4 --> 5 5 --> 6 6 --> -1 Elements in the array are: 6 5 4 3 2 1 0 The next larger elements are: 0 --> -1 1 --> -1 2 --> -1 3 --> -1 4 --> -1 5 --> -1 6 --> -1 Elements in the array are: 3 7 5 9 3 2 4 1 4 The next larger elements are: 3 --> 7 5 --> 9 7 --> 9 2 --> 4 3 --> 4 1 --> 4 4 --> -1 9 --> -1 --------------------------------
9. Write a C program to implement two stacks using a single array.
Expected Output:
3 popped from stack 1 2 popped from stack 1 1 popped from stack 1 30 popped from stack 2 20 popped from stack 2 10 popped from stack 2
10. Write a C program that reverses a stack using only stack operations push and pop.
Expected Output:
Original Stack: 10 20 30 40 50 Reversed Stack: 50 40 30 20 10
11. Write a C program to find the minimum element in a stack.
Expected Output:
Current stack elements: 9 2 4 2 4 Minimum element: 2 After removing two elements: Current stack elements: 9 2 4 Minimum element: 2 After adding one element: Current stack elements: 9 2 4 1 Minimum element: 1
12. Write a C program to find the maximum element in a stack.
Expected Output:
Current stack elements: 5 2 1 6 8 Maximum element: 8 After removing two elements: Current stack elements: 5 2 1 Maximum element: 5 After adding one element: Current stack elements: 5 2 1 10 Maximum element: 10
13. Write a C program to implement a stack that supports push, pop, get middle, and delete middle elements.
Expected Output:
Stack elements: 88 15 26 32 23 Middle element: 26 Delete the middle element of the said stack: Stack elements: 88 15 32 23 Middle element: 15 Delete the middle element of the said stack: Stack elements: 88 32 23 Middle element: 32
14. Write a C program to calculate the average value of the stack elements.
Expected Output:
Elements of the stack: 6 4 2 5 3 1 Average of the said stack values: 3.50 Popped value: 6 Elements of the stack: 4 2 5 3 1 Average of the said stack values: 3.00
15. Write a C program to implement a stack and accept some numeric values. Remove the number whose value is the minimum on the stack.
Expected Output:
Elements of the stack: Stack: 7 4 5 2 3 1 Minimum value of the said stack: 1 Elements of the stack after removing the said minimum value: Stack: 7 4 5 2 3 Minimum value of the said stack: 2 Elements of the stack after removing the said minimum value: Stack: 7 4 5 3 Minimum value of the said stack: 3 Elements of the stack after removing the said minimum value: Stack: 7 4 5
16. Write a C program to implement a stack and accept some numeric values. Find the top and kth element of the stack.
Expected Output:
Elements of the stack: 1 2 3 4 5 6 Top element: 6 3rd element from top: 4 Remove the topmost element from the stack: Elements of the stack: 1 2 3 4 5 Top element: 5 4th element from top: 2
17. Write a C program to convert a decimal number to its binary equivalent using stack.
Expected Output:
Input a decimal number: 10 The binary equivalent is: 1010 Input a decimal number: 109 Binary equivalent of the said number is: 1101101 Input a decimal number: 2015 Binary equivalent of the said number is: 11111011111
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://www.w3resource.com/c-programming-exercises/stack/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics