0% found this document useful (0 votes)
9 views61 pages

3.recursion Answers

The document contains multiple C programs demonstrating various algorithms, including calculating the sum of an array, reversing a string, checking for palindromes, calculating power, solving the Tower of Hanoi, generating string permutations, and managing a linked list. Each program includes source code, user input prompts, and example outputs. The author is Pankaj Gop, a second-year student in CSE(IoTCSBT).

Uploaded by

ARJAK GHOSH
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)
9 views61 pages

3.recursion Answers

The document contains multiple C programs demonstrating various algorithms, including calculating the sum of an array, reversing a string, checking for palindromes, calculating power, solving the Tower of Hanoi, generating string permutations, and managing a linked list. Each program includes source code, user input prompts, and example outputs. The author is Pankaj Gop, a second-year student in CSE(IoTCSBT).

Uploaded by

ARJAK GHOSH
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/ 61

Page 1 of 61

PART 1

Source Code (Program 1) :-

#include <stdio.h>

int arraySum(int arr[], int n)

if (n == 0)

return 0;

return arr[n-1] + arraySum(arr, n-1);

void main()

int n; // Total number of elements in the array

printf("\nEnter the total number of elements in the array: ");

scanf("%d", &n);

int arr[n];

for(int i=0;i<n;i++)

printf("Enter element %d: ", i+1);

scanf("%d", &arr[i]);

printf("\nSum of all the array elements: %d\n\n", arraySum(arr, n));

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 2 of 61

Output (Program 1) :-

Enter the total number of elements in the array: 5

Enter element 1: 1

Enter element 2: 2

Enter element 3: 3

Enter element 4: 4

Enter element 5: 5

Sum of all the array elements: 15

PART 1

Source Code (Program 2) :-

#include <stdio.h>

#include <string.h> // For strlen() function

void reverseString(char *str, int start, int end)

if (start >= end)

return;

/*Swapping characters at start and end indices and recursively calling reverseString for the

remaining substring*/

char temp = str[start];

str[start] = str[end];

str[end] = temp;

reverseString(str, start + 1, end - 1);

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 3 of 61

void main()

char str[100];

printf("\nEnter a string: ");

fgets(str, 100, stdin); // To include the space character in the string

// Removes the newline character from the input string

str[strcspn(str, "\n")] = 0;

int n = strlen(str);

reverseString(str, 0, n-1);

printf("\nReversed string: %s\n\n", str);

Output (Program 2) :-

Enter a string: My name is Pankaj!

Reversed string: !jaknaP si eman yM

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 4 of 61

PART 1

Source Code (Program 3) :-

#include <stdio.h>

#include <ctype.h> // For isalnum() function and tolower() function

#include <string.h> // For strlen() function

// Helper function to check if a character is alphanumeric

int isAlphanumeric(char ch)

return isalnum((unsigned char)ch);

// Recursive function to check if a string is a palindrome

int isPalindromeRecursive(char *str, int start, int end)

// Moving left index to the next alphanumeric character

while (start < end && !isAlphanumeric(str[start]))

start++;

// Moving right index to the previous alphanumeric character

while (start < end && !isAlphanumeric(str[end]))

end--;

// Base case: if left index crosses or meets right index

if (start >= end)

return 1;

// Converting characters to lowercase for case insensitivity and check equality

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 5 of 61

if (tolower((unsigned char)str[start]) != tolower((unsigned char)str[end]))

return 0;

// Recursive case

return isPalindromeRecursive(str, start + 1, end - 1);

// Function to check if a string is a palindrome

int isPalindrome(char *str)

return isPalindromeRecursive(str, 0, strlen(str) - 1);

void main()

char str[100];

printf("\nEnter a string: ");

fgets(str, 100, stdin); // To include the space character in the string

// Removes the newline character from the input string

str[strcspn(str, "\n")] = 0;

printf("Is \"%s\" a palindrome? %s\n", str, isPalindrome(str) ? "Yes" : "No");

Output (Program 3) :-

Case 1:

Enter a string: abacaba

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 6 of 61

Is "abacaba" a palindrome? Yes

Case 2:

Enter a string: Man

