0% found this document useful (0 votes)
161 views

DSU Micro Project

The document proposes using recursion to reverse a stack data structure in C. It includes function definitions for initializing a stack, checking if it is full or empty, pushing and popping elements, printing the stack, and inserting an element at the bottom. The key functions are insertAtBottom(), which inserts an element at the bottom of the stack recursively, and reverse(), which pops elements and stores them in a function call stack before recursively reversing the remaining elements and re-inserting them at the bottom. Sample input and output stacks are provided to demonstrate reversing the stack order using these recursive functions.

Uploaded by

Dhruv Makhija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views

DSU Micro Project

The document proposes using recursion to reverse a stack data structure in C. It includes function definitions for initializing a stack, checking if it is full or empty, pushing and popping elements, printing the stack, and inserting an element at the bottom. The key functions are insertAtBottom(), which inserts an element at the bottom of the stack recursively, and reverse(), which pops elements and stores them in a function call stack before recursively reversing the remaining elements and re-inserting them at the bottom. Sample input and output stacks are provided to demonstrate reversing the stack order using these recursive functions.

Uploaded by

Dhruv Makhija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Micro Project Proposal

Title: Recursion using Stack


Introduction:
A Data structure is a group of data elements that provides the easiest way to store and perform
different actions on a data of the computer.
Recursion in data structure is when a function calls itself indirectly or directly.
Recursive Function use something called “The Call Stack”. When a program calls a function,
that function goes on top of the call stack. This is similar to a stack of books. You add things one
at a time. Then, when you are ready to take something off, you always take off the top item.
Aim of the Topic: Recursion in stack using Data Structure in C.
Course Outcome:
Literature Review:
Proposal Methodology:
#include <stdio.h>

#define MAXSIZE 7
#define TRUE 1
#define FALSE 0

struct Stack
{
int top;
int array[MAXSIZE];
} st;

void initialize()
{
st.top = -1;
}

int isFull()
{
if(st.top >= MAXSIZE-1)
return TRUE;
else
return FALSE;
}
int isEmpty()
{
if(st.top == -1)
return TRUE;
else
return FALSE;
}

void push(int num)


{
if (isFull())
printf("Stack is Full...\n");
else
{
st.array[st.top + 1] = num;
st.top++;
}
}

int pop()
{
if (isEmpty())
printf("Stack is Empty...\n");
else
{
st.top = st.top - 1;
return st.array[st.top+1];
}
}

void printStack()
{
if(!isEmpty())
{
int temp = pop();
printStack();
printf(" %d ", temp);
push( temp);
}
}
void insertAtBottom(int item)
{
if (isEmpty())
{
push(item);
}
else
{
int top = pop();
insertAtBottom(item);

push(top);
}
}

void reverse()
{
if (!isEmpty())
{
int top = pop();
reverse();

insertAtBottom(top);
}
}
int getSize()
{
return st.top+1;
}

int main()
{
initialize(st);
push(1);
push(2);
push(3);
push(4);
push(5);
printf("Original Stack\n");
printStack();
reverse();
printf("\nReversed Stack\n");
printStack();
return 0;
}

Operations Done:
Input Stack
2 <--- Top
4
8
9
Output Stack
9 <--- Top
8
4
2

Here we are going to use recursion to reverse the stack elements. We will store the top elements
of stack on function stack one by one until stack becomes empty. When stack becomes empty,
we will insert an element at the bottom of stack and then insert all the elements stores in function
stack back in same sequence. Here we will use two user defined functions "insertAtBottom" and
"reverse".
void insertAtBottom(int num) : This function inserts a number "num" at the bottom of stack
using recursion.
void reverse() : This function pop's top element and store it in function stack. Then it recursively
calls itself to reverse remaining stack. Once remaining stack elements are reversed, it calls
insertAtBottom function to insert top element at the bottom of stack.

You might also like