In: Computer Science
Write a program in C or C++ that takes a number series of size n
(n integers) as
input from the user, push all the numbers to the stack, and reverse
the stack using recursion. Please note
that this is not simply popping and printing the numbers, but the
program should manipulate the stack to
have the numbers stored in reverse order. In addition to the
provided header file, the students can use
the following function to print the content of the stack:
#include <stdio.h>
#define MAXSIZE 50
int size;
// structure for stack
struct Stack {
int top;
int elements[MAXSIZE];
} s;
// empty stack. top=-1
void init() {
s.top = -1;
}
// check if stack is full or not
int isFull() {
if(s.top >= MAXSIZE-1)
return 1;
else
return 0;
}
// check if stack is empty or not
int isEmpty() {
if(s.top == -1)
return 1;
else
return 0;
}
// display the stack
void displayStack(){
if(!isEmpty()){
for(int i=s.top;i>=0;--i)
printf("%d\n",s.elements[i]);
}
}
//push element in the stack
void push(int ele) {
if (isFull())
printf("Stack is Full\n");
else {
s.elements[s.top + 1] = ele;
s.top++;
}
}
//pop top element from stack
int pop() {
if (isEmpty())
printf("Stack is Empty...\n");
else {
s.top = s.top - 1;
return s.elements[s.top+1];
}
}
// insert at bottom
void insertAtBottom(int ele) {
// check if stack is empty
if (isEmpty())
push(ele); // psuh elemnet
else {
// store top element
int t = pop();
// insert at bottom
insertAtBottom(ele);
// push top element
push(t);
}
}
// reverse the stack using recursion
void doReverse() {
if (!isEmpty()) {
// pop top element using recursion
// until stack is empty
int t = pop();
doReverse();
// insert element at bottom
insertAtBottom(t);
}
}
int main() {
// declare variables
int i, j, element;
// take size of stack as input
printf("Enter the size of stack: ");
scanf("%d",&size);
// initialize the stack
init();
// take input from user
// for stack elements
i=0;
while(i<size){
printf("Enter a element to push in the stack: ");
scanf("%d",&element);
push(element);
printf("Element %d has been pushed.\n",element);
i++;
}
// display the stack
printf("\nUser given stack: \n");
// display the current stack
displayStack();
// call this function to reverse the stack
doReverse();
printf("\nReversed Stack: \n");
displayStack();
return 0;
}
___________________________________________________________________
Enter the size of stack: 5
Enter a element to push in the stack: 2
Element 2 has been pushed.
Enter a element to push in the stack: 4
Element 4 has been pushed.
Enter a element to push in the stack: 6
Element 6 has been pushed.
Enter a element to push in the stack: 8
Element 8 has been pushed.
Enter a element to push in the stack: 10
Element 10 has been pushed.
User given stack:
10
8
6
4
2
Reversed Stack:
2
4
6
8
10
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.