Is "Man" a palindrome? No

PART 1

Source Code (Program 4) :-

#include <stdio.h>

double power(double base, int exponent)

if (exponent == 0)

return 1;

else if (exponent <1)

return 1/(power(base, -exponent));

else

return base * power(base, exponent - 1);

void main()

double base;

int exponent;

printf("\nEnter base: ");

scanf("%lf", &base);

printf("\nEnter exponent: ");

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 7 of 61

scanf("%d", &exponent);

double result = power(base, exponent);

printf("\n%lf^%d = %lf\n\n", base, exponent, result);

Output (Program 4) :-

Enter base: 2

Enter exponent: -3

2.000000^-3 = 0.125000

PART 1

Source Code (Program 5) :-

#include <stdio.h>

void towerOfHanoi(int n, char source, char destination, char auxiliary)

if (n == 1)

printf("Move disk 1 from %c to %c\n", source, destination);

return;

towerOfHanoi(n - 1, source, auxiliary, destination);

printf("Move disk %d from %c to %c\n", n, source, destination);

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 8 of 61

towerOfHanoi(n - 1, auxiliary, destination, source);

void main()

int n;

printf("\nEnter number of disks: ");

scanf("%d", &n);

towerOfHanoi(n, 'A', 'C', 'B'); // A = source, B = auxiliary, C = destination

Output (Program 5) :-

Enter number of disks: 3

Move disk 1 from A to C

Move disk 2 from A to B

Move disk 1 from C to B

Move disk 3 from A to C

Move disk 1 from B to A

Move disk 2 from B to C

Move disk 1 from A to C

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 9 of 61

PART 1

Source Code (Program 6) :-

#include <stdio.h>

#include <string.h>

// Function to swap two characters

void swap(char *x, char *y)

char temp = *x;

*x = *y;

*y = temp;

// Recursive function to generate permutations

void permute(char *str, int l, int r)

if (l == r)

printf("%s\n", str); // Base case: print the permutation

else

for (int i = l; i <= r; i++)

swap((str + l), (str + i)); // Swap current character with the start

permute(str, l + 1, r); // Recurse for the rest of the string

swap((str + l), (str + i)); // Backtrack to original configuration

void main()

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 10 of 61

char str[100]; // Array to store the input string

// User input

printf("Enter a string: ");

fgets(str, 100, stdin); // To include the space character in the string

// Removes the newline character from the input string

str[strcspn(str, "\n")] = 0;

int n = strlen(str); // Find the length of the string

printf("Permutations of the string are:\n");

permute(str, 0, n - 1); // Generate permutations

Output (Program 6) :-

Enter a string: ABC

Permutations of the string are:

ABC

ACB

BAC

BCA

CBA

CAB

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 11 of 61

PART 2

Source Code (Program 1) :-

#include <stdio.h>

#include <stdlib.h>

// Node structure definition

typedef struct Node

int data;

struct Node *next;

} Node; //making the alias, so that there's no need to write struct Node every time when required

// Function prototypes

void insertNode(Node **head, int value);

void deleteNode(Node **head, int value);

void displayList(Node *head);

// TODO: Extra things in this program STARTS HERE

int lengthOfList(Node *head);

// TODO: Extra things in this program ENDS HERE

void menu();

void main()

Node *head = NULL;

int choice, value, pos;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 12 of 61

while (1)

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insertNode(&head, value);

break;

case 2:

printf("Enter value to delete: ");

scanf("%d", &value);

deleteNode(&head, value);

break;

case 3:

displayList(head);

break;

// TODO: Extra things in this program STARTS HERE

case 4:

printf("Length of the list: %d\n", lengthOfList(head));

break;

// TODO: Extra things in this program ENDS HERE

case 5:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 13 of 61

void menu()

printf("\nMenu:\n");

printf("1. Insert a node in the list\n");

printf("2. Delete a node by value\n");

printf("3. Display the list\n");

// TODO: Extra things in this program STARTS HERE

printf("4. Length of the list\n");

// TODO: Extra things in this program ENDS HERE

printf("5. Exit the program\n");

void insertNode(Node **head, int value)

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

