0% found this document useful (0 votes)
27 views11 pages

DS Programs

The document contains C programs for various data structures and algorithms, including a singly linked list implementation with functions for insertion, deletion, and display in reverse order. It also includes a recursive program to calculate the sum of digits of a number, a recursive solution for the Tower of Hanoi problem, and both recursive and iterative methods to compute the factorial of a number. Each program is provided without comments.

Uploaded by

viratttt91
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)
27 views11 pages

DS Programs

The document contains C programs for various data structures and algorithms, including a singly linked list implementation with functions for insertion, deletion, and display in reverse order. It also includes a recursive program to calculate the sum of digits of a number, a recursive solution for the Tower of Hanoi problem, and both recursive and iterative methods to compute the factorial of a number. Each program is provided without comments.

Uploaded by

viratttt91
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/ 11

DS programs

1. Define the term linked list. Write a C


program to implement singly linked list for
the following function using array : (i)
Insert at beginning (ii) Insert at end (iii)
Insert after element (iv) Delete at end (v)
Delete at beginning (vi) Delete after
element (vii) Display in reverse order
without comments

Ans -:

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int data[MAX_SIZE];
int next[MAX_SIZE];
int head = -1;
int size = 0;
void insert_beginning(int value) {
if (size == MAX_SIZE) {
printf("List is full.\n");
return;
}
int new_node = size++;
data[new_node] = value;
next[new_node] = head;
head = new_node;
}

void insert_end(int value) {


if (size == MAX_SIZE) {
printf("List is full.\n");
return;
}
int new_node = size++;
data[new_node] = value;
next[new_node] = -1;
if (head == -1) {
head = new_node;
return;
}
int temp = head;
while (next[temp] != -1) {
temp = next[temp];
}
next[temp] = new_node;
}

void insert_after(int value, int after_value) {


int temp = head;
while (temp != -1 && data[temp] != after_value) {
temp = next[temp];
}
if (temp == -1) {
printf("Element not found.\n");
return;
}
if (size == MAX_SIZE) {
printf("List is full.\n");
return;
}
int new_node = size++;
data[new_node] = value;
next[new_node] = next[temp];
next[temp] = new_node;
}

void delete_end() {
if (head == -1) {
printf("List is empty.\n");
return;
}
if (next[head] == -1) {
head = -1;
size--;
return;
}
int temp = head;
while (next[next[temp]] != -1) {
temp = next[temp];
}
next[temp] = -1;
size--;
}

void delete_beginning() {
if (head == -1) {
printf("List is empty.\n");
return;
}
head = next[head];
size--;
}

void delete_after(int after_value) {


int temp = head;
while (temp != -1 && data[temp] != after_value) {
temp = next[temp];
}
if (temp == -1 || next[temp] == -1) {
printf("Element not found or no element after it.\n");
return;
}
next[temp] = next[next[temp]];
size--;
}

void display_reverse(int current) {


if (current == -1) {
return;
}
display_reverse(next[current]);
printf("%d ", data[current]);
}

int main() {
insert_beginning(10);
insert_end(20);
insert_end(30);
insert_after(25, 20);
printf("List in reverse order: ");
display_reverse(head);
printf("\n");

delete_end();
delete_beginning();
delete_after(20);

printf("List in reverse order after deletions: ");


display_reverse(head);
printf("\n");
return 0;

2 Write a recursive program to find sum of


digits of the given number in c language
without comments

#include <stdio.h>

int sum_digits(int n) {
if (n == 0) {
return 0;
} else {
return (n % 10) + sum_digits(n / 10);
}
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of digits: %d\n", sum_digits(num));
return 0;
}

3-What is Tower of Hanoi problem ? Write


the recursive code in C language for the
problem with its algorithm. without
comments

The Tower of Hanoi is a mathematical puzzle where we have


three rods (let's call them A, B, and C) and a number of disks of
different sizes which can slide onto any rod. The puzzle starts
with the disks stacked in decreasing size on rod A, the smallest
disk being at the top, thus forming a conical shape.
The goal of the puzzle is to move the entire stack of disks from
rod A to rod C, obeying the following rules:
1. Only one disk can be moved at a time.
2. A larger disk cannot be placed on top of a smaller disk.
3. Disks can only be moved between the three rods.

Here's the recursive C code for the Tower of Hanoi problem,


along with its algorithm:
CODE-:
#include <stdio.h>

void tower_hanoi(int n, char source, char auxiliary, char


destination) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source,
destination);
return;
}
tower_hanoi(n - 1, source, destination, auxiliary);
printf("Move disk %d from %c to %c\n", n, source,
destination);
tower_hanoi(n - 1, auxiliary, source, destination);
}

int main() {
int num_disks;
printf("Enter the number of disks: ");
scanf("%d", &num_disks);
tower_hanoi(num_disks, 'A', 'B', 'C');
return 0;
}
Algorithm:

The tower_hanoi function implements the following recursive


algorithm:
1. Base Case: If there is only one disk (n == 1), simply move it
from the source rod to the destination rod.
2. Recursive Step: If there are more than one disk: a. Move
the top (n-1) disks from the source rod to the auxiliary rod,
using the destination rod as the auxiliary. b. Move the
largest disk (nth disk) from the source rod to the
destination rod. c. Move the (n-1) disks from the auxiliary
rod to the destination rod, using the source rod as the
auxiliary.

4- Write a recursive and non-recursive


program to calculate the factorial of the given
number. in c language without coments

#include <stdio.h>

int factorial_recursive(int n) {
if (n == 0) {

return 1;
} else {

return n * factorial_recursive(n - 1);


}
}

int factorial_iterative(int n) {

int result = 1;
for (int i = 1; i <= n; i++) {

result *= i;
}

return result;
}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");


} else {
printf("Factorial (recursive): %d\n",
factorial_recursive(num));
printf("Factorial (iterative): %d\n",
factorial_iterative(num));

return 0;

You might also like