0% found this document useful (0 votes)
3 views34 pages

Unit 2 (Stack & Recursion)

The document discusses the implementation of a stack using a linked list in C, detailing the push, pop, and display operations. It also explains the concepts of recursion and iteration, highlighting when to use each method, their advantages, and potential pitfalls such as stack overflow in recursion. Additionally, it provides examples to illustrate the differences between recursion and iteration.

Uploaded by

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

Unit 2 (Stack & Recursion)

The document discusses the implementation of a stack using a linked list in C, detailing the push, pop, and display operations. It also explains the concepts of recursion and iteration, highlighting when to use each method, their advantages, and potential pitfalls such as stack overflow in recursion. Additionally, it provides examples to illustrate the differences between recursion and iteration.

Uploaded by

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

Linked Implementation of Stack in C

#include <stdlib.h>

#include <stdio.h>

// Structure to create a node with data and the next pointer

struct Node {

int data;

struct Node *next;

};

struct Node* top = NULL;

// Push() operation on a stack

void push(int value) {

struct Node *newNode;

newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value; // assign value to the node

if (top == NULL) {

newNode->next = NULL;

} else {

newNode->next = top; // Make the node as top

top = newNode; // top always points to the newly created node

printf("Node is Inserted\n\n");

int pop() {

if (top == NULL) {

printf("\nStack Underflow\n");

} else {

struct Node *temp = top;

int temp_data = top->data;

top = top->next;

free(temp);

return temp_data;
}}

void display() {

// Display the elements of the stack

if (top == NULL) {

printf("\nStack Underflow\n");

} else {

printf("The stack is \n");

struct Node *temp = top;

while (temp->next != NULL) {

printf("%d--->", temp->data);

temp = temp->next;

printf("%d--->NULL\n\n", temp->data);

}}

int main() {

int choice, value;

printf("\nImplementation of Stack using Linked List\n");

while (1) {

printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");

printf("\nEnter your choice : ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("\nEnter the value to insert: ");

scanf("%d", &value);

push(value);

break;

case 2:

printf("Popped element is :%d\n", pop());

break;

case 3:
display();

break;

case 4:

exit(0);

break;

default:

printf("\nWrong Choice\n");

}
Difference Between Recursion and
Iteration
Recursion and Iteration are the basic ways to repeatedly execute a given set of
instructions in any programming language, and hence, every software engineer must
be familiar with these topics. Many software engineering interview problems can be
tackled by the simple use of recursion.

When to Use Recursion?


Sometimes we encounter a problem that is too complex to solve directly. In most
cases, we try to break such problems down into smaller pieces. Then, we can find a
way to solve these smaller pieces and then build up a solution to the entire problem.
This is the entire idea behind recursion.

Recursion is when a function calls itself directly or indirectly, and the function calling
itself is called a recursive function. It is mainly used when the solution to a bigger
problem can be expressed in terms of smaller problems.

To terminate the recursion, we use some conditions so that the function knows when
not to make any further recursive call to itself and return; otherwise, it will lead to
infinite recursion once the function is called. Such conditions are called base
conditions in recursion.

For example, let's define steps for Leo in our example above to get the clue he
needs:

• Step 1: If you have found the clue, stop; else, go to step 2.


• Step 2: Find a target to enter their dream for finding clues. Go to step 3.
• Step 3: Use the machine to enter their dream. Go to step 1.
We can see the recursive solution for finding the clue can be written in three steps.
The base case here is if we have already found the clue, then we simply stop the
recursion. Else, we enter some other target’s dream and redo the entire algorithm.

Wondering how many times the function can call itself?

The local variables and parameters being passed by value are newly created each
time the function calls itself. They are stored in stack memory whenever a recursive
call is made and are deallocated when the execution of the current function call is
complete. The state of the functions is also stored in stack memory. Thus, each
recursive call consumes some amount of memory on the stack. In case of infinite
recursion or when recursion depth is large enough to exhaust the memory on the
stack, it will cause a stack overflow error.
When to Use Iteration?
Iteration is when we execute a set of instructions repeatedly until the condition
controlling the loop becomes false. It includes initialization, comparison, executing
statements inside the iteration, and updating the control variable.

In iteration, it is necessary to have the right controlling condition; else, the program
may go in an infinite loop.

For example, let’s write an iterative program to help Leo find his clue:

• Step 1: Create an initial list of people who are targets.


• Step 2: While the list is not empty, get a target from the list and enter his dream. Go
to step 3.
• Step 3: If you do not get the clue, go to step 2; else, go to step 4.
• Step 4: Congratulations! You found the clue!
Here, the while statement in Step 2 is the controlling statement, which will decide
when the loop will end.
Key Differences Between Recursion and
Iteration

Reference:

https://www.interviewkickstart.com/learn/difference-between-recursion-and-
iteration#:~:text=The%20main%20difference%20between%20these,more%20space%2Defficient%20
than%20recursion.

You might also like