newNode->data = value;

newNode->next = NULL;

// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head

if (*head == NULL)

*head = newNode;

else

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 14 of 61

// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end

Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void deleteNode(Node **head, int value)

Node *temp = *head, *prev = NULL;

// If head node itself holds the value

if (temp != NULL && temp->data == value)

*head = temp->next;

free(temp);

return;

// Otherwise, searching for the value

while (temp != NULL && temp->data != value)

prev = temp;

temp = temp->next;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 15 of 61

// If value was not present in linked list

if (temp == NULL) return;

// Otherwise unlinking the node from linked list

prev->next = temp->next;

// Removing the temp Node

free(temp);

void displayList(Node *head)

Node *temp = head;

while (temp != NULL)

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

temp = temp->next;

printf("NULL\n");

// TODO: Extra things in this program STARTS HERE

int lengthOfList(Node *head)

if (head == NULL)

return 0;

return 1 + lengthOfList(head->next);

// TODO: Extra things in this program ENDS HERE

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 16 of 61

Output (Program 1) :-

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Length of the list

5. Exit the program

Enter your choice: 1

Enter value to insert: 10

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Length of the list

5. Exit the program

Enter your choice: 1

Enter value to insert: 20

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Length of the list

5. Exit the program

Enter your choice: 1

Enter value to insert: 30

Menu:

1. Insert a node in the list

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 17 of 61

2. Delete a node by value

3. Display the list

4. Length of the list

5. Exit the program

Enter your choice: 2

Enter value to delete: 20

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Length of the list

5. Exit the program

Enter your choice: 3

10 -> 30 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Length of the list

5. Exit the program

Enter your choice: 4

Length of the list: 2

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Length of the list

5. Exit the program

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 18 of 61

Enter your choice: 5

PART 2

Source Code (Program 2) :-

#include <stdio.h>

#include <stdlib.h>

// Node structure definition

typedef struct Node

int data;

struct Node *next;

} Node; //making the alias, so that there's no need to write struct Node every time when required

// Function prototypes

void insertNode(Node **head, int value);

void deleteNode(Node **head, int value);

void displayList(Node *head);

// TODO: Extra things in this program STARTS HERE

int searchElement(Node *head, int value);

// TODO: Extra things in this program ENDS HERE

void menu();

void main()

Node *head = NULL;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 19 of 61

int choice, value, pos;

while (1)

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insertNode(&head, value);

break;

case 2:

printf("Enter value to delete: ");

scanf("%d", &value);

deleteNode(&head, value);

break;

case 3:

displayList(head);

break;

// TODO: Extra things in this program STARTS HERE

case 4:

printf("Enter the value to search: ");

scanf("%d", &value);

if (searchElement(head, value))

printf("%d is present in the list.\n", value);

else

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 20 of 61

printf("%d is not present in the list.\n", value);

break;

// TODO: Extra things in this program ENDS HERE

case 5:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

void menu()

printf("\nMenu:\n");

printf("1. Insert a node in the list\n");

printf("2. Delete a node by value\n");

printf("3. Display the list\n");

// TODO: Extra things in this program STARTS HERE

printf("4. Search for an element\n");

// TODO: Extra things in this program ENDS HERE

printf("5. Exit the program\n");

void insertNode(Node **head, int value)

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

newNode->data = value;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 21 of 61

newNode->next = NULL;

// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head

if (*head == NULL)

*head = newNode;

else

// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end

Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void deleteNode(Node **head, int value)

Node *temp = *head, *prev = NULL;

// If head node itself holds the value

if (temp != NULL && temp->data == value)

*head = temp->next;

free(temp);

return;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 22 of 61

// Otherwise, searching for the value

while (temp != NULL && temp->data != value)

prev = temp;

temp = temp->next;

// If value was not present in linked list

if (temp == NULL) return;

// Otherwise unlinking the node from linked list

prev->next = temp->next;

// Removing the temp Node

free(temp);

void displayList(Node *head)

Node *temp = head;

while (temp != NULL)

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

temp = temp->next;

printf("NULL\n");

// TODO: Extra things in this program STARTS HERE

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 23 of 61

