https://github.com/holasoymas/dsa
https://github.com/holasoymas/dsa
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/holasoymas/dsa
- Owner: holasoymas
- Created: 2023-09-28T05:00:44.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-08T08:06:23.000Z (12 months ago)
- Last Synced: 2025-02-06T02:08:11.205Z (4 months ago)
- Language: C
- Size: 193 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Data Structures and Algorithms (DSA)
This repository contains various data structures and algorithm problems and their solutions.
## Table of Contents
* [Tower of Hanoi](#tower-of-hanoi)
## Tower of Hanoi
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3. No disk may be placed on top of a smaller disk.### C solution
```
#includevoid towers(int, char, char, char);
int main() {
int n;
printf("Enter the number of tower : \n");
scanf("%d", &n);
towers(n, 'A', 'C', 'B');
return 0;
}void towers(int n, char from, char to, char aux) {
if (n == 1) {
printf("Move disk %d from ring %c to ring %c\n", n, from, to);
return;
}
towers(n - 1, from, aux, to);
printf("Move disk %d from ring %c to ring %c\n", n, from, to);
towers(n - 1, aux, to, from);
}
```