From c8c21bf445baf5410fa01a867e5672a7f1bc5811 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Fri, 6 Oct 2023 01:40:09 +0530 Subject: [PATCH] Update TowerOfHanoi.c --- algorithms/data-structures/TowerOfHanoi.c | 108 ++++++++++++++++++---- 1 file changed, 88 insertions(+), 20 deletions(-) diff --git a/algorithms/data-structures/TowerOfHanoi.c b/algorithms/data-structures/TowerOfHanoi.c index 49a83571..3bd9fe4d 100644 --- a/algorithms/data-structures/TowerOfHanoi.c +++ b/algorithms/data-structures/TowerOfHanoi.c @@ -1,24 +1,92 @@ #include - - void towers(int, char, char, char); - -int main() -{ - int num; - printf("Enter the number of disks : "); - scanf("%d", &num); - printf("The sequence of moves involved in the Tower of Hanoi are :\n"); - towers(num, 'A', 'C', 'B'); - return 0; +#include + +// Structure to represent a stack +struct Stack { + int capacity; + int top; + int* array; +}; + +// Function to create a stack of given capacity +struct Stack* createStack(int capacity) { + struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); + stack->capacity = capacity; + stack->top = -1; + stack->array = (int*)malloc(stack->capacity * sizeof(int)); + return stack; +} + +// 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 item) { + stack->array[++stack->top] = item; +} + +// Function to pop an element from the stack +int pop(struct Stack* stack) { + if (!isEmpty(stack)) { + return stack->array[stack->top--]; + } + return -1; // Stack is empty } -void towers(int num, char frompeg, char topeg, char auxpeg) -{ - if (num == 1) - { - printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg); - return; + +// Function to move a disk from one peg to another and print the step +void moveDisk(char from, char to, int disk) { + printf("Move disk %d from rod %c to rod %c\n", disk, from, to); +} + +// Function to solve Tower of Hanoi iteratively +void towerOfHanoiIterative(int n, struct Stack* source, struct Stack* auxiliary, struct Stack* destination) { + // Calculate the total number of moves required + int total_moves = (1 << n) - 1; + + // Determine the direction of movement based on whether n is even or odd + char from_peg = 'A', to_peg = 'C', aux_peg = 'B'; + if (n % 2 == 0) { + to_peg = 'B'; + aux_peg = 'C'; + } + + for (int move = 1; move <= total_moves; ++move) { + if (move % 3 == 1) { + moveDisk(from_peg, to_peg, pop(source)); + } else if (move % 3 == 2) { + moveDisk(from_peg, aux_peg, pop(auxiliary)); + } else { + moveDisk(aux_peg, to_peg, pop(destination)); + } } - towers(num - 1, frompeg, auxpeg, topeg); - printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg); - towers(num - 1, auxpeg, topeg, frompeg); } + +int main() { + int n = 4; // Number of disks + + // Create stacks for the source, auxiliary, and destination pegs + struct Stack* source = createStack(n); + struct Stack* auxiliary = createStack(n); + struct Stack* destination = createStack(n); + + // Initialize the source peg with disks + for (int i = n; i >= 1; i--) { + push(source, i); + } + + // Solve the Tower of Hanoi problem iteratively + towerOfHanoiIterative(n, source, auxiliary, destination); + + // Free memory for the stacks + free(source->array); + free(source); + free(auxiliary->array); + free(auxiliary); + free(destination->array); + free(destination); + + return 0; +} +