int searchElement(struct Node* head, int value)

if (head == NULL)

return 0;

if (head->data == value)

return 1;

return searchElement(head->next, value);

// TODO: Extra things in this program ENDS HERE

Output (Program 2) :-

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Search for an element

5. Exit the program

Enter your choice: 1

Enter value to insert: 10

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Search for an element

5. Exit the program

Enter your choice: 1

Enter value to insert: 20

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 24 of 61

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Search for an element

5. Exit the program

Enter your choice: 1

Enter value to insert: 30

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Search for an element

5. Exit the program

Enter your choice: 3

10 -> 20 -> 30 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Search for an element

5. Exit the program

Enter your choice: 4

Enter the value to search: 20

20 is present in the list.

Menu:

1. Insert a node in the list

2. Delete a node by value

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 25 of 61

3. Display the list

4. Search for an element

5. Exit the program

Enter your choice: 5

PART 2

Source Code (Program 3) :-

#include <stdio.h>

#include <stdlib.h>

// Node structure definition

typedef struct Node

int data;

struct Node *next;

} Node; //making the alias, so that there's no need to write struct Node every time when required

// Function prototypes

void insertNode(Node **head, int value);

void deleteNode(Node **head, int value);

void displayList(Node *head);

// TODO: Extra things in this program STARTS HERE

Node* mergeSortedLists(Node *head1, Node *head2); //function to merge two sorted linked lists in
a sorted manner

// TODO: Extra things in this program ENDS HERE

void menu();

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 26 of 61

void main()

// TODO: Extra things in this program STARTS HERE

Node *head1 = NULL;

Node *head2 = NULL;

Node *head_merged = NULL;

// TODO: Extra things in this program ENDS HERE

int choice, value, pos;

while (1)

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

// TODO: Extra things in this program STARTS HERE

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

printf("Which Linked List to insert 1 or 2: ");

scanf("%d", &pos);

if (pos == 1)

insertNode(&head1, value);

else if(pos == 2)

insertNode(&head2, value);

else

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 27 of 61

printf("Invalid choice. Please try again.\n");

break;

case 2:

printf("Enter value to delete: ");

scanf("%d", &value);

printf("Which Linked List to delete 1 or 2: ");

scanf("%d", &pos);

if (pos == 1)

deleteNode(&head1, value);

else if(pos == 2)

deleteNode(&head2, value);

else

printf("Invalid choice. Please try again.\n");

break;

case 3:

printf("Which Linked List to display 1 or 2 or 3(for merged): ");

scanf("%d", &pos);

if (pos == 1)

displayList(head1);

else if(pos == 2)

displayList(head2);

else if(pos == 3)

head_merged = mergeSortedLists(head1, head2);

displayList(head_merged);

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 28 of 61

else

printf("Invalid choice. Please try again.\n");

break;

// TODO: Extra things in this program ENDS HERE

case 4:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

void menu()

printf("\nMenu:\n");

printf("1. Insert a node in the list\n");

printf("2. Delete a node by value\n");

printf("3. Display the list\n");

printf("4. Exit the program\n");

void insertNode(Node **head, int value)

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

newNode->data = value;

newNode->next = NULL;

// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 29 of 61

if (*head == NULL)

*head = newNode;

else

// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end

Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void deleteNode(Node **head, int value)

Node *temp = *head, *prev = NULL;

// If head node itself holds the value

if (temp != NULL && temp->data == value)

*head = temp->next;

free(temp);

return;

// Otherwise, searching for the value

while (temp != NULL && temp->data != value)

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 30 of 61

prev = temp;

temp = temp->next;

// If value was not present in linked list

if (temp == NULL) return;

// Otherwise unlinking the node from linked list

prev->next = temp->next;

// Removing the temp Node

free(temp);

void displayList(Node *head)

Node *temp = head;

while (temp != NULL)

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

temp = temp->next;

printf("NULL\n");

// TODO: Extra things in this program STARTS HERE

Node* mergeSortedLists(Node *head1, Node *head2)

if (head1 == NULL)

return head2;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 31 of 61

if (head2 == NULL)

return head1;

Node *result = NULL;

if (head1->data <= head2->data)

