Ds Practical File-1
Ds Practical File-1
ALGORITHM
CODE
#include <stdio.h>
int main()
{
int n;
// Input the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n]; // Declare the array with size n
// Input the elements of the array
printf("Enter %d integers:\n", n);
for (int i = 0; i< n; i++)
{ printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Bubble Sort algorithm
for (int i = 0; i< n - 1; i++) {
// Track whether a swap was made
int swapped = 0;
// Last i elements are already sorted
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] >arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1; // Set swapped to indicate a swap occurred
}
}
// If no two elements were swapped in the inner loop, then the array is sorted
if (swapped == 0) {
break;
}
}
return 0;
}
PROGRAM NO: 2
ALGORITHM
CODE
#include <stdio.h>
int main() {
int n;
// Push the initial indices of the array onto the stack stack[+
+top] = 0; // Start index
stack[++top] = n - 1; // End index
// Partitioning process
for (int j = start; j < end; j++)
{ if (arr[j] < pivot) {
i++; // Increment index of smaller element
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap the pivot element with the element at i + 1
int temp = arr[i + 1];
arr[i + 1] = arr[end];
arr[end] = temp;
// The pivot is now at the correct position
int pivotIndex = i + 1;
// Push the indices of the left sub-array onto the stack
if (pivotIndex - 1 > start) {
stack[++top] = start; stack[+
+top] = pivotIndex - 1;
}
// Push the indices of the right sub-array onto the
stack if (pivotIndex + 1 < end) {
stack[++top] = pivotIndex + 1;
stack[++top] = end;
}
}
// Display the sorted array
printf("Sorted array: \n");
for (int i = 0; i< n; i++)
{ printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
PROGRAM NO: 4
AIM:- Write a program to merge two sorted arrays using Merge Sort Technique.
ALGORITHM
ENTER (a[10],n)
1. Repeat step 2 for i = 0 to (n-1)
2. Input a[i]
3. Return
DISPLAY(c[20],p)
1. Repeat step 2 for k = 0 to p-1
2. Print c[k]
3. Return
MAIN(
)
1. Start
2. Input no. of elements in 1st & 2nd array as
‘n’ & ‘m’
3. Enter (a.n)
4. Enter (b,m)
5. i = j = k = 0
6. Repeat step 7 to 12 while ((i < n)&&(j < m))
7. If (a[i] >= b[j]),goto step 9
8. c[k+1] = a[i+1]
9. If a[i] = b[j] ,goto step 11 10. c[k++] = b[j++]
goto step 7 11. c[k++] = a[i++]
12. j++
13. Repeat step 14 while (i<n)
14. c[k++] = a[i++]
15. Repeat step 16 while m > j
16. c[k++] = b[j++]
17. Display merged arrays as display(c;k)
18. Exit
CODE
#include <stdio.h>
int main()
{ int n1,
n2;
return 0;
}
PROGRAM NO: 5
ALGORITHM
Exit
CODE
#include <stdio.h>
int main() {
scanf("%d", &n);
scanf("%d", &arr[i]);
scanf("%d", &searchElement);
if (arr[i] == searchElement) {
if (!found) {
return 0;}
PROGRAM NO: 6
AIM:-Write programs for finding the element in the array using the Binary Search Technique.
ALGORITHM
1. low = 1,high = n
2. Repeat step 3 to 5 while low <= high
3. mid = (low + high)
4. If a[mid] = x
Print “ found at mid”
Return
5. If (a[mid] < x) low = mid + 1 Else
High = mid – 1
6. Print “x not found”
7. Exit
CODE
#include <stdio.h>
int main() {
int n, searchElement, left, right, middle;
// Input the size of the array
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);
intarr[n]; // Declare the array with size n
// Input the elements of the array
printf("Enter %d sorted integers:\n", n);
for (inti = 0; i< n; i++)
{ printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Input the element to search for
printf("Enter the element to search for: ");
scanf("%d", &searchElement);
// Binary Search Implementation
left = 0;
right = n - 1;
int found = 0; // Flag to check if the element is found
while (left <= right) {
middle = left + (right - left) / 2; // Calculate the middle index
if (arr[middle] == searchElement) {
printf("Element %d found at index %d.\n", searchElement, middle);
found = 1; // Set found flag
break; // Exit the loop if found
} else if (arr[middle] <searchElement) {
left = middle + 1; // Search in the right half
} else {
right = middle - 1; // Search in the left half
}
}
if (!found) {
printf("Element %d not found in the array.\n", searchElement);
}
return 0;
}
PROGRAM NO: 7
ALGORITHM
INSERTION
PUSH(item)
1. If (item = max of stack)
Print “overflow”
Return
2. top = top + 1
3. stack[top] = item 4. Return
DELETION
POP(item)
1. If (top = - 1)
Print
“underflow”
Return
2. Item = stack[top]
3. top = top – 1
4. Return
DISPLAY
1. If top = - 1
Print “underflow”
2. repeat step 3 for i = top to i >= 0
3. Print stack[i] 4. Return
CODE
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 // Maximum size of the stack
// Stack structure
struct Stack {
int arr[MAX];
int top;
};
// Function to initialize the stack
void initStack(struct Stack* stack) {
stack->top = -1; // Stack is initially empty
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow! Cannot push %d\n", value);
} else {
stack->arr[++stack->top] = value;
printf("%d pushed onto stack.\n", value);
}
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow! Cannot pop from an empty stack.\n");
return -1; // Return -1 to indicate stack is empty
} else {
return stack->arr[stack->top--];
}
}
// Function to display the stack
void display(struct Stack* stack) {
if (isEmpty(stack))
{ printf("Stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = stack->top; i>= 0; i--)
{ printf("%d ", stack->arr[i]);
}
printf("\n");
}
}
// Main function
int main() {
struct Stack stack;
initStack(&stack);
int choice, value;
do {
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display Stack\n");
printf("4. Check if Stack is Empty\n");
printf("5. Exit\n");
printf("Enter your choice:
"); scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
value = pop(&stack);
if (value != -1) {
printf("%d popped from stack.\n", value);
}
break;
case 3:
display(&stack);
break;
case 4:
if (isEmpty(&stack))
{ printf("Stack is empty.\n");
} else {
printf("Stack is not empty.\
n");
}
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
PROGRAM NO: 8
AIM:- Write a menu driven program to implement stack using Linked List.
ALGORITHM
PUSH( )
1. t = newnode( )
2. Enter info to be inserted
3. Read n
4. t info = n
5. t next = top
6. top = t
7. Return
POP( )
1. If (top = NULL)
Print “ underflow”
Return
2. x = top
3. top = top next
4. delnode(x)
5. Return
CODE
#include <stdio.h>
#include <stdlib.h>
// Node structure for the linked list
struct Node {
int data;
struct Node* next;
};
// Stack structure
struct Stack {
struct Node* top;
};
do { printf("\
nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display Stack\n");
printf("4. Check if Stack is Empty\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
value = pop(&stack);
if (value != -1) {
printf("%d popped from stack.\n", value);
}
break;
case 3:
display(&stack);
break;
case 4:
if (isEmpty(&stack))
{ printf("Stack is empty.\n");
} else {
printf("Stack is not empty.\
n");
}
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
PROGRAM NO: 9
ALGORITHM
P postfix expression
1. Add a right parenthesis “)” at the end of P
2. Scan P from left to right and repeat steps 3 &
4 until sentinel “)” is encountered
3. If an operand is encountered, put it on stack 4. If
an operator is encountered , then:
a) Remove the top two elements of stack , where A is the top element &
B is the next to top element b) Evaluate B A
c) Place the result of (b) back on stack 5. Set
value equal to the top element on stack
6. Exit
CODE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
switch (expression[i]) {
case '+':
result = leftOperand + rightOperand;
break;
case '-':
result = leftOperand - rightOperand;
break;
case '*':
result = leftOperand * rightOperand;
break;
case '/':
if (rightOperand == 0) {
printf("Error: Division by zero.\n");
return -1; // Error condition
}
result = leftOperand / rightOperand;
break;
default:
printf("Error: Invalid operator %c\n", expression[i]);
return -1; // Error condition
}
push(&stack, result);
}
i++;
}
return pop(&stack); // The final result will be the only element left in the stack
}
// Main function
int main() {
char expression[MAX];
return 0;
}
PROGRAM NO: 10
ALGORITHM
Q arithmetic expression
P postfix expression
1. Push “(“ onto stack, and add “)” to the end of Q
2. Scan Q from left to right and repeat steps 3 to 6 for each element of
Q untill the stack is empty
3. If an operand is encountered , add it to P
4. If a left parenthesis is encountered, push it onto stack 5. If an operator
is encountered , then:
(a) Repeatedly pop from stack and add to P each operator
which has the same precedence as or higher precedence
than
(b) Add to stack
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from stack and add to P each operator until a
left parenthesis is encountered (b) Remove the left parenthesis
7. Exit
CODE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 100 // Maximum size for the stack and expression
// Stack structure
struct Stack {
char arr[MAX];
int top;
};
// Main function
int main() {
char infix[MAX], postfix[MAX];
printf("Enter an infix expression (e.g., A + B * C): ");
fgets(infix, sizeof(infix), stdin);
infix[strcspn(infix, "\n")] = 0; // Remove trailing newline
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
PROGRAM NO: 11
AIM:- Write a menu driven program to implement queue using Linked List.
ALGORITHM
CREATE
1. t = new node
2. Enter info to be inserted
3. Read n
4. t info = n
5. t next = front 6. front = t
INSERTION
1. r next = t
2. t next = NULL
3. Return
DELETION
1. x = front
2. front = front next
3. delnode(x) 4. Return
DISPLAY
1. If (front = NULL)
Print “ empty
queue” Return
Else
P = start
Repeat until (p<> NULL)
Print p info
P=p next
Return
Program
CODE
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Queue* queue = createQueue();
int choice, data;
switch (choice)
{ case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &data);
enqueue(queue, data);
break;
case 2:
dequeue(queue);
break;
case 3:
displayQueue(queue);
break;
case 4:
printf("Exiting the program.\n");
free(queue); // Free the queue structure
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
PROGRAM NO: 12
AIM:- Write a Menu Driven Program to implement various operation of Singly Linked List.
ALGORITHM
1. t = newmode( )
2. Enter info to be inserted
3. Read n
4. t info = n
5. t next = start
6. Start = t
INSERTION
BEGIN
1. t next = start
2. start = t
Return
MIDDLE
1. Enter info of the node after which new node
to be inserted
2. Read x
3. p = start
4. Repeat step 5 until p info <> x
5. p = p next
6. t next = p next
7. p next = t
8. Return
LAST
1. p = start
2. Repeat step 3 until p next NULL
3. p = p next
4. t next = NULL
5. p next = t
6. Return
DELETION
BEGIN
1. x = start
2. start = start next
3. delnode(x)
MIDDLE
1. Enter the info of node to be deleted
2. Read n
3. p = start
4. c = start
5. while (c info <> NULL) p = c c = c next
6. p next = c next
7. delnode ( c )
8. Return
LAST
1. p = start c = start
2. while (c next <> NULL)
p = c c = c next 3. p
next = c next
4. delnode ( c)
5. Return
TRAVERSAL
1. p = start
2. while (p <> NULL) Print p info
P=p next
3. Return
CODE
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
int choice, data;
AIM:-Write a Menu Driven Program to implement various operation of Doubly Linked List.
ALGORITHM
1. t = new node
2. Enter “the info to be inserted”
3. Read n
4. t info = n
5. t next = NULL 6. t prev
NULL INSERTION
BEGIN
1. If start = NULL start = t
2. else
t next = NULL t
next prev = t
start = t
Return
MIDDLE
1. Print “ enter info of the node after which
you want to insert”
2. Read x
3. p = start
4. Repeat while p<> NULL If (p info = n) t
next = p next p next = t t prev
=p p next prev = t
Return
Else
P=p next
5. Print x not found
t next = NULL p
next = t
DELETION
BEGIN
1. p = start
2. p next prev = NULL
3. start = p next
4. start = p next
5. delnode(p)
6. Return
MIDDLE
1. Enter “info of the node to be deleted”
2. Read x
3. p = start
4. Repeat until p<> NULL If(p info = x)
p prev next = p
next p next prev = p
prev delnode(p) Return
Else
P=p next
5. Print “x not
found” LAST
1. P = start
2. Repeat while p<> NULL
If(p next = NULL)
Delnode(p)
3. Return
DISPLAY
1. p = start
2. Repeat while p <> NULL
Print p info
P=p next
CODE
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
int choice, data;
switch (choice)
{ case 1:
printf("Enter the value to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter the value to insert at the end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter the value to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 4:
displayList(head);
break;
case 5:
printf("Enter the value to search: ");
scanf("%d", &data);
searchElement(head, data);
break;
case 6:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
PROGRAM NO: 14
ALGORITHM
INSERTION
1. t = newnode
2. t -> info = n
3. t -> left = t -> right = NULL 4. If (root =
NULL) root = t return 5. ptr = root
6. Repeat step 7 until ptr = NULL
7. If (ptr -> info > n)
If (ptr -> left = NULL)
Ptr ->left = t
Return
Else
Ptr = ptr -> left
Else
If (ptr ->right = NULL)
Ptr ->right = t
Return
Else
Ptr = ptr -> right
DELETION
1. If (root = NULL)
Print “Empty tree “
Return
2. ptr = root, par = NULL
3. Repeat step 4 & 5 until (ptr info = n or ptr
= NULL)
4. par = ptr
5. If (ptr-> info > n) ptr = ptr ->left Else
Ptr = ptr ->right 6.
If ptr = NULL
print “ no. not present”
CODE
#include <stdio.h>
#include <stdlib.h>
// Node with two children: Get the inorder successor (smallest in the right subtree)
struct Node* temp = findMin(root->right);
root->data = temp->data; // Copy the inorder successor's content to this node
root->right = deleteNode(root->right, temp->data); // Delete the inorder successor
}
return root;
}
// Main function
int main() {
struct Node* root = NULL;
int choice, data;
switch (choice)
{ case 1:
printf("Enter the value to insert: ");
scanf("%d", &data);
root = insert(root, data);
printf("%d inserted into the BST.\n", data);
break;
case 2:
printf("Enter the value to search: ");
scanf("%d", &data);
struct Node* foundNode = search(root, data);
if (foundNode != NULL) {
printf("%d found in the BST.\n", data);
} else {
printf("%d not found in the BST.\n", data);
}
break;
case 3:
printf("Enter the value to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
printf("%d deleted from the BST.\n", data);
break;
case 4:
printf("In-order Traversal:
"); inorderTraversal(root);
printf("\n");
break;
case 5:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
PROGRAM NO: 15
ALGORITHM
Preorder(root)
If root = null then exit
Process root->info
Preorder root->left;
Preorder root->right
Exit
Inorder(root)
If root = null then exit
Inorder root->left
Process root->info
Inorder root->right
Exit
Postorder(root)
If root = null then exit
Postorder root->left
Postorder root->right
Postorder root->info exit
CODE
#include <stdio.h>
#include <stdlib.h>
queue[rear++] = root;
// Main function
int main() {
// Creating a simple binary tree
struct Node* root =
createNode(1); root->left =
createNode(2);
root->right = createNode(3);
root->left->left =
createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
printf("In-order Traversal:
"); inorderTraversal(root);
printf("\n");
printf("Pre-order Traversal:
"); preorderTraversal(root);
printf("\n");
printf("Post-order Traversal:
"); postorderTraversal(root);
printf("\n");
printf("Level-order Traversal:
"); levelOrderTraversal(root);
printf("\n");
ADDED PRACTICALS
PROGRAM NO: 16
AIM:- Write a program to perform following operations on matrix using functions - Addition,
Subtraction, Multiplication, and Transpose.
ALGORITHM
Matmul(a,b,m,n,p)
1 for(i=1 to m)
2 for(j = 1 to p)
3 c[i][j] =0;
4 for(k= 1to n)
5 c[i][j] = c[i][j]+a[i][j]*b[i][j]
6 exit
CODE
#include <stdio.h>
#define MAX 10 // Maximum size of the matrix
// Function prototypes
void inputMatrix(int matrix[MAX][MAX], int rows, int cols);
void displayMatrix(int matrix[MAX][MAX], int rows, int cols);
void addMatrices(int a[MAX][MAX], int b[MAX][MAX], int result[MAX][MAX], int rows, int cols);
void subtractMatrices(int a[MAX][MAX], int b[MAX][MAX], int result[MAX][MAX], int rows, int
cols);
void multiplyMatrices(int a[MAX][MAX], int b[MAX][MAX], int result[MAX][MAX], int rowsA, int
colsA, int colsB);
void transposeMatrix(int matrix[MAX][MAX], int result[MAX][MAX], int rows, int cols);
int main() {
int a[MAX][MAX], b[MAX][MAX], result[MAX][MAX];
int rowsA, colsA, rowsB, colsB;
int choice;
do { printf("\nMenu:\
n");
printf("1. Add Matrices\n");
printf("2. Subtract Matrices\n");
printf("3. Multiply Matrices\n");
printf("4. Transpose First Matrix\
n");
printf("5. Transpose Second Matrix\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{ case 1:
if (rowsA == rowsB&&colsA == colsB)
{ addMatrices(a, b, result, rowsA, colsA);
printf("Result of Addition:\n");
displayMatrix(result, rowsA, colsA);
} else {
printf("Matrices must be of the same dimensions for addition.\n");
}
break;
case 2:
if (rowsA == rowsB&&colsA == colsB)
{ subtractMatrices(a, b, result, rowsA, colsA);
printf("Result of Subtraction:\n");
displayMatrix(result, rowsA, colsA);
} else {
printf("Matrices must be of the same dimensions for subtraction.\n");
}
break;
case 3:
if (colsA == rowsB)
{ multiplyMatrices(a, b, result, rowsA, colsA,
colsB); printf("Result of Multiplication:\n");
displayMatrix(result, rowsA, colsB);
} else {
printf("Number of columns in the first matrix must equal the number of rows in the second matrix for
multiplication.\n");
}
break;
case 4:
transposeMatrix(a, result, rowsA, colsA);
printf("Transpose of First Matrix:\n");
displayMatrix(result, colsA, rowsA);
break;
case 5:
transposeMatrix(b, result, rowsB,
colsB); printf("Transpose of Second
Matrix:\n"); displayMatrix(result, colsB,
rowsB);
break;
case 6:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 6);
return 0;
}
ALGORITHM
1. Initialize Buckets:
Determine the number of buckets based on the array size.
Create an array of empty buckets.
1. Distribute Elements into Buckets:
Calculate the index for each element in the array by mapping the element’s value to
its corresponding bucket.
Place each element in its respective bucket.
1. Sort Each Bucket:
1. Sort each bucket individually. Use a sorting algorithm like Insertion Sort for each
bucket, which is efficient for small sizes.
1. Concatenate Buckets:
1. Combine all sorted buckets to obtain the final sorted array.
CODE
#include <stdio.h>
#include <stdlib.h>
#define MAX 10 // Maximum number of elements in the array
#define BUCKETS 5 // Number of buckets
void bucketSort(float arr[], int n);
void insertionSort(float arr[], int n);
void printArray(float arr[], int n);
int main() {
float arr[MAX] = {0.42, 0.32, 0.33, 0.52, 0.37, 0.47, 0.51};
int n = 7; // Number of elements in the array
bucketSort(arr, n);
printf("\nSorted Array: \
n"); printArray(arr, n);
return 0;
}
ALGORITHM
Initialize the Problem:
Let n be the number of disks.
There are three poles: Source (A), Auxiliary (B), and Destination (C).
Represent each pole as a stack.
Push Initial State to Stack:
Define a structure, say Move, that contains information about the source pole, destination pole, and
the number of disks.
Create an empty stack for moves, and push the initial move (i.e., move n disks from Source
to Destination using Auxiliary).
Iteratively Process Moves:
While there are moves in the stack:
o Pop the top move from the stack.
o Check the number of disks (num) for this move:
If num is 1:
Move the disk from the Source to Destination pole.
If num> 1:
Break down the move into three sub-moves (mimicking the recursive steps):
....1. Push a move to transfer num - 1 disks from Auxiliary to Destination using Source.
....2. Push a move to transfer the single largest disk from Source to Destination.
....3. Push a move to transfer num - 1 disks from Source to Auxiliary using Destination.
End of Algorithm:
Repeat the above steps until all disks have been moved from the Source pole to the Destination pole.
Print each move as disks are transferred to visualize the process.
CODE
#include <stdio.h>
#include <stdlib.h>
typedef struct Move {
int num; // Number of disks to move
char src; // Source pole
char dest; // Destination pole
char aux; // Auxiliary pole
} Move;
typedef struct Stack
{ Move data[100];
int top;
} Stack;
void push(Stack *s, Move move)
{ s->data[++(s->top)] = move;
}
Move pop(Stack *s) {
return s->data[(s->top)--];
}
int isEmpty(Stack *s)
{ return s->top == -1;
}
void towerOfHanoi(int n, char src, char dest, char aux)
{ Stack s;
s.top = -1;
while (!isEmpty(&s))
{ Move move =
pop(&s);
if (move.num == 1) {
printf("Move disk 1 from %c to %c\n", move.src, move.dest);
} else {
// Push the steps in reverse order to simulate recursive calls in stack
Move step1 = { move.num - 1, move.aux, move.dest, move.src };
Move step2 = { 1, move.src, move.dest, move.aux };
Move step3 = { move.num - 1, move.src, move.aux, move.dest };
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}.