result = head1;

result->next = mergeSortedLists(head1->next, head2);

else

result = head2;

result->next = mergeSortedLists(head1, head2->next);

// TODO: Extra things in this program ENDS HERE

Output (Program 3) :-

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 1

Enter value to insert: 10

Which Linked List to insert 1 or 2: 1

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 32 of 61

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 1

Enter value to insert: 15

Which Linked List to insert 1 or 2: 2

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 1

Enter value to insert: 20

Which Linked List to insert 1 or 2: 1

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 1

Enter value to insert: 25

Which Linked List to insert 1 or 2: 2

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 33 of 61

4. Exit the program

Enter your choice: 1

Enter value to insert: 30

Which Linked List to insert 1 or 2: 1

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 3

Which Linked List to display 1 or 2 or 3(for merged): 1

10 -> 20 -> 30 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 3

Which Linked List to display 1 or 2 or 3(for merged): 2

15 -> 25 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 3

Which Linked List to display 1 or 2 or 3(for merged): 3

10 -> 15 -> 20 -> 25 -> 30 -> NULL

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 34 of 61

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Exit the program

Enter your choice: 4

PART 2

Source Code (Program 4) :-

#include <stdio.h>

#include <stdlib.h>

// Node structure definition

typedef struct Node

int data;

struct Node *next;

} Node; //making the alias, so that there's no need to write struct Node every time when required

// Function prototypes

void insertNode(Node **head, int value);

void deleteNode(Node **head, int value);

void displayList(Node *head);

// TODO: Extra things in this program STARTS HERE

void reverseLinkedList(struct Node** head);

// TODO: Extra things in this program ENDS HERE

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 35 of 61

void menu();

void main()

Node *head = NULL;

int choice, value, pos;

while (1)

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insertNode(&head, value);

break;

case 2:

printf("Enter value to delete: ");

scanf("%d", &value);

deleteNode(&head, value);

break;

case 3:

displayList(head);

break;

// TODO: Extra things in this program STARTS HERE

case 4:

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 36 of 61

reverseLinkedList(&head);

break;

// TODO: Extra things in this program ENDS HERE

case 5:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

void menu()

printf("\nMenu:\n");

printf("1. Insert a node in the list\n");

printf("2. Delete a node by value\n");

printf("3. Display the current list\n"); //TODO: the word "current" is added here

// TODO: Extra things in this program STARTS HERE

printf("4. Reverse the list\n");

// TODO: Extra things in this program ENDS HERE

printf("5. Exit the program\n");

void insertNode(Node **head, int value)

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

newNode->data = value;

newNode->next = NULL;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 37 of 61

// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head

if (*head == NULL)

*head = newNode;

else

// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end

Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void deleteNode(Node **head, int value)

Node *temp = *head, *prev = NULL;

// If head node itself holds the value

if (temp != NULL && temp->data == value)

*head = temp->next;

free(temp);

return;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 38 of 61

// Otherwise, searching for the value

while (temp != NULL && temp->data != value)

prev = temp;

temp = temp->next;

// If value was not present in linked list

if (temp == NULL) return;

// Otherwise unlinking the node from linked list

prev->next = temp->next;

// Removing the temp Node

free(temp);

void displayList(Node *head)

Node *temp = head;

while (temp != NULL)

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

temp = temp->next;

printf("NULL\n");

// TODO: Extra things in this program STARTS HERE

void reverseLinkedList(struct Node** head)

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 39 of 61

struct Node* prev = NULL;

struct Node* current = *head;

struct Node* next = NULL;

while (current != NULL)

next = current->next; // Stores next node

current->next = prev; // Reverses the current node's pointer

prev = current; // Moves the pointers one position ahead

current = next;

*head = prev;

// TODO: Extra things in this program ENDS HERE

Output (Program 4) :-

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the current list

4. Reverse the list

5. Exit the program

Enter your choice: 1

Enter value to insert: 10

Menu:

1. Insert a node in the list

2. Delete a node by value

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 40 of 61

3. Display the current list

4. Reverse the list

5. Exit the program

Enter your choice: 1

Enter value to insert: 20

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the current list

4. Reverse the list

5. Exit the program

Enter your choice: 1

Enter value to insert: 30

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the current list

4. Reverse the list

5. Exit the program

Enter your choice: 3

10 -> 20 -> 30 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the current list

4. Reverse the list

5. Exit the program

Enter your choice: 4

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 41 of 61

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the current list

4. Reverse the list

5. Exit the program

Enter your choice: 3

30 -> 20 -> 10 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the current list

4. Reverse the list

5. Exit the program

Enter your choice: 5

PART 2

Source Code (Program 5) :-

#include <stdio.h>

#include <stdlib.h>

// TODO: Extra things in this program STARTS HERE

int c=0;

// TODO: Extra things in this program ENDS HERE

// Node structure definition

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 42 of 61

typedef struct Node

int data;

struct Node *next;

} Node; //making the alias, so that there's no need to write struct Node every time when required

// Function prototypes

void insertNode(Node **head, int value);

void deleteNode(Node **head, int value);

void displayList(Node *head);

// TODO: Extra things in this program STARTS HERE

void findMiddleRecursive(Node *head, int a);

// TODO: Extra things in this program ENDS HERE

void menu();

void main()

Node *head = NULL;

int choice, value, pos;

while (1)

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter value to insert: ");

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 43 of 61

scanf("%d", &value);

insertNode(&head, value);

c++;

break;

case 2:

printf("Enter value to delete: ");

scanf("%d", &value);

deleteNode(&head, value);

c--;

break;

case 3:

displayList(head);

break;

// TODO: Extra things in this program STARTS HERE

case 4:

findMiddleRecursive(head, 1);

break;

// TODO: Extra things in this program ENDS HERE

case 5:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

void menu()

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 44 of 61

printf("\nMenu:\n");

printf("1. Insert a node in the list\n");

printf("2. Delete a node by value\n");

printf("3. Display the list\n");

// TODO: Extra things in this program STARTS HERE

printf("4. Print the value of the middle node\n");

// TODO: Extra things in this program ENDS HERE

printf("5. Exit the program\n");

void insertNode(Node **head, int value)

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

newNode->data = value;

newNode->next = NULL;

// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head

if (*head == NULL)

*head = newNode;

else

// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end

Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal

while (temp->next != NULL)

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 45 of 61

temp = temp->next;

temp->next = newNode;

void deleteNode(Node **head, int value)

Node *temp = *head, *prev = NULL;

// If head node itself holds the value

if (temp != NULL && temp->data == value)

*head = temp->next;

free(temp);

return;

// Otherwise, searching for the value

while (temp != NULL && temp->data != value)

prev = temp;

temp = temp->next;

// If value was not present in linked list

if (temp == NULL) return;

// Otherwise unlinking the node from linked list

prev->next = temp->next;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 46 of 61

// Removing the temp Node

free(temp);

void displayList(Node *head)

Node *temp = head;

while (temp != NULL)

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

temp = temp->next;

printf("NULL\n");

// TODO: Extra things in this program STARTS HERE

void findMiddleRecursive(Node *head, int a)

if(a==((c+1)/2))

printf("Middle node is: %d\n", head->data);

return;

findMiddleRecursive(head->next, a+1); // Recursion call

// TODO: Extra things in this program ENDS HERE

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 47 of 61

Output (Program 5) :-

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Enter your choice: 1

Enter value to insert: 10

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Enter your choice: 1

Enter value to insert: 20

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Enter your choice: 1

Enter value to insert: 30

Menu:

1. Insert a node in the list

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 48 of 61

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Enter your choice: 1

Enter value to insert: 40

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Enter your choice: 1

Enter value to insert: 50

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Enter your choice: 3

10 -> 20 -> 30 -> 40 -> 50 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 49 of 61

Enter your choice: 4

Middle node is: 30

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Print the value of the middle node

5. Exit the program

Enter your choice: 5

PART 3

Source Code (Program 1) :-

#include <stdio.h>

#include <stdlib.h>

// Node structure definition

typedef struct Node

int data;

struct Node *next;

} Node; //making the alias, so that there's no need to write struct Node every time when required

// Function prototypes

void insertNode(Node **head, int value);

void deleteNode(Node **head, int value);

void displayList(Node *head);

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 50 of 61

// TODO: Extra things in this program STARTS HERE

int isPalindrome(struct Node** left, struct Node* right);

// TODO: Extra things in this program ENDS HERE

void menu();

void main()

Node *head = NULL;

int choice, value, pos;

while (1)

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insertNode(&head, value);

break;

case 2:

printf("Enter value to delete: ");

scanf("%d", &value);

deleteNode(&head, value);

break;

case 3:

displayList(head);

break;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 51 of 61

// TODO: Extra things in this program STARTS HERE

case 4:

if(isPalindrome(&head, head))

printf("The list is Palindromic.\n");

else

printf("The list is not Palindromic.\n");

break;

// TODO: Extra things in this program ENDS HERE

case 5:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

void menu()

printf("\nMenu:\n");

printf("1. Insert a node in the list\n");

printf("2. Delete a node by value\n");

printf("3. Display the list\n");

// TODO: Extra things in this program STARTS HERE

printf("4. Display if list is Palindromic or not\n");

// TODO: Extra things in this program ENDS HERE

printf("5. Exit the program\n");

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 52 of 61

void insertNode(Node **head, int value)

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

newNode->data = value;

newNode->next = NULL;

// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head

if (*head == NULL)

*head = newNode;

else

// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end

Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void deleteNode(Node **head, int value)

Node *temp = *head, *prev = NULL;

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 53 of 61

// If head node itself holds the value

if (temp != NULL && temp->data == value)

*head = temp->next;

free(temp);

return;

// Otherwise, searching for the value

while (temp != NULL && temp->data != value)

prev = temp;

temp = temp->next;

// If value was not present in linked list

if (temp == NULL) return;

// Otherwise unlinking the node from linked list

prev->next = temp->next;

// Removing the temp Node

free(temp);

void displayList(Node *head)

Node *temp = head;

while (temp != NULL)

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

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 54 of 61

temp = temp->next;

printf("NULL\n");

// TODO: Extra things in this program STARTS HERE

int isPalindrome(struct Node** left, struct Node* right)

if (right == NULL)

return 1;

int isPal = isPalindrome(left, right->next);

if (!isPal)

return 0;

int isEqual = ((*left)->data == right->data);

*left = (*left)->next;

return isEqual;

// TODO: Extra things in this program ENDS HERE

Output (Program 1) :-

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 1

Enter value to insert: 10

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 55 of 61

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 1

Enter value to insert: 20

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 1

Enter value to insert: 30

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 1

Enter value to insert: 20

Menu:

1. Insert a node in the list

2. Delete a node by value

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 56 of 61

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 1

Enter value to insert: 10

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 3

10 -> 20 -> 30 -> 20 -> 10 -> NULL

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 4

The list is Palindromic.

Menu:

1. Insert a node in the list

2. Delete a node by value

3. Display the list

4. Display if list is Palindromic or not

5. Exit the program

Enter your choice: 5

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 57 of 61

DISCUSSION ON THE PROGRAMS OF PART 1 :-

1. Array Sum

Objective: Calculate the sum of elements in an integer array using recursion.

1. Base Case(s):

If the array is empty (`size == 0`), the sum is `0`.

If there is only one element (`size == 1`), the sum is that element itself.

2. Problem Decomposition:

For an array of size `n`, the problem can be decomposed into calculating the sum of the first `n-1`
elements and then adding the `n`th element.

3. Composition of Solutions:

The solution to the problem is obtained by recursively summing the elements of the array from the
first to the second-last element, and then adding the last element (`arraySum(arr, n) = arr[n-1] +
arraySum(arr, n-1)`).

2. String Reversal

Objective: Reverse a given string using recursion.

1. Base Case(s):

If the string is empty (`length == 0`) or has only one character (`length == 1`), it is already reversed.

2. Problem Decomposition:

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 58 of 61

The problem can be broken down by fixing the first character and reversing the rest of the string.
After reversing the substring that excludes the first character, the first character is appended to the
end of the reversed substring.

3. Composition of Solutions:

The solution to reversing the entire string is achieved by recursively reversing the substring that
excludes the first character and then appending the first character to the end (`reverseString(str) =
reverseString(str + 1) + str[0]`).

3. Palindrome Check

Objective: Determine if a given string is a palindrome using recursion.

1. Base Case(s):

If the string is empty (`length == 0`) or has only one character (`length == 1`), it is a palindrome.

If the first and last characters are different, it is not a palindrome.

2. Problem Decomposition:

The problem can be reduced by checking if the first and last characters are the same and then
recursively checking if the substring that excludes these characters is a palindrome.

3. Composition of Solutions:

The solution is constructed by comparing the outermost characters and recursively confirming the
same for the inner substring (isPalindrome(str) = (str[0] == str[length-1]) &&
isPalindrome(str[1:length-2])`).

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 59 of 61

4. Power Function

Objective: Calculate `base` raised to the power of `exponent` using recursion.

1. Base Case(s):

If the exponent is `0`, the result is `1` (`base^0 = 1`).

If the exponent is `1`, the result is the base itself (`base^1 = base`).

2. Problem Decomposition:

The problem can be reduced by multiplying the base by the result of the power function with one
less exponent (`exponent - 1`).

3. Composition of Solutions:

The solution is obtained by recursively calculating the power for the reduced exponent and
multiplying by the base (`power(base, exponent) = base * power(base, exponent - 1)`).

5. Tower of Hanoi

Objective: Move `n` disks from a source peg to a destination peg using an auxiliary peg.

1. Base Case(s):

If there is only one disk (`n == 1`), move it directly from the source peg to the destination peg.

2. Problem Decomposition:

The problem can be decomposed into three steps:

1. Move the top `n-1` disks from the source peg to the auxiliary peg.

2. Move the `n`th disk from the source peg to the destination peg.

3. Move the `n-1` disks from the auxiliary peg to the destination peg.

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 60 of 61

3. Composition of Solutions:

The solution is constructed by recursively solving for `n-1` disks and using the results to achieve the
movement of the `n`th disk and the subsequent reorganization (`solveHanoi(n) = solveHanoi(n-1,
source, auxiliary, destination) + move(n, source, destination) + solveHanoi(n-1, auxiliary, destination,
source)`).

6. Permutations

Objective: Generate all possible permutations of a given string using recursion.

1. Base Case(s):

If the string is empty (`length == 0`) or has only one character (`length == 1`), the only permutation is
the string itself.

2. Problem Decomposition:

The problem can be decomposed by fixing one character and recursively finding all permutations of
the remaining substring. This process is repeated for each character in the string.

3. Composition of Solutions:

The solution is constructed by recursively generating permutations for the substrings and combining
them by prefixing each permutation with the fixed character (`permute(str) = {fixed_char +
permute(rest_of_str)}` for each character in the string).

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr


Page 61 of 61

DISCUSSION ON THE PROGRAM OF PART 3 :-

1. Palindrome Check for a Linked List

Base Case(s):

If the linked list is empty (`head` is `NULL`) or has only one node, it is a palindrome.

Problem Decomposition:

To check if a linked list is a palindrome, we can use two pointers: a slow pointer that moves one step
at a time and a fast pointer that moves two steps at a time. This will help in reaching the middle of
the linked list.

The recursive decomposition involves comparing nodes from the start and end moving toward the
center of the list:

1. Identify the Middle: Using the two-pointer technique, the slow pointer reaches the middle when
the fast pointer reaches the end.

2. Recursively Compare Nodes: Start comparing the elements from the start and the end. This can
be achieved by using a helper function that passes a reference to the start of the list and a reference
that moves to the end of the list during each recursive call.

Composition of Solutions:

The recursive function checks if the elements at the front and the corresponding elements at the
back (as the recursion unfolds) are the same:

1. The function first recursively traverses to the end of the linked list.

2. On the way back, it compares each node from the end with the corresponding node from the
front using a reference or a pointer.

3. If all elements match, then the linked list is a palindrome; otherwise, it is not.

Pankaj Gop (A 64) CSE(IoTCSBT) 2nd yr

